DEV Community

Cover image for Raspberry Pi Pico - Blink LED Without Sleep
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Raspberry Pi Pico - Blink LED Without Sleep

This tutorial blinks an LED on the Raspberry Pi Pico without ever calling time.sleep(). If you've only ever blinked an LED by sleeping between on and off, this shows the non-blocking pattern you'll need once your code has to do more than one thing at a time, like checking a button or reading a sensor while the LED keeps flashing on its own schedule.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Raspberry Pi Pico - Blink LED Without Sleep

What you need

Wiring

Raspberry Pi Pico - Blink LED Without Sleep wiring diagram

Connect the LED to GPIO 13 on the Pico. If you use a plain LED, wire it through a 220Ω resistor on a breadboard to limit current. The LED Module is recommended instead, since it has the resistor built in, so you just run one wire from the module's signal pin to GPIO 13 plus power and ground, no extra resistor or breadboard needed.

How it works

time.sleep() pauses your whole program, so nothing else can run while the LED is mid-blink. This code avoids that by tracking time instead of blocking: it remembers when the LED last changed state and checks, on every pass through the main loop, whether enough time has passed to flip it again. That means you can add other tasks inside the same loop and they'll keep running while the LED blinks in the background.

Code

The script uses the DIYables_MicroPython_LED library's LED class, created here on pin 13 with CTRL_ANODE since the LED module is wired anode-controlled. Calling led.blink(250, 750) sets it blinking 250ms on, 750ms off, immediately. You must call led.loop() on every pass of the main loop, that's what actually advances the timing. led.get_state() reports LED_BLINKING or LED_IDLE, which the example uses to print a message only when the state changes. Other examples in the same pattern blink for a fixed duration or a fixed number of times instead of forever.

raspberry-pi-pico-blink-led-without-sleep.py

"""
This Raspberry Pi Pico MicroPython code was developed by newbiely.com
This Raspberry Pi Pico code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-blink-led-without-sleep
"""

from DIYables_MicroPython_LED import LED, LED_BLINKING, LED_IDLE, CTRL_ANODE

# Create an LED object that attaches to pin 13
led = LED(13, CTRL_ANODE)

led.blink(250, 750)  # 250ms ON, 750ms OFF, blink immediately
# led.blink(250, 750, 1000)  # Uncomment to blink after 1 second delay

# Function to print the state of the LED
def print_state(state):
    if state == LED_BLINKING:
        print("BLINKING")
    elif state == LED_IDLE:
        print("BLINK ENDED")

# Track the previous state of the LED
previous_state = None

# Main loop
while True:
    led.loop()  # MUST call the led.loop() function in loop

    # Get the current state of the LED
    current_state = led.get_state()

    # Print the state only if it has changed
    if current_state != previous_state:
        print_state(current_state)
        previous_state = current_state  # Update the previous state

    # DO OTHER TASKS HERE
    # To stop blinking immediately, call led.cancel()
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

If the LED never blinks, check that you're calling led.loop() inside your while True loop, the library can't update the LED without it. If the LED stays on or off instead of blinking, double check CTRL_ANODE matches how your LED or module is wired, the wrong control mode inverts the logic. And if nothing happens at all, confirm the Pico shows up as the MicroPython interpreter in Thonny before you hit run.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-blink-led-without-sleep

Top comments (0)