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 analog_differential.py
Sample Code
analog_differential.py
#!/usr/bin/env python3#This sample demonstrates measuring differential analog inputs from analog ports.#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_protohat import Turta_Analog#Initializeanalog = Turta_Analog.ADC()try:whileTrue:#Read differential input on pads 1 and 2 a1_2 = analog.read(Turta_Analog.CH.DIFFERENTIAL_1_2)#Read differential input on pads 3 and 4 a3_4 = analog.read(Turta_Analog.CH.DIFFERENTIAL_3_4)#Read board temperature board_temp_c = analog.read_temperature() board_temp_f = analog.read_temperature(True)#Print the readingsprint("Analog Input 1-2: "+str(a1_2))print("Analog Input 3-4: "+str(a3_4))print("Board Temp......: "+str(round(board_temp_c, 1)) +"C"+ \" / "+str(round(board_temp_f, 1)) +"F")#Waitprint("-----")sleep(0.5)#Exit on CTRL+CexceptKeyboardInterrupt:print('Bye.')
Result
When you run the sample, it prints differential analog measurements from analog ports 1 and 2. The example output should be like this:
Analog Input 1-2: 0
Analog Input 3-4: 0
Board Temp......: 0C / 0F
-----