Skip to content

Blink an LED on Raspberry Pi Pico Using MicroPython (Hardware & Simulation) : Step-by-Step Guide

blink led

Table of Contents

Abstract

Raspberry Pi Pico is a modern development board with Cortex-M0+, Dual core microcontroller supporting micro-python. Know the key features and comparison with Pico 2 version.

In this article we go through.

  • Accessing the built-in led of the Raspberry Pi Pico board.
  • Turning on and off built-in led using micro python.
  • Simulation using wokwi.
  • Hardware demo.

🧭 Pre-Request

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

Hardware Required

  • Raspberry Pi Pico / Pico 2.
  • Micro USB Cable.
Components Purchase Link
Raspberry Pi Pico Purchase Link
Raspberry Pi Pico 2 link
Micro USB Cable Purchase Link

Don't own a hardware 😢

No worries,

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

Connection Table

Built-in led of Raspberry Pi Pico is internally connected to GPIO 25.

Particular GPIO Remarks
Built-in LED 25 Internally Connected

Built-in LED

📂 Code

from machine import Pin
import time

# Built-in LED is connected to GPIO 25
led = Pin(25, Pin.OUT)

while True:
    led.on()
    time.sleep(1)
    led.off()
    time.sleep(1)

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 built-in led of Raspberry Pi Pico / Pico 2 board.

# Built-in LED is connected to GPIO 25
led = Pin(25, Pin.OUT)
  • GPIO pin 25 is configured as OUTPUT pin (Line number 5) and assigned to a variable led

👉 Blinking LED

while True:
    led.on()
    time.sleep(1)
    led.off()
    time.sleep(1)
  • Continuous loop is achieved using while True:
  • led.on() sets the value of GPIO 25 to HIGH or 1, which in turn turns ON the LED. (line number 8)
  • Delay of 1 second is achieved through time.sleep(1) method. (line number 9 & 11)
  • led.off() sets the value of GPIO 25 to LOW or 0, which in turn turns OFF the LED. (line number 10)

Try It

  • Change the value in the sleep method and observe the change in the on and off time.
    • time.sleep(2), time.sleep(4) , etc
  • Use time.sleep_ms() method to set the values in terms of milli seconds.
    • time.sleep_ms(1000) : 1000 ms or 1 s
    • time.sleep_ms(500) : 500 ms or 0.5 s

Simulation

Not able to view the simulation

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

Result

Above code turns ON LED for 1 second and OFF for 1 second. Similar to a square wave of 0.5 Hz output connected to an LED.


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