Digital In Out Sample
This sample demonstrates digital port read and write.

To run the sample:

    2.
    Copy the sample code to a folder on the Raspberry Pi.
    3.
    Open terminal, and then go to the folder you copied the sample.
    4.
    Run the sample with the following command:
1
python3 digital_port_in_out.py
Copied!

Sample Code

digital_port_in_out.py
1
#!/usr/bin/env python3
2
3
#This sample demonstrates digital port read and write.
4
#Install LoRa HAT library with "pip3 install turta-lorahat"
5
6
from time import sleep
7
from turta_lorahat import Turta_Digital
8
9
#Initialize
10
#Set left digital port as output, right digital port as input
11
digital = Turta_Digital.DigitalPort(False, False, True, True)
12
13
try:
14
while True:
15
#Turn pin 1 on
16
digital.write(1, True)
17
print("Pin 1 is set to high.")
18
19
#Wait
20
sleep(1.0)
21
22
#Turn pin 1 off
23
digital.write(1, False)
24
print("Pin 1 is set to low.")
25
26
#Wait
27
sleep(1.0)
28
29
#Toggle pin 2
30
#Toggling inverts the pin state
31
digital.toggle(2)
32
print("Pin 2 is inverted to " + ("high." if digital.read(2) else "low."))
33
34
#Wait
35
sleep(1.0)
36
37
#Read pin 3 and 4
38
pin3 = digital.read(3)
39
pin4 = digital.read(4)
40
41
#Print pin 3 and 4 states
42
print("Pin 3 state is " + ("high." if pin3 else "low."))
43
print("Pin 4 state is " + ("high." if pin4 else "low."))
44
45
#Wait
46
sleep(1.0)
47
48
#Exit on CTRL+C
49
except KeyboardInterrupt:
50
print('Bye.')
Copied!

Result

When you run the sample, it toggles digital port 1 and reads digital port 2. The example output should be like this:
1
Pin 1 is set to high.
2
Pin 1 is set to low.
3
Pin 2 is inverted to high.
4
Pin 3 state is low.
5
Pin 4 state is low.
6
Pin 1 is set to high.
7
Pin 1 is set to low.
8
Pin 2 is inverted to low.
9
Pin 3 state is low.
10
Pin 4 state is low.
Copied!
To stop the Python script, just press CTRL + C.
Last modified 1yr ago
Copy link