lecture-adc.py

Raspberry Pi Pico 2 · File : lecture-adc.py · Language : Python · Use case : Lire une entrée analogique et la température interne du circuit

Two 12-bit analog readings in one script: an external voltage on GP26 (ADC0) and the chip's built-in thermal sensor converted to degrees using datasheet parameters. Great for watching CPU load.

Hardware

A Raspberry Pi Pico 2 (RP2350 dual Cortex-M33 @ 150 MHz) with MicroPython installed, powered over USB. For the external channel: 10 kΩ potentiometer (wiper to GP26, ends to 3V3 and GND). The internal sensor needs nothing.

Wiring

Potentiometer wiper to GP26, one end to 3V3, the other to GND. Never exceed 3.3 V on an ADC pin.

How it works

ADC(Pin) or ADC(4) opens a channel; read_u16() returns 0-65535 scaled to the 3.3 V reference. The internal temperature uses the typical calibration 0.706 V at 27 °C, slope -1.721 mV/K.

Running it

Install the « Pico 2 » MicroPython build (hold BOOTSEL, plug USB, drop the .uf2), then copy the script via Thonny. Auto-starts when named main.py.

Variants

Average several readings to cut noise, or use adc.init(sample_bits=8) to trade precision for speed.

lecture-adc.py

# ADC 12 bits : entree externe GP26 (ADC0) + capteur thermique interne.
from machine import ADC, Pin
import time

# CAN 0-3 = GP26..GP29 ; le canal 4 est le capteur de temperature.
adc_externe = ADC(Pin(26))
adc_temp = ADC(4)

REFERENCE = 3.3  # tension de reference de l'ADC

def tension(adc):
    # read_u16 renvoie 0..65535 ; l'ADC interne a 12 bits.
    return adc.read_u16() * REFERENCE / 65535.0

def temperature_interne():
    # Doc technique RP2040/RP2350 : 0,706 V a 27 degC, pente -1,721 mV/K.
    v = tension(adc_temp)
    return 27.0 - (v - 0.706) / 0.001721

print("Mesures toutes les secondes (Ctrl-C pour arreter)")
while True:
    u = tension(adc_externe)
    t = temperature_interne()
    print("GP26 = %.2f V | temperature interne = %.1f C" % (u, t))
    time.sleep(1)

← Back to the board