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

Raspberry Pi Pico · debutant · Schéma : IMG-pico-debutant-3-pico-adc-potentiometre.svg — potentiomètre relié à 3V3, GND et GP26 (ADC0) de la Pico, tension USB et console série affichant les valeurs

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials