Let's get started with the PiicoDev® 3x RGB LED Module. In this guide, we'll connect the LED Module to our Raspberry Pi Pico and get it working with some example code to display colourful patterns.
To follow along, it's best to have:
- A Raspberry Pi Pico with pins soldered (pointing down)
- A PiicoDev 3x RGB LED Module
- A PiicoDev Expansion Board for Raspberry Pi Pico
- A PiicoDev Cable
- (Optional) A PiicoDev platform helps secure everything together.
If you prefer not to use the Expansion Board for Raspberry Pi Pico, there are other connection options in our PiicoDev Connection Guide.
Contents
- Connect the RGB Module to your Pico
- Download MicroPython Modules
- Example Code - Feature Demo
- Remix - Fade LEDs
- Multiple Modules (Traffic Lights)
- Reference
Connect the RGB LED Module to your Pico
Plug your Pico into the Expansion Board, connect your OLED to the Expansion Board via the PiicoDev cable, and finally connect your Pico to your computer with a USB lead.
On the RGB Module, Ensure both ID switches are OFF, and both ID jumpers (on the back side) are unsoldered.
If you're unfamiliar with connecting PiicoDev modules, read the PiicoDev Connection Guide before proceeding.
Download MicroPython modules
We will need three files to easily control the LED Module:
- Download the PiicoDev Unified Library: PiicoDev_Unified.py (right-click, "save link as").
- Download the device module: PiicoDev_RGB.py (right-click, "save link as")
- Download the example script: main.py (right-click, "save link as")
It will be best to keep these files wherever you like to keep your coding projects eg. Documents > PiicoDev
Feature Demo
We'll be working in Thonny - if you're unfamiliar working with Thonny see our guide for Thonny and Raspberry Pi Pico.
Open Thonny, connect to your Pico and navigate to where you stored the files from the last step. Right-click each file and select "Upload to /" to upload them to your Pico (Hint: View the files menu with View > Files)
Restart your Pico (Keyboard shortcut, Ctrl+D) and you should see your RGB LED Module run through a feature demonstration. The demos, in order, are:
- Colour individual pixels
- Clear the LEDs
- Fill the LEDs with a colour from the colour wheel, and toggle the "Power" LED
Remix - Fade LEDs
In this remix, we'll use loops to animate the LEDs, glowing them on then off. Copy the following code and paste it into main.py. Re-run main.py to observe the results.
from PiicoDev_RGB import PiicoDev_RGB from PiicoDev_Unified import sleep_ms # Cross-platform compatible sleep function leds = PiicoDev_RGB() # initialise the LED module with conservative default brightness while True: for x in range(255): # glow on leds.fill( [x, 0, 0] ) # fill() will automatically show() sleep_ms(2) for x in range(255,0,-1): # glow off leds.fill( [x, 0, 0] ) # fill() will automatically show() sleep_ms(2)
Multiple Modules (Traffic Lights)
We can control multiple LED Modules by setting unique IDs using the on-board switches and jumpers. For this remix, leave one module with all its switches cleared (off) and set ID 1 on the second module. Make sure the solder jumpers on the rear of each module are open (unsoldered)
The following code initialises two LED modules to be controlled independently. This is done by setting the id argument of the second module to indicate the switch positions: [1,0,0,0]. For this module, only the first switch is ON, and so we put a 1 in the first position. If we closed the second switch instead, we would use id=[0,1,0,0].
To learn more about PiicoDev Modules with ID options, read our PiicoDev Connection Guide (Smart Modules).
from PiicoDev_RGB import PiicoDev_RGB from PiicoDev_Unified import sleep_ms # Cross-platform compatible sleep function trafficA = PiicoDev_RGB() # initialise an RGB LED module with default address (all switches OFF) trafficB = PiicoDev_RGB(id=[1,0,0,0]) # initialise RGB LED module using switch positions (first switch ON, all others OFF) red = [255,0,0] amber = [225,165,0] green = [0,255,0] while True: ### Traffic flows one way trafficA.clear(); trafficB.clear() trafficA.setPixel(2, green); trafficA.show() trafficB.setPixel(0, red); trafficB.show() sleep_ms(2000) ### Transition trafficA.clear(); trafficA.setPixel(1, amber); trafficA.show() sleep_ms(1000) ### Traffic flows the other way trafficA.clear(); trafficB.clear() trafficA.setPixel(0, red); trafficA.show() trafficB.setPixel(2, green); trafficB.show() sleep_ms(2000) ### Transition trafficB.clear(); trafficB.setPixel(1, amber); trafficB.show() sleep_ms(1000)
Reference
setPixel
setPixel(n, colour) will set led # n to the RGB colour colour = [r,g,b] where r,g,b are 0-255
Once the pixels are set with setPixel(), the new values must be pushed to the physical LEDs with show().
show
Updates the physical LEDs with data set by eg. setPixel().
clear
Blanks all the RGB LEDs. It is not necessary to call show() after calling clear().
setBrightness
setBrightness(x) controls the maximum brightness of the RGB LEDs, where x may be between 0-255. This function is useful to avoid being dazzled by the bright LEDs, or to keep current consumption low.
fill
fill(colour) will fill all RGB LEDs with the colour specified by the RGB list colour. Automatically updates the LEDs by calling show()
wheel
wheel(i) returns an RGB colour list [r,g,b] colour from the colour wheel, where i is the angle (in degrees) on the wheel. You don't have to constrain i to between 0-360, i will wrap the colour wheel.
pwrLED
pwrLED(True/False) will set the LED to True = on, False = off