Single-Ended Sample
This sample demonstrates measuring single-ended analog inputs from analog pads.

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 analog_single_ended.py
Copied!

Sample Code

analog_single_ended.py
1
#!/usr/bin/env python3
2
3
#This sample demonstrates measuring single-ended analog inputs from analog pads.
4
#Install Proto HAT library with "pip3 install turta-protohat"
5
6
#Raspberry Pi Configuration
7
# - You should enable SPI and I2C from the Raspberry Pi's configuration.
8
# To do so, type 'sudo raspi-config' to the terminal, then go to 'Interfacing Options' and enable both SPI and I2C.
9
10
from time import sleep
11
from turta_protohat import Turta_Analog
12
13
#Initialize
14
analog = Turta_Analog.ADC()
15
16
try:
17
while True:
18
#Read analog channel 1
19
a1 = analog.read(Turta_Analog.CH.SINGLE_1)
20
21
#Read analog channel 2
22
a2 = analog.read(Turta_Analog.CH.SINGLE_2)
23
24
#Read analog channel 3
25
a3 = analog.read(Turta_Analog.CH.SINGLE_3)
26
27
#Read analog channel 4
28
a4 = analog.read(Turta_Analog.CH.SINGLE_4)
29
30
#Read board temperature
31
board_temp_c = analog.read_temperature()
32
board_temp_f = analog.read_temperature(True)
33
34
#Print the readings
35
print("Analog Input 1..: " + str(a1))
36
print("Analog Input 2..: " + str(a2))
37
print("Analog Input 3..: " + str(a3))
38
print("Analog Input 4..: " + str(a4))
39
print("Board Temp......: " + str(round(board_temp_c, 1)) + "C" + \
40
" / " + str(round(board_temp_f, 1)) + "F")
41
42
#Wait
43
print("-----")
44
sleep(0.5)
45
46
#Exit on CTRL+C
47
except KeyboardInterrupt:
48
print('Bye.')
Copied!

Result

When you run the sample, it prints analog readings from all analog inputs. The example output should be like this:
1
Analog Input 1..: 0
2
Analog Input 2..: 0
3
Analog Input 3..: 0
4
Analog Input 4..: 0
5
Board Temp......: 0C / 0F
Copied!
To stop the Python script, just press CTRL + C.
Last modified 1yr ago
Copy link