🛠 Installer MicroPython et écrire main.py

Raspberry Pi Pico · intermediaire · Schéma : IMG-pico-intermediaire-3-pico-micropython.svg — Pico en mode BOOTSEL apparaissant comme lecteur RPI-RP2, fichier firmware uf2 déposé dessus, puis fenêtre REPL série

Objectives

Install MicroPython (official rp2 port) on the Pico and master the complete workflow: flashing the .uf2 firmware, using the REPL console over the USB serial port, and editing and saving main.py, which starts automatically upon power-up. MicroPython fully leverages RP2040 specifics: PWM (16 channels, duty_u16 range 0-65535), ADC, and PIO via the rp2 module.

Hardware

Complete Code

# main.py — à sauvegarder à la racine du lecteur RPI-RP2 (alias « Pico »)
# respiration LED GP25 + bouton GP2 qui force l'extinction
from machine import Pin, PWM
import time

led = PWM(Pin(25))          # LED embarquée, tranche PWM 12B
led.freq(1000)              # fréquence de la tranche
btn = Pin(2, Pin.IN, Pin.PULL_UP)   # bouton GP2 -> GND

while True:
    for duty in range(0, 65536, 512):     # rampe montante
        led.duty_u16(duty)
        if not btn.value:               # appui => extinction
            led.duty_u16(0)
        time.sleep_ms(5)
    for duty in range(65535, -1, -512):   # rampe descendante
        led.duty_u16(duty)
        time.sleep_ms(5)

Expected Verification

After flashing the firmware, the LED breathes in a loop as soon as the Pico is powered, without needing a PC nearby — main.py executes at boot. Pressing the button turns off the LED during the ramp. In Thonny/REPL, help('rp2') lists the specific classes (StateMachine, PIO): the port is correctly installed.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials