🛠 Lire un potentiomètre avec l'ADC 12 bits
Objectives
Use the RP2350's 12-bit / 500 kSPS SAR analog-to-digital converter to read a potentiometer, convert the value to voltage, and understand the specifics of the ADC reference on the Pico 2 (filtered 3.3 V SMPS rail, inherent offset of approximately 30 mV).
Hardware
- 1 × Raspberry Pi Pico 2
- 1 × 10 kΩ potentiometer (or 4.7 kΩ)
- Pico SDK ≥ 2.0.0, serial terminal at 115200 bauds on UART0 (GP0/GP1)
- Wiring: potentiometer between 3V3 and GND, wiper connected to GP26
Complete Code
/* adc_pot.c — lecture potentiomètre sur GP26, affichage tension */
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include <stdio.h>
#define POT_ADC_PIN 26
#define SMPS_MODE_PIN 23 /* GP23 haut = mode PWM SMPS, ripple réduit */
int main(void) {
stdio_init_all;
/* Améliore la stabilité de la référence ADC (conseil datasheet §5.3) */
gpio_set_function(SMPS_MODE_PIN, GPIO_FUNC_SIO);
gpio_set_dir(SMPS_MODE_PIN, GPIO_OUT);
gpio_put(SMPS_MODE_PIN, 1);
adc_init;
adc_gpio_init(POT_ADC_PIN);
while (true) {
adc_select_input(0); /* canal ADC0 (GP26) */
uint16_t raw = adc_read; /* 0..4095 (12 bits) */
float v = raw * 3.3f / 4096.0f;
printf("raw=%4u U=%.3f V\n", raw, v);
sleep_ms(200);
}
}
Expected Verification
When turning the wiper, the raw value follows 0 → 4095 and the displayed voltage follows 0 → ~3.3 V. At the low end, you may read a few tens of mV instead of 0: this is the documented offset (~30 mV) related to the ADC's own consumption (~150 µA) on the filtered 200 Ω/2.2 µF reference.
Common Errors
- Forgetting
adc_gpio_init: the pin remains in its default function and the reading is incorrect or zero. - Expecting absolute precision: the reference is the SMPS rail; for fine measurements, enforce PWM mode via GP23, perform a zeroing on a channel connected to GND, or add an external LM4040 reference on ADC_VREF.
- Caution GP26–GP29: decoupling diode towards the 3V3 rail, never exceed 3.3 V + ~300 mV nor inject voltage when the board is powered off (back-feeding the rail).
- Confusing the 12-bit scale (4095) of the C SDK with the 16-bit scale (65535) of MicroPython.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-interruption.pyPythonpwm-servo.pyPythonlecture-adc.pyPythonneopixel.pyPythonoled-i2c.pyPython