🛠 Installer MicroPython et écrire main.py
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
- 1 × Raspberry Pi Pico + micro-USB data cable
- MicroPython rp2 firmware (
.uf2) from micropython.org/download - A file editor or a tool like Thonny (recommended for beginners: integrated REPL + editor)
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
- Flashing CircuitPython instead of MicroPython (or vice versa): both are flashed the same way but share neither API nor filesystem; keep only one.
- Saving in a subfolder:
main.pymust be at the root of the drive. - After a hard reset, pins "sink" ~60 µA, skewing ADC measurements: reconfigure them to high-Z before reading the ADC (documented quirk in the rp2 quick reference).
- REPL not responding: wrong serial port; the MicroPython REPL supports auto-baud rate — simply reopen the monitor.
Ctrl-Cineffective during a loop: send it multiple times, or reset usingmachine.reset.- Accidentally overwriting the firmware by dropping an old application
.uf2: re-flash the MicroPython firmware; the BOOTSEL method is always available (bootrom in ROM, impossible to brick).
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-interruption.pyPythonpwm-servo.pyPythonlecture-adc.pyPythonneopixel.pyPythonoled-i2c.pyPython