Raspberry Pi Pico - RGB LED
This guide shows you how to control an RGB LED with a Raspberry Pi Pico. We will cover:
- How to control RGB LED
- How to connect RGB LED to Raspberry Pi Pico
- How to connect RGB LED module with Raspberry Pi Pico
- How to program Raspberry Pi Pico to control the color of RGB LED
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of RGB LED
The RGB LED can make any color by combining red, green, and blue, the three basic colors. It has three different LEDs - one red, one green, and one blue - all in a single unit.
Pinout
An RGB LED comes with four pins.
- Attach the Common (Cathode-) pin to GND (0V).
- The R (red) pin manages the red color.
- The G (green) pin manages the green color.
- The B (blue) pin manages the blue color.
To connect an RGB LED to a Raspberry Pi Pico, it's better to use resistors that control the current, making the process complex. But, you can use the RGB LED module which already includes these resistors.
The RGB LED module also has four pins.
- Connect the Common (Cathode-) pin to GND (0V).
- The R (red) pin controls the red color.
- The G (green) pin controls the green color.
- The B (blue) pin controls the blue color.
※ NOTE THAT:
This guide shows how to use an RGB LED that has a common cathode. This means the cathode is the common pin. Some RGB LEDs might have the anode as the common pin.
How it works
In physics, a color consists of three values: Red (R), Green (G), and Blue (B). These values range from 0 to 255.
There are 16,777,216 colors made by mixing three different values.
By sending PWM signals (with a duty bit range from 0 to 255) to the R, G, and B pins, we can cause the RGB LED to show any color. The PWM duty cycle directed to the R, G, and B pins corresponds to the respective color values for Red (R), Green (G), and Blue (B).
Wiring Diagram
- How to connect a Raspberry Pi Pico to an RGB LED.
This image is created using Fritzing. Click to enlarge image
Do not connect just one resistor to the common pin of an RGB LED. You should connect three different resistors, one to each of the other pins as shown in the diagram. The LEDs in the RGB package have different characteristics, so they do not use the same amount of current. This difference can make the LEDs shine with uneven brightness and might even harm them if only one resistor is used on the common pin.
- How to connect a Raspberry Pi Pico to RGB LED module:
This image is created using Fritzing. Click to enlarge image
How To Control RGB LED
Let's learn how to change the RGB LED to a specific color, like #00979D, step by step.
- Pick your preferred color and get its color code.
- Use this color code selector: color picker.
- To extract a color code from a photograph, try this tool: Colors From Image.
- Change the color code to RGB format with this converter: RGB converter. Note these numbers: R = 0, G = 151, B = 157.
- Define the Raspberry Pi Pico pins that connect to the R (red), G (green), and B (blue) pins. For example:
- Set these Raspberry Pi Pico pins to output mode.
- Set the PWM frequency to 1000 Hz
- Set the PWM duty cycle
Raspberry Pi Pico - RGB LED Example Code
The code below alters the color of the LED in this sequence:
- #00C9CC (Red = 0, Green = 201, Blue = 204)
- #F7788A (Red = 247, Green = 120, Blue = 138)
- #34A853 (Red = 52, Green = 168, Blue = 83)