Raspberry Pi Pico - Blink LED

This guide shows you how to use the Raspberry Pi Pico to control an LED. It teaches you to write a MicroPython code that turns an LED on and off and makes it blink.

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

Pinout

LEDs have two pins.

  • Connect the Cathode (-) pin to GND (0V)
  • The Anode (+) pin controls the LED's state
LED Pinout

How It Works

After attaching the negative side (cathode) to the ground (GND):

  • If you connect the ground (GND) to the positive side (anode +) of the LED, the LED will not light up.
  • If you connect the power supply (VCC) to the positive side (anode +) of the LED, the LED will light up.
How LED Works

You can also change how bright the LED is by using a PWM signal at the anode (+). The way to adjust the LED brightness with the PWM value is shown in detail in this tutorial.

※ NOTE THAT:

LEDs usually need a resistor. You can attach the resistor to either the positive side, also called the anode, connected to the power supply or VCC, or the negative side, known as the cathode, connected to the ground or GND. The value of the resistor depends on the LED's specifications. Some LEDs already have a resistor built in. If your LED has one, you probably don't need another resistor.

Raspberry Pi Pico - LED

To control an LED with a Raspberry Pi Pico, first declare a pin as a digital output. This lets you manage the voltage as either GND (Ground) or VCC (Power). Connect the chosen Pico pin to the LED's positive (+) side with a resistor between them.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico LED

This image is created using Fritzing. Click to enlarge image

Real wiring diagram:

Raspberry Pico LED Blink

How To Program

  • Set the mode of Raspberry Pi Pico pin GP13 to digital output.
led = Pin(13, Pin.OUT)
  • Turn off the LED.
led.value(0) # Turn the LED off
  • Turn on the LED.
led.value(1) # Turn the LED on

Raspberry Pi Pico 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 """ from machine import Pin from time import sleep led = Pin(13, Pin.OUT) while True: led.value(1) # Turn the LED on sleep(1) # Wait for 1 second led.value(0) # Turn the LED off sleep(1) # Wait for 1 second

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 LED to the Raspberry Pi Pico 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).
  • 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.
  • Check out the LED's status.

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Code Explanation

The explanation can be found in the comments section of the above Raspberry Pi Pico code.

※ NOTE THAT:

The code mentioned uses sleep() which stops the Raspberry Pi Pico from performing other actions simultaneously. If your project needs to handle multiple tasks, do not use this function as it blocks the Pico. Rather, consider using a method that does not block the Raspberry Pi Pico.

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!