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
from analogio import AnalogIn

knob = AnalogIn(board.G1)

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))

Save that as something like adc.py and then run it with:

python3 adc.py

Spin the knob and the values should change.

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

This guide was first published on Dec 22, 2019. It was last updated on Mar 08, 2024.

This page (ADC) was last updated on Mar 08, 2024.

Text editor powered by tinymce.