Download the sample code from https://github.com/Turta-io/Modular-HAT/blob/master/Samples/Python/digital_in_out.py
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:
digital_in_out.py#!/usr/bin/env python3#This sample demonstrates digital port read and write.#Install Modular HAT library with "pip3 install turta-modularhat"#Raspberry Pi Configuration# - You should enable SPI and I2C from the Raspberry Pi's configuration.# To do so, type 'sudo raspi-config' to the terminal, then go to 'Interfacing Options' and enable both SPI and I2C.from time import sleepfrom turta_modularhat import Turta_Digital#Initialize#Set left digital port as output, right digital port as inputdigital = Turta_Digital.DigitalPort(False, False, True, True)try:while True:#Turn pin 1 ondigital.write(1, True)print("Pin 1 is set to high.")#Waitsleep(1.0)#Turn pin 1 offdigital.write(1, False)print("Pin 1 is set to low.")#Waitsleep(1.0)#Toggle pin 2#Toggling inverts the pin statedigital.toggle(2)print("Pin 2 is inverted to " + ("high." if digital.read(2) else "low."))#Waitsleep(1.0)#Read pin 3 and 4pin3 = 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+Cexcept KeyboardInterrupt:print('Bye.')
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.
To stop the Python script, just press CTRL + C.