MicroPython examples · MicroPython and GPIO

Blink the Pico 2 W LED with MicroPython

Run a continuous LED blink program on a Raspberry Pi Pico 2 W, then use its short source to understand GPIO output and blocking delays. The onboard LED changes state once per second.

Prerequisites and installation

Use a Raspberry Pi Pico 2 W with matching MicroPython firmware, a USB data cable and a MicroPython editor or REPL tool. The machine module belongs to the board runtime; ordinary desktop Python cannot run this script as written.

For firmware setup, follow the official RP2 getting-started guide. No external wiring or network connection is used by this program.

Program

The following matches main.py:

from machine import Pin
from time import sleep

led = Pin("LED", Pin.OUT)

while True:
    led.toggle()
    sleep(1)

How it works

  1. Pin("LED", Pin.OUT) selects the board’s named onboard LED and configures it as an output. The program does not explicitly set an initial high/low value before the first toggle.
  2. led.toggle() changes the current LED state.
  3. sleep(1) waits one second before the next state change.
  4. The while True loop repeats until execution is stopped.

The LED changes state once per second; a complete on/off cycle takes about two seconds. No network connection is used.

Run it on the board

Install the matching MicroPython firmware for your Pico 2 W, connect through a compatible editor or MicroPython REPL, and run the source program. Saving it as main.py on the board lets MicroPython run it at startup.

Interrupt execution from the REPL before editing or replacing a continuously running startup script.

Verification

Expected board behavior is one LED state change per second. Verify both visible toggling and the ability to stop execution. The code block was compared with the source during documentation review; physical hardware execution was not performed.

Troubleshooting and limitations

Symptom Check
No module named machine Run on MicroPython on the board, not desktop Python.
The named pin is unavailable Confirm the board and firmware expose the LED alias.
The script restarts on reconnect Check whether it was saved as the board’s startup main.py.

The script has no options, Wi-Fi initialization or shutdown handler. Its one-second delay is intended for a visual learning example rather than precision timing.

See the official RP2 quick reference.


Repository overview and license · Documentation index