Skip to content

Interfacing LED to Raspberry Pi Pico and controlling it Using MicroPython

blink led

Table of Contents

Abstract

In this article, a comprehensive step-by-step guide to interface an external LED with Raspberry Pi Pico board using MicroPython. Raspberry Pi Pico has 4 different variants (Pico, Pico 2, Pico W, Pico 2W) supporting micro-python. This articles lays the foundation for more advanced embedded system and IoT projects.

🧭 Pre-Request

  • OS : Windows / Linux / Mac / Chrome
  • Thonny IDE.
  • MicroPython firmware in Raspberry Pi Pico / Pico 2 / Pico W / Pico 2W.
    • For step by step procedure Click here .

Hardware Required

  • Raspberry Pi Pico / Pico 2 / Pico W / Pico 2W.
  • LED.
  • Resistors.
  • BreadBoard.
  • Micro USB Cable.
  • Connecting wires.
  • 5V DC power supply (Optional)
Components Purchase Link
Raspberry Pi Pico link
Raspberry Pi Pico 2 link
Raspberry Pi Pico W link
Raspberry Pi Pico 2W link
LED link
BreadBoard large : small
Connecting Wires link
Micro USB Cable link
5V DC Adaptor link

Don't own a hardware 😢

No worries,

Still you can learn using simulation. check out simulation part 😃.

Connection Table

Particular GPIO Remarks
LED 1 3 LED-1 Cathode to GPIO 3 (Pin 5) : Low Logic
LED 2 12 LED-2 Anode to GPIO 12 (Pin 16) : High Logic

Info

  • LED-1 : Low Logic LED : ON for GPIO set to 0 : OFF for GPIO set to 1
  • LED-2 : HIGH Logic LED : ON for GPIO set to 1 : OFF for GPIO set to 0
  • Best practice is to connect LED in low logic.

Circuit Diagram

fig-Connection Diagram

📂 Code

from machine import Pin
import time


# LED 1 connected to GPIO 3
led_1 = Pin(3, Pin.OUT)

# LED 2 connected to GPIO 12
led_2 = Pin(12, Pin.OUT)

while True:
  # Turning ON LED
  led_1.value(0) 
  led_2.value(1) 

  time.sleep_ms(1000)

  # Turning OFF LED
  led_1.value(1) 
  led_2.value(0) 

  time.sleep_ms(500)

Code Explanation

👉 Imports

from machine import Pin
import time
  • Importing Pin class from machine module to control the GPIO pins.
  • time module for creating delay between on and off state.

👉 Accessing LED's.

5
6
7
8
9
# LED 1 connected to GPIO 3
led_1 = Pin(3, Pin.OUT)

# LED 2 connected to GPIO 12
led_2 = Pin(12, Pin.OUT)
  • GPIO pin 3 is configured as OUTPUT pin (Line number 6) and assigned to a variable led_1
  • GPIO pin 12 is configured as OUTPUT pin (Line number 9) and assigned to a variable led_2

👉 Controlling LED

while True:
  # Turning ON LED
  led_1.value(0) 
  led_2.value(1) 

  time.sleep_ms(1000)

  # Turning OFF LED
  led_1.value(1) 
  led_2.value(0) 

  time.sleep_ms(500)
  • Continuous loop for blinking led is achieved using while True:
  • led_1.value(0) sets the value of GPIO 3 to LOW or 0, it turns ON the LED-1. (line number 13)
  • led_2.value(1) sets the value of GPIO 12 to HIGH or 1, it turns ON the LED-2. (line number 14)
  • Delay is achieved through time.sleep_ms() method. (line number 16 & 22)
  • led_1.value(1) sets the value of GPIO 3 to HIGH or 1, it turns OFF the LED-1. (line number 19)
  • led_2.value(1) sets the value of GPIO 12 to LOW or 0, it turns OFF the LED-2. (line number 20)

Try It

  • Change the value in the sleep_ms method and observe the change in the on and off time.
    • time.sleep_ms(2000), time.sleep_ms(750) , etc
  • Use led_1.on() and led_1.off() method instead of led_1.value function, and observe the changes.

Simulation

Not able to view the simulation

  • Desktop or Laptop : Reload this page ( Ctrl+R )
  • Mobile : Use Landscape Mode and reload the page

Extras

Components details

Modules / Libraries Used

  • machine

    • machine module contains specific attributes and methods related to hardware on a particular board. Here class Pin is imported to control the Input Output pins.
    • More Details
  • time

    • time module provides functions related to date & time, measuring time intervals and generating delays.
    • More Details