Pico: blink the onboard LED with MicroPython (the 5-minute start)
The shortest possible Pico + MicroPython tutorial. Blink the LED. Get to a working project before you know all the theory.
If you have a Pico and you want to see it do something right now, this is the tutorial. It assumes you already have MicroPython installed (covered in the previous tutorial). If not, go read that one first.
The code
MicroPython (Pico)
from machine import Pin
import time
led = Pin("LED", Pin.OUT)
while True:
led.toggle()
time.sleep(0.5)
Click the green “Run” button. The onboard LED blinks at 1 Hz. Click the red “Stop” button to stop.
Arduino (Pico)
Board package: Raspberry Pi Pico/RP2040 by Earle Philhower. Pin
numbers in the Pico Arduino core map directly to the GPIO number, so
LED_BUILTIN works on the Pico the same way it works on the Uno.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
For an external LED on a GPIO pin, the wiring and code are the same as the Arduino blink tutorial, just with a different pin number:
#define LED_PIN 0
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}
Saving to flash
The script above only runs while Thonny is connected. To save it to the Pico’s flash:
File>>Save as- Save to “MicroPython device” as
main.py
Now the script runs whenever the Pico boots, with no computer attached.
Controlling the LED from the REPL
While the script is running, you cannot use the REPL. Press Ctrl+C in
the Shell pane to interrupt. Now you can:
from machine import Pin
led = Pin("LED", Pin.OUT)
led.on()
led.off()
led.toggle()
The Pin("LED") constant works on both the Pico and the Pico W. On the
original Pico, the LED is on pin 25; on the Pico W, it is on the Wi-Fi
chip’s GPIO (accessed via Pin("LED")).
Making it fade with PWM
The Pico has 8 PWM slices, each with two channels (16 PWM outputs total).
Pin 25 (the onboard LED on the original Pico) is on PWM slice 0, channel
0 (it’s PWM capable). On the Pico W, the onboard LED is not on a PWM-capable
pin directly; you need to use PWM("LED") or the PWM(Pin("LED")) form.
from machine import Pin, PWM
import time
pwm = PWM(Pin("LED"))
pwm.freq(1000)
while True:
for duty in range(0, 65536, 1000):
pwm.duty_u16(duty)
time.sleep_ms(5)
for duty in range(65535, -1, -1000):
pwm.duty_u16(duty)
time.sleep_ms(5)
The LED fades up and down smoothly.
The differences between Pico and Pico W onboard LEDs
This trips up a lot of people.
- Original Pico: onboard LED on GPIO 25 (PWM-capable).
- Pico W: onboard LED on the Wi-Fi chip, accessed via
Pin("LED")orPWM(Pin("LED")). PWM works but requires a special form because the LED is not on the main GPIO.
If your script works on the original Pico but not the Pico W, this is usually why.
Multiple LEDs
Wire external LEDs to any GPIO pin:
from machine import Pin
import time
red = Pin(0, Pin.OUT)
green = Pin(1, Pin.OUT)
blue = Pin(2, Pin.OUT)
while True:
red.toggle()
time.sleep_ms(500)
green.toggle()
time.sleep_ms(500)
blue.toggle()
time.sleep_ms(500)
Add a 220 ohm resistor in series with each LED to limit current.
Why Pin.toggle() and not Pin.value(not led.value())
toggle() is one operation; it is faster and atomic. value(not led.value()) reads, negates, and writes, which has a tiny window where
another interrupt could change the pin.
For LED blinking, neither matters. For GPIO-banging a custom protocol, toggle is the right call.
What to build next
- A button-controlled LED (combines with the button tutorial).
- A “police light” pattern (alternating red and blue).
- A “breathing” LED using PWM (the fade above, in a more polished form).
The “breathing” pattern is in the book Pico Fun Projects. The button version is one of the next tutorials on this site.