🛠 Lire un potentiomètre avec l'ADC 12 bits
Objectives
Use the Pico's analog-to-digital converter: a 12-bit SAR, 4 channels output at 500 ksps (GP26 = ADC0, GP27 = ADC1, GP28 = ADC2; ADC3 = VSYS/3 and ADC4 = internal temperature sensor). You will read a potentiometer and display the values on the USB serial console. Note: the Pico has no DAC — the ADC is the only analog path, read-only.
Hardware
- 1 × Raspberry Pi Pico
- 1 × potentiometer (e.g., 10 kΩ)
- Cables: one end of the potentiometer to 3V3, the other to GND, the wiper (middle pin) to GP26
- Serial console at 115200 baud (PuTTY,
picocom, etc.) on the Pico's USB port
Complete code
// adc_pot.c — ADC0 (GP26), moyenne sur 16 échantillons
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
int main(void) {
stdio_init_all; // console sur USB (stdio_usb)
adc_init;
adc_gpio_init(26); // GP26 en mode ADC
adc_select_input(0); // canal ADC0
while (true) {
uint32_t sum = 0;
for (int i = 0; i < 16; i++) sum += adc_read;
uint16_t raw = sum / 16;
float mv = raw * 3300.0f / 4095.0f;
printf("raw=%u ~%.0f mV\n", raw, mv);
sleep_ms(200);
}
}
Expected verification
Turning the potentiometer, the values scroll from ~0 to ~4095 (12 bits) and the calculated voltage follows 0 → 3.3 V. The values may be slightly noisy: this is normal, the Pico's ADC uses its own filtered 3.3 V supply as a reference, with an inherent offset of approximately 30 mV documented in the datasheet.
Common errors
- Forgetting
adc_gpio_init(26): the pin remains in GPIO mode, the reading is incorrect or zero. - Wrong channel:
adc_select_input(3)reads VSYS/3,4reads the temperature — not your potentiometer! - Measuring more than 3.3 V: the ADC cannot handle more than its supply voltage; and GP26-GP29 have a diode to IOVDD (beyond ~3.6 V it conducts, and powering a pin on an unpowered board powers the entire 3V3 rail).
- Expecting precision: for noise, average multiple samples; for accuracy, subtract the offset (reading a grounded channel) or force the regulator into PWM mode (GPIO23).
stdio_init_allover UART in older examples: connect to the correct port (USB for stdio_usb).
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-interruption.pyPythonpwm-servo.pyPythonlecture-adc.pyPythonneopixel.pyPythonoled-i2c.pyPython