Raspberry Pi Pico - Blink LED Without Sleep

In this tutorial, we will learn how to make the Raspberry Pi Pico blink an LED in non-blocking method without using time.sleep() function. In detail, we will learn:

Raspberry Pi Pico Blink LED

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LED

Explore how LEDs works, their pinout, and programming methods in these tutorials.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico LED

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - Blinking LED without sleep

The below MicroPython script blinks a LED with frequency: 250ms ON, 750ms OFF, repectedly forever, without blocking other code.

""" 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()

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the LED according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • On Thonny IDE, Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-LED”, then find the LED library created by DIYables.
  • Click on DIYables-MicroPython-LED, then click Install button to install LED library.
Raspberry Pi Pico LED library
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Watch the LED: LED will blink forever.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!