clignotante.py

Raspberry Pi Pico W · File : clignotante.py · Language : Python · Use case : Faire clignoter une LED avec MicroPython

The first MicroPython program: the LED blinks once per second with serial console tracing. It shows Pin as output, value() and time.sleep, the basis of every embedded script on the board.

Hardware

A Raspberry Pi Pico W (RP2040 + 2.4 GHz Wi-Fi radio chip) with MicroPython installed, powered over USB. No external part (external LED + 330 Ω resistor only if the user LED is unavailable).

Wiring

External LED anode to GP15 through a 330 Ω resistor, cathode to GND. The user LED is driven by the radio chip, not a GPIO.

How it works

Pin(LED_PIN, Pin.OUT) sets the pin as output. The infinite loop alternates value(1)/value(0) with a one-second pause; print() goes to the USB serial (Thonny).

Running it

Install the « Pico W » MicroPython build (BOOTSEL + .uf2), copy the script via Thonny. Since the user LED sits behind the radio chip, output examples use an external LED.

Variants

Save as main.py for auto-start, or replace sleep(1) with sleep_ms(100) for fast blinking.

clignotante.py

# Clignotement d'une LED avec MicroPython (module machine.Pin).
# LED externe sur GP15 (la LED utilisateur est portee par la puce radio)
from machine import Pin
import time

LED_PIN = 15

led = Pin(LED_PIN, Pin.OUT)   # broche en sortie numerique

print("Clignotement demarre sur GP" + str(LED_PIN))
compteur = 0

while True:
    led.value(1)              # LED allumee
    compteur += 1
    print("LED ON  (cycle %d)" % compteur)
    time.sleep(1)             # 1 seconde

    led.value(0)              # LED eteinte
    print("LED OFF")
    time.sleep(1)

# Pour arreter : bouton Stop dans Thonny (le script tourne aussi
# tout seul s'il est enregistre sous main.py sur la carte).

← Back to the board