analog-input.py
Measure analog voltages with the internal SAMD51 16-bit analog-to-digital converter (ADC)
We use the built-in CircuitPython library called analogio
for analog input (as well as out). There are a number of pins on the SAMD51 capable of analog-to-digital conversion (ADC), one of which is convenient to access from the GPIO header below the microcontroller.
⚙ Hardware Needed
- PyCubed mainboard
📚 External Libraries Needed
None
📑 Code
import board
import analogio
ain5 = analogio.AnalogIn(board.AIN5)
# measure the voltage at pin AIN5 (raw 16-bit value)
raw_ain5 = ain5.value
print(raw_ain5)
# to see what the raw value is scaled from 0V to 3.3V
scaled_ain5 = (ain5.value * 3.3) / (2 ** 16)
print(scaled_ain5 )