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 (model 3, 4, Zero W) This tutorial will use a Raspberry Pi 4, Model B
- A PiicoDev 3x RGB LED Module
- A PiicoDev Adapter for Raspberry Pi
- A PiicoDev Cable
- (Optional) A PiicoDev platform helps secure everything together.
If you prefer not to use the PiicoDev Adapter for Raspberry Pi, there are other connection options in our PiicoDev Connection Guide.
If you haven't set up a Raspberry Pi to be used as a desktop computer before, head over to our Raspberry Pi Workshop for Beginners to get started.
Contents
- Connect the RGB Module to your Pi
- Download MicroPython Modules
- Example Code - Feature Demo
- Remix - Fade LEDs
- Multiple Modules (Traffic Lights)
- Reference
Connect the RGB LED Module to your Pi
Mount the PiicoDev Adapter on your Raspberry Pi and plug connect your RGB Module to the Adapter with the PiicoDev cable.
Ensure both ID switches are OFF, and both ID jumpers (on the back side) are unsoldered.
If you're unfamiliar with connecting PiicoDev "Smart" Modules, read the PiicoDev Connection Guide before proceeding.
Enable I2C
Power on your Raspberry Pi. Open the Raspberry Pi Configuration Menu, select the Interfaces tab and ensure I2C is enabled.
You should only need to do this step for your first PiicoDev project.
Install/Upgrade PiicoDev
Open Thonny (Pi Start Menu > Programming > Thonny IDE) and open the Manage Packages menu (Tools > Manage Packages)
Search for 'piicodev' and install or upgrade if necessary.
Download Example script
Download the example script main.py (right-click, "save link as") and open it in Thonny. This script is ready to run so click the run button to see a demonstration of some features.
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
It will be best to keep these files wherever you like to keep your coding projects eg. /home/pi/PiicoDev
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