Raspberry Control RGB LED to Display Different Colors in Turn
In the Introduction to Electronic Hardware, we mainly used the LED, which only has a single color. We can control the brightness of the light through the program, but we cannot change the color to present more diverse effects. In Project 1, we will control the RGB LED (ws2813 mini) to display different colors in turn.
In this project, the electronic hardware we need to use is as follows:
• Raspberry Pi Pico
• Grove Shield for Pi Pico
• Grove - RGB LED (WS2813 Mini)
Connect the Pico and the Grove Shield, use a Grove data cable to connect the RGB LED to D18.
Write a Program
To interface with the new WS2813 RGB LED module, we will need a new library ws2812. py(2kB) to control the RGB LED. After downloading the library file and saving it in Pico, we can start to introduce the function library we need.
The functions used in the ws2812 library are as follows:
• pixels_fills() — RGB LED fill in color
• pixels_show() — RGB LED show color
First, introduce the function library that we need:
Next, define the display color of the RGB LED. We have listed the codes of common colors. If you want other colors, you can find them in the RGB Color Code List.
After defining the colors, create a Tuple named COLORS to collect all the colors that need to be displayed.
Finally, define the RGB LED, traverse all the color elements in the COLORS Tuple through the “for" statement, and display them on the RGB LED. In order to display each color clearly, we will buffer them with a delay.
The complete program is as follows:
Use a USB cable to connect Pico to the computer, click the “run" button to save the program to any location. Now, you can see the RGB LED displays different colors.