MicroPython on a Pico: read a potentiometer on GP26
MicroPython on a Raspberry Pi Pico: machine.ADC reads a potentiometer’s wiper on GP26 and prints the reading and its voltage ten times a second. The whole build, a Raspberry Pi Pico and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $4.50. On Mokxi they're free.
What you'll learn
- Reading an analog voltage with machine.ADC
- How a potentiometer makes a variable voltage
- Converting a raw reading into volts
- Formatting numbers in a print with %
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Raspberry Pi Pico
- Parts: 3
How it works
A potentiometer is a knob that slides along a resistor. With its ends on 3.3 V and ground, the middle pin (the wiper) gives anything from 0 V to 3.3 V as you turn. The wiper goes to GP26, which is ADC0.
Ten times a second, the script reads the pin with read_u16, which gives 0 at 0 V and 65535 at 3.3 V. It then turns that into volts and prints both numbers.
Parts list
3 parts, plus the jumper wires. They retail for about $4.50; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
- 1 × Power, 3.3 V
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Potentiometer pin 2 | Raspberry Pi Pico pin GP26 |
| Potentiometer pin 1 | Ground |
| Potentiometer pin 3 | 3.3 V |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Read the knob: the potentiometer's wiper is on GP26, which is ADC0.
# read_u16() is 0 at 0 V and 65535 at 3.3 V.
from machine import ADC
import time
knob = ADC(26)
while True:
value = knob.read_u16()
volts = value * 3.3 / 65535
print("ADC %5d %.2f V" % (value, volts))
time.sleep_ms(100)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The reading sticks at 0 or the maximum This project
Check that the wiper (middle pin) goes to GP26 and the outer pins go to 3.3 V and ground. - The knob works backward This project
Swap the two outer pins, 3.3 V and ground. - The reading jumps around or only covers part of the range. Potentiometer
Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range. - Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise. - The Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it. - A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.