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

Raspberry Pi Pico 2 W · debutant · Schéma : IMG-pico-2-w-debutant-3-pico2w-adc-potentiometre.svg — potentiomètre relié à GP26, 3V3 et GND, convertisseur ADC 12 bits du RP2350 et offset d'environ 30 mV annoté.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials