Raspberry Pi Pico with
RGB LED
Introduction
We all must be familiar with multi-colored LEDs. Typical example: the charging indicator in my laptop displays a red color while charging, then turns white when the battery is full. That's a bi-colored LED.
​
In this tutorial, we take a step further and see how we can play with the values of a tri-colored LED using a Raspberry Pi Pico with a few lines of code to get almost any color of choice.
Requirements
-
Raspberry Pi Pico running MicroPython
-
Micro USB cable
-
RGB LED (common cathode)
-
220Ω resistors (x3)
-
Breadboard
-
Male - Male jumper wires (x4)
-
Thonny IDE
RGB LED
​
An RGB LED is basically an LED package that can produce almost any color. This is usually a combination of three LED colors (Red, Green, and Blue (RGB)) in a single package.
​
RGB LEDs have 4 legs rather than the usual two. Here, we'll be using a common cathode RGB LED which means that out of the four legs in our LED, one will be connected to GND and the other three legs will be connected to GPIO pins of the Pico.
There are also common anode RGB LEDs, which means that one leg needs to connect to a 3.3V pin, and the other legs need to connect to GPIO pins.
​
With either type of RGB LED, a resistor is needed for each pin that is connected to a GPIO pin. We will be making use of 220Ω resistors in this tutorial.
Hardware Assembly
Connection
Layout
Programming
​
You can simply just turn on/off the individual RGB LEDs using the traditional LED control code as shown below:
from machine import Pin
import utime
red = Pin(1, Pin.OUT)
green = Pin(2, Pin.OUT)
blue = Pin(3, Pin.OUT)
while True:
red.value(1)
utime.sleep(1)
red.value(0)
utime.sleep(1)
​
green.value(1)
utime.sleep(1)
green.value(0)
utime.sleep(1)
​
blue.value(1)
utime.sleep(1)
blue.value(0)
utime.sleep(1)
The above code will turn on RED LED for one second then off, then does the same for GREEN and BLUE, then the loop continues.
​
That seems pretty boring don't you think? Whereas you can't even vary the colors.
Let's take things up a bit.
​
In this next section, we'll be varying the values (from 0 to 255) we send to each of these LEDs in other to get the color we desire. For example, if we send 255 to RED and 0 to GREEN and BLUE (255, 0, 0), we get a red color. If we send 128 to RED, 0 to GREEN, and 128 to BLUE (255, 0, 255), we get a purple color.
​
Check out this RGB color mixer or this color chart for RGB Decimal code for any color of choice.
​
Before we dive in, we will be needing the RGBLED module to help make our work easier. The module is found in the picozero library which you can install using the simple steps below:
Install picozero Library
-
Open your Thonny IDE, click on Tools, and on the pop-up window, click on Manage packages... this will pop-up a new window.
-
In the new window, you'll see a search bar. Type picozero in the search bar and click on the Serach on PyPI button on the right.
-
Clicking on the search button will pop-up the window below. Click on the Install button at the bottom left of the window.
-
When the installation is complete, the window below will appear. Ignore the message and click on the Close button.
Code
​
Open your Thonny IDE and input the following code:
from picozero import RGBLED
from time import sleep
​
rgb = RGBLED(red = 1, green = 2, blue = 3)
​
while True:
rgb.color = (255,0,0)
sleep(0.5)
rgb.color = (0,255,0)
sleep(0.5)
rgb.color = (0,0,255)
sleep(0.5)
The above code will turn on the Red LED first for half a second, same for Green the Blue. As discussed earlier, you can just play with value to change the color to any color of choice.
​
About that my laptop charging indicator story we started this tutorial with, you can program a similar system with what we have learnt so far with simple if statements like:
if battery_level == 100:
#light white
rgb.color = (255, 255, 255)
else:
#light red
rgb.color = (255, 0, 0)