🛠 Lire un potentiomètre avec l'ADC 12 bits
Objectives
Read a potentiometer on a real ADC input (GP26-GP28 only, as GP29 is diverted to the radio's VSYS/3 monitor), convert the raw value to a percentage, and understand the ~30 mV offset inherent to the board's filtered ADC supply.
Hardware
- Pico 2 W
- 10 kOhm potentiometer
- Breadboard and cables
- MicroPython >= v1.25.0 (RPI_PICO2_W firmware) or pico-sdk
Complete code
# main.py - MicroPython, potentiometre sur GP26 (ADC0)
import machine, time
pot = machine.ADC(26)
def lire_pct:
brut = pot.read_u16 >> 4 # 12 bits
# offset ~30 mV du a la filtre d'alim ADC (datasheet paragraphe 3.3)
offset = int(30.0 / 3300.0 * 4095)
if brut > offset:
brut -= offset
return brut * 100 // (4095 - offset)
while True:
print("Potentiometre :", lire_pct, "%")
time.sleep_ms(500)
In C, the same principle applies using adc_init, adc_gpio_init(26), adc_select_input(0), and adc_read.
Expected verification
When turning the potentiometer to its limits, the displayed value continuously sweeps from 0% to 100%. At rest, the reading is stable within a few units; the grounded channel returns a value close to zero after subtracting the offset.
Common errors
- Using GP29 as a free input: on the Pico 2 W, ADC3/GP29 is shared with the CYW43's SPI clock and only reads VSYS, and only when the radio is inactive.
- Exceeding 3.3 V (or ~3.6 V max) on GP26-GP28: the protection diode towards IOVDD can back-power a powered-off board.
- Ignoring the ~30 mV offset: 0% reads back as 30-40 units.
- Not shifting read_u16 by 4 bits: you think you are reading 16-bit values, but the actual resolution is 12 bits.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-interruption.pyPythonpwm-servo.pyPythonlecture-adc.pyPythonneopixel.pyPythonoled-i2c.pyPython