🛠 MicroPython sur Pico 2 : moniteur de tension VSYS

Raspberry Pi Pico 2 · intermediaire · Schéma : IMG-pico-2-intermediaire-3-pico2-micropython-vsys.svg — Pico 2 alimentée par une batterie sur VSYS, tension mesurée en interne par ADC3 (GP29, diviseur /3), REPL série affiché à l'écran

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials