CircuitPython Analog In
This example shows you how you can read the analog voltage on the A1 pin on your board.
Copy and paste the code into code.py using your favorite editor, and save the file to run the demo.
Creating the analog input
analog1in = AnalogIn(board.A1)
Creates an object and connects the object to A1 as an analog input.
get_voltage Helper
getVoltage(pin) is our little helper program. By default, analog readings will range from 0 (minimum) to 65535
(maximum). This helper will convert the 0-65535 reading from pin.value and convert it a 0-3.3V voltage reading.
Main Loop
The main loop is simple. It prints out the voltage as floating point values by calling get_voltage on our analog object.
Connect to the serial console to see the results.
# CircuitPython AnalogIn Demo
import time
import board
from analogio import AnalogIn
analog_in = AnalogIn(board.A1)
def get_voltage(pin):
return (pin.value * 3.3) / 65536
while True:
print((get_voltage(analog_in),))
time.sleep(0.1)