Let's read an analog signal!

For this, we'll use a small 10k trim pot to set up a voltage divider. Here's the wiring diagram:

And here's the code:

import time
import board
import analogio

knob = analogio.AnalogIn(board.ADC0)

def get_voltage(raw):
    return (raw * 3.3) / 65536

while True:
    raw = knob.value
    volts = get_voltage(raw)
    print("raw = {:5d} volts = {:5.2f}".format(raw, volts))
    time.sleep(0.5)

Spin the knob and the values should change.

Note that even though the Pico's ADC is 12 bits, the value is scaled to 16 bits to comply with the CircuitPython API.

This guide was first published on May 01, 2021. It was last updated on May 01, 2021.

This page (ADC) was last updated on May 01, 2021.

Text editor powered by tinymce.