PyRasp

5. Measure a voltage

You will be able to

  • read a voltage with the Pico's ADC,
  • build a voltage divider,
  • convert a raw reading into volts or a percentage,
  • make a decision with a threshold and with hysteresis.

You need: two 10 kΩ resistors (a divider), or a potentiometer. Problems: Read a voltage, Night light, Sticky dimmer, Bar graph.

The idea

A digital pin reads only 0 or 1. An analogue-to-digital converter (ADC) measures in between.

The Pico's ADC is 12-bit: it turns 0–3.3 V into 0–4095. MicroPython's read_u16() gives a 0–65535 version of the same measurement (16-bit scaling), which is finer than the hardware.

A voltage divider: 3.3 V, then a 10 kilohm resistor R1, then the GP26 junction, then a 10 kilohm resistor R2 to ground. GP26 sits at 3.3 times R2 over R1 plus R2, which is 1.65 V, and the ADC reads about 32768.

Two equal resistors put half the supply in the middle: 1.65 V. The ADC should read about half its range.

Fun fact. 4096 steps over 3.3 V is about 0.8 mV per step — more than a hundred times finer than the 0.1 V you can read on a cheap multimeter.

The exact tools

WhatMicroPythonNotes
An ADC pinADC(26), ADC(27), ADC(28)Only GP26–28
16-bit readingadc.read_u16()0–65535
12-bit readingadc.read()0–4095
Voltsadc.read_u16() * 3.3 / 65535Compare like with like: use volts everywhere
Percentageround(adc.read_u16() * 100 / 65535)0–100

Build it

from machine import ADC
import utime

adc = ADC(26)
while True:
    volts = adc.read_u16() * 3.3 / 65535
    print(round(volts, 2))
    utime.sleep(0.5)

You should see about 1.65 with the equal divider.

Thresholds and hysteresis

A single threshold makes an output chatter when the input sits near it. Hysteresis uses two thresholds with a gap:

if reading > 0.7 * 65535:
    on = True       # clearly bright
elif reading < 0.3 * 65535:
    on = False      # clearly dark
# in between: keep what it was

Think about it. Why is the gap useful for a night light? (The light itself can change the sensor's reading; without a gap the lamp would blink on and off all night.)

What you should see

The printed voltage follows the divider. With a potentiometer, turning the shaft sweeps the number from 0 to about 3.3 V.


Common mistakes

What you seeWhyFix
ValueError on ADC(13)GP13 is not an ADC pinUse GP26, GP27 or GP28
The reading never changesThe wiper is not connected to the ADC pinWiper → GP26
The reading is noiseThe input is floatingAdd the divider or a pull
The reading is pegged at fullMore than 3.3 V on the pinCheck the divider; 5 V can damage an ADC pin

Check yourself

  1. How many volts is full scale on the ADC?
  2. What does an equal divider put on GP26?
  3. Why use 0.3 and 0.7 instead of one threshold?

Answers: 3.3 V · half of 3.3 V, about 1.65 V · the gap stops chatter near the threshold.

Remember

  • ADC is only on GP26, GP27, GP28; it measures 0–3.3 V.
  • read_u16 spans 0–65535; convert to volts before comparing.
  • Two thresholds with a gap = hysteresis.

Go further

Next: Lesson 6 — Dim and move