🛠 Lire un potentiomètre avec l'ADC 12 bits

Raspberry Pi Pico 2 · debutant · Schéma : IMG-pico-2-debutant-3-pico2-adc-potentiometre.svg — potentiomètre relié à 3V3, GND et GP26 (entrée ADC) sur une Pico 2, curseur au centre

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials