🛠 MicroPython sur Pico 2 : moniteur de tension VSYS
Objectives
Install RP2350-specific MicroPython on the Pico 2, use the Thonny REPL, and leverage Pico 2-specific system pins: GP29 = VSYS/3 for battery monitoring, GP23 = SMPS mode for cleaner ADC readings, GP24 = VBUS detection.
Hardware
- 1 × Raspberry Pi Pico 2
- Micro-USB data cable; Thonny ≥ 4.1 (or mpremote)
- Firmware:
RPI_PICO2UF2 from MicroPython ≥ v1.24.0 (supports RP2350 ARM and RISC-V) — an RP2040 build will refuse to install - Optional: Li-ion battery on VSYS (1.8–5.5 V accepted thanks to the RT6150 buck-boost)
Complete Code
# main.py — moniteur VSYS + LED d'alerte, Pico 2 / MicroPython >= 1.24
import time
from machine import Pin, ADC
led = Pin(25, Pin.OUT) # LED utilisateur GP25 (confirmee datasheet)
smps = Pin(23, Pin.OUT) # GP23 : niveau haut = SMPS en mode PWM
smps.value(1) # moins de ripple -> ADC plus stable
vsys = ADC(3) # ADC3 = VSYS/3 (GP29, diviseur interne)
vbus = Pin(24, Pin.IN) # GP24 : presence USB
def lire_vsys:
total = 0
for _ in range(16): # moyenne grossiere anti-bruit
total += vsys.read_u16
time.sleep_us(100)
return total / 16 / 65535 * 3.3 * 3 # 16 bits -> tension, x3 divider
while True:
v = lire_vsys
led.value(v < 3.6) # alerte batterie faible
print("VSYS = %.2f V USB = %d" % (v, vbus.value))
time.sleep_ms(500)
Expected Verification
In Thonny, the REPL displays values every 500 ms. When USB is connected, USB = 1 and VSYS ≈ 5 V; on a 3.7 V battery, you read ≈ 3.7 V and the LED remains off as long as V > 3.6 V.
Common Errors
- Flashing a standard "pico" UF2: RP2350 support is only available from MicroPython 1.24.0 (October 26, 2024); check for the RPI_PICO2 name.
- Forgetting the /3 factor of the internal VSYS divider: the displayed voltage is divided by three.
- GP29 has a decoupling diode to 3V3: never apply an external voltage to it; it is for internal measurement only.
- The SMPS power-saving mode (GP23 low) increases ripple on the ADC reference: keep GP23 high for stable measurements.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-interruption.pyPythonpwm-servo.pyPythonlecture-adc.pyPythonneopixel.pyPythonoled-i2c.pyPython