Site icon High Voltages

LED with Raspberry pi pico | Hello world

LED with Raspberry pi pico
LED with Raspberry pi pico

This tutorial will help you interface LED with raspberry pi pico. Since it is the beginners level project, therefore we can call it Raspberry pi pico Hello world. Raspberry pi pico is the new addition to the Raspberry pi series, but unlike other Pi boards which are microprocessors, it is a microcontroller and it is one of the beginners friendly board. Also, pi pico is capable of pro progamming of Input/outputs (IOs) i.e. you can emulate many interfaces. The pico board is capable of running MicroPython, CircuitPython, Arduino language and Its SDK.

In this tutorial, you will learn how to interface and blink LED with Raspberry pi pico using Arduino IDE, Micropython, and CircuitPython.

LED with Raspberry pi Circuit

Components Required

ComponentAmazon (US)Aliexpress
Raspberry pi picoLED with Raspberry pi pico | Hello worldLED with Raspberry pi pico | Hello world
LEDLED with Raspberry pi pico | Hello worldLED with Raspberry pi pico | Hello world
ResistorLED with Raspberry pi pico | Hello worldLED with Raspberry pi pico | Hello world
BreadboardLED with Raspberry pi pico | Hello worldLED with Raspberry pi pico | Hello world
Jumper wiresLED with Raspberry pi pico | Hello worldLED with Raspberry pi pico | Hello world
Micro USB cableLED with Raspberry pi pico | Hello worldLED with Raspberry pi pico | Hello world
LED with Raspberry pi pico Components required

Disclosure: Some of these links are affiliate links. We mayy earn commission on you purchase at no extra cost to you. Support us by buying from here .

LED Pinout and working

In one of LED with ESP32 tutorial, I mentioned the working of LED. But in short, LED has two pins, Cathode (-) and anode (+).

LED with Raspberry pi pico | Hello world
LED Pinout
Credits: tweaking4all

Cathode (-) will be connected to Ground (0V) and anode (+) is connected to any digital pin of Pi pico so we can control the state of LED by giving HIGH (3.3V) or LOW (0V).

Pi Pico and LED Wiring Diagram

LED with Pi pico Circuit diagram
LED with Pi pico Circuit diagram

The wiring diagram shows that we have connected the cathode of LED with the resistor and resistor is connected to ground, where as the anode is connected to GP5 of Pi pico.

Why we are using resistor with LED ?

We can prevent the LED from burning by using resistors. You can connect a resistor to any LED pin, either with the cathode (-) or the anode (+), depending on the resistor’s value and the LED’s specifications. You could omit the resistor if your LED has a built-in resistor for protection.

LED with Raspberry Pi pico Code

As we have discussed in the introduction, we can program raspberry pi pico in multiple ides and programming languages. In this tutorial, we will be learning how to use LED with pi pico in arduino IDE, microPython and circuitPython.

LED with Pi Pico Arduino IDE

First thing, you need to install the support of RP2040 in Arduino IDE.

We can write code once the Arduino IDE has been configured to program the Raspberry pi pico. We’ll write the LED’s Pin number first. In the above circuit, we connected the LED with GP5 so we can write,

#define LED_PIN 5

or you can also use the builtin LED, by typing.

#define LED_PIN LED_BUILTIN

The setup function, which is mainly for defining and setting up the pins or other parameters, will be written after the pin has been defined. It will only be executed once.

void setup() {
  // put your setup code here, to run once:
  pinMode(LED_PIN,OUTPUT);
}

The code snippet above indicates that our LED, which is attached to pin GP5, is an output, and that we can use the following function to control the LED’s state.

digitalWrite(LED_PIN,HIGH);

To control your digital outputs, use the digitalWrite(pinNum, state) function. In the pin number field, enter the pin number, and in the state field, enter “High” for 3.3V or “LOW” for 0V. In order to make LEDs blinking, we also need the delay function, which will enable us to turn LEDs on for a period of time before turning it off again. The following is the way to use the delay function:

  delay(500);

In the delay function, we pass milliseconds of time. The function’s value 500 denotes a 0.5-second delay (1 second is equal to 1000 milliseconds).

LED blink with Pi Pico in Arduino

#define LED_PIN 5

void setup() {
  // put your setup code here, to run once:
  pinMode(LED_PIN,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(LED_PIN,HIGH);
  delay(500);
  digitalWrite(LED_PIN,LOW);
  delay(500);
}

The LED will blink with a frequency of 0.5 seconds using the code above. You can experiment with the delay values to alter the frequency, change the pin number to any digital pin, or use an builtin LED with the ESP32.

Congratulations! You’ve completed your first Pi Pico program and said hello world using pi pico.

LED with Pi Pico Micropython

First thing install the micropython support for Pi pico.

Importing two libraries to access the delay function (time library) and GPIO pins (machine library) comes first after configuring the micropython support.

import time
from machine import Pin

In the aforementioned snippet, all of the time library’s functions are imported, but only the pin function from the machine library is imported.

Next, the LED pin is defined as an output.

LED_PIN = Pin(5,Pin.OUT)

The first input to the pin function is the pin number, and the second argument is what we define as output.

Then we’ll use the snippet below to make

while(True):

and by using the following snippet, we can change the LED’s status between HIGH and LOW. HIGH is defined as 1, and LOW as 0.

LED_PIN.value(1)

Then we’ll use the following function that we imported from the time library for the delay and to control how long the LED will be ON or OFF.

time.sleep(0.5)

Unlike the delay function in Arduino, where we were providing time in milliseconds, the sleep function takes time in seconds.

LED blink with Pi pico in Micropython

import time
from machine import Pin
LED_PIN=Pin(5,Pin.OUT)        

while True:
  LED_PIN.value(1)            #Set led turn on
  time.sleep(0.5)
  LED_PIN.value(0)            #Set led turn off
  time.sleep(0.5)

With the help of the aforementioned code, you can use the ESP32 to blink LEDs in Micropython. The frequency of delay is fixed at 0.5 seconds, but you can experiment with the sleep function to change it.

LED with Pi Pico CircuitPython

First thing you need to download the Circuitpython support for Pi Pico.

We need to import three libraries. Time for using the sleep function i.e. to create delay, board library to use the GPIO pins in pi pico and digitalio library for controlling the states of LED.

import time
import board
import digitalio

Then we will set the pin number of LED to GP5, and then set the direction as OUTPUT.

LED_PIN = digitalio.DigitalInOut(board.GP5)
LED_PIN.direction = digitalio.Direction.OUTPUT

and then we can control the state of LED by changing its value to True for ON and False for OFF.

LED_PIN.value = True

LED blink with Pi pico in CircuitPython

import time
import board
import digitalio

LED_PIN = digitalio.DigitalInOut(board.GP5)
LED_PIN.direction = digitalio.Direction.OUTPUT

while True:
    LED_PIN.value = True
    time.sleep(0.5)
    LED_PIN.value = False
    time.sleep(0.5)

The ESP32 can flash LEDs in CircuitPython with the help of the aforementioned code. Although the delay frequency is set to 0.5 seconds, you can play about with the sleep function to alter it.

Exit mobile version