lecture-adc.py

RP2040 (generic board / chip) · 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 generic RP2040 (dual Cortex-M0+) board programmed with MicroPython. 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

Flash MicroPython via BOOTSEL (.uf2 file), then copy the script to the board drive. Check your board's user LED pin (often GP25) and adapt if needed.

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