Copy the sample code to a folder on the Raspberry Pi.
Open terminal, and then go to the folder you copied the sample.
Run the sample with the following command:
python3 digital_in_out.py
Sample Code
digital_in_out.py
#!/usr/bin/env python3#This sample demonstrates digital port read and write.#Install Sensor uHAT library with "pip3 install turta-sensoruhat"from time import sleepfrom turta_sensoruhat import Turta_Digital#Initialize#Set left digital port as output, right digital port as inputdigital = Turta_Digital.DigitalPort(False, False, True, True)try:whileTrue:#Turn pin 1 on digital.write(1, True)print("Pin 1 is set to high.")#Waitsleep(1.0)#Turn pin 1 off digital.write(1, False)print("Pin 1 is set to low.")#Waitsleep(1.0)#Toggle pin 2#Toggling inverts the pin state digital.toggle(2)print("Pin 2 is inverted to "+ ("high."if digital.read(2) else"low."))#Waitsleep(1.0)#Read pin 3 and 4 pin3 = digital.read(3) pin4 = digital.read(4)#Print pin 3 and 4 statesprint("Pin 3 state is "+ ("high."if pin3 else"low."))print("Pin 4 state is "+ ("high."if pin4 else"low."))#Waitsleep(1.0)#Exit on CTRL+CexceptKeyboardInterrupt:print('Bye.')
Result
When you run the sample, it toggles digital port 1 and reads digital port 2. The example output should be like this:
Pin 1 is set to high.
Pin 1 is set to low.
Pin 2 is inverted to high.
Pin 3 state is low.
Pin 4 state is low.
Pin 1 is set to high.
Pin 1 is set to low.
Pin 2 is inverted to low.
Pin 3 state is low.
Pin 4 state is low.