Interfacing LED to Raspberry Pi Pico and controlling it Using MicroPython

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.

fig-Connection Diagram
Code
Code Explanation
Imports
- Importing
Pinclass from machine module to control the GPIO pins. timemodule for creating delay betweenonandoffstate.
Accessing LED's.
- GPIO pin 3 is configured as
OUTPUTpin (Line number 6) and assigned to a variableled_1 - GPIO pin 12 is configured as
OUTPUTpin (Line number 9) and assigned to a variableled_2
Controlling LED
- Continuous loop for blinking led is achieved using
while True: led_1.value(0)sets the value of GPIO 3 toLOWor0, it turns ON the LED-1. (line number 13)led_2.value(1)sets the value of GPIO 12 toHIGHor1, 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 toHIGHor1, it turns OFF the LED-1. (line number 19)led_2.value(1)sets the value of GPIO 12 toLOWor0, it turns OFF the LED-2. (line number 20)
Try It
- Change the value in the
sleep_msmethod and observe the change in the on and off time.time.sleep_ms(2000),time.sleep_ms(750), etc
- Use
led_1.on()andled_1.off()method instead ofled_1.valuefunction, 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
- Raspberry Pi Pico / Pico 2 : Pin Diagram
- Raspberry Pi Pico : Data Sheet
- Raspberry Pi Pico 2 : Data Sheet
- Raspberry Pi Pico W : Data Sheet
- Raspberry Pi Pico 2 W : Data Sheet
Modules / Libraries Used
-
machine
machinemodule contains specific attributes and methods related to hardware on a particular board. Here classPinis imported to control the Input Output pins.- More Details
-
time
timemodule provides functions related to date & time, measuring time intervals and generating delays.- More Details