3x RGB LED¶
On this page we will learn
- how to connect the 3x RGB LED
- how to set each LED's colour
- how to fill, clear and change the brightness of the LEDs
- how to pick colours from a colour wheel
Terminology
- RGB LED – an LED that mixes red, green and blue light so it can show any colour.
The PiicoDev 3x RGB LED Module has three full-colour LEDs that can each be set to any colour and brightness.
Possible uses:
- traffic lights
- status indicators for sensor readings
- colour-mixing projects
- notification lights
Connect it¶
- Connect the module to the PiicoDev adapter with a PiicoDev cable. See Using PiicoDev.
- Upload these files to the micro:bit with
main.py:PiicoDev_Unified.pyPiicoDev_RGB.py
- The LEDs are numbered
0,1and2. - Colours are lists of three numbers,
[red, green, blue], each from0to255. For example,[255, 0, 255]is magenta.
Set it up¶
Methods¶
| Method | Parameters | Returns | Description |
|---|---|---|---|
leds.setPixel(n, colour) |
n: LED 0–2colour: [r, g, b] |
none | Sets one LED's colour. Nothing changes until show(). |
leds.show() |
none | none | Sends the colours set with setPixel() to the LEDs |
leds.fill(colour) |
colour: [r, g, b] |
none | Sets all three LEDs to one colour straight away |
leds.clear() |
none | none | Turns all the LEDs off |
leds.setBrightness(level) |
level: 0–255 |
none | Sets the brightness of all the LEDs straight away (default 50) |
leds.pwrLED(state) |
state: True or False |
none | Turns the small power LED on or off |
wheel(h) |
h: position on the colour wheel, 0–1 |
list [r, g, b] |
Converts a colour-wheel position into a colour |
setPixel() and show()¶
setPixel() sets the colour of one LED in memory. show() sends the colours to the LEDs so they light up.
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 2 → imports the RGB LED driver.
- line 5 → creates the module and calls it
leds. - line 8 → starts an endless loop.
- line 9 → sets LED
0to red, in memory. - line 10 → sends the colour to the LEDs so it lights up.
fill()¶
Sets all three LEDs to the same colour straight away.
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 2 → imports the RGB LED driver.
- line 5 → creates the module and calls it
leds. - line 8 → starts an endless loop.
- line 9 → sets all the LEDs to magenta.
clear()¶
Turns all the LEDs off.
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 2 → imports the RGB LED driver.
- line 5 → creates the module and calls it
leds. - line 8 → starts an endless loop.
- line 9 → sets all the LEDs to white.
- line 10 → waits 1 second.
- line 11 → turns all the LEDs off.
- line 12 → waits 1 second before the loop repeats.
setBrightness()¶
Sets the brightness of all the LEDs, from 0 (off) to 255 (brightest).
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 2 → imports the RGB LED driver.
- line 5 → creates the module and calls it
leds. - line 6 → sets all the LEDs to blue at the default brightness.
- line 9 → starts an endless loop.
- line 10 → sets the brightness to
10, much dimmer than the default50. The LEDs change straight away, so we don't needshow().
pwrLED()¶
Turns the small green power LED on the module on (True) or off (False).
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 2 → imports the RGB LED driver.
- line 5 → creates the module and calls it
leds. - line 8 → starts an endless loop.
- line 9 → turns the power LED off.
wheel()¶
Converts a position on the colour wheel, from 0 to 1, into an [r, g, b] colour.
wheel() is imported separately from the driver. It makes rainbow effects easy: 0 is red, and moving towards 1 goes through yellow, green, cyan, blue and magenta back to red.
PRIMM
- Predict what you think will happen. Be specific.
- Run the program.
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → imports all the commands from the
microbitlibrary. - line 2 → imports the RGB LED driver and the
wheelfunction. - line 5 → creates the module and calls it
leds. - line 8 → starts an endless loop.
- line 9 → gets the colour halfway around the wheel (cyan) and fills the LEDs with it.
Documentation¶
Documentation is the official guide written by the people who made the code library, and we can use it to look up every method and its parameters, including ones that aren't covered on this page.
Exercises¶
PRIMM
Time to modify the code and see what happens.
Starter files are in the rgb_led folder of your tutorial files. Solutions are on the Exercise Solutions page.
Exercise 1¶
Starter: rgb_led/ex1_traffic_light
Can you make a traffic light, with LED 0 red, LED 1 amber and LED 2 green, lit one at a time in the right order?
Exercise 2¶
Starter: rgb_led/ex2_temperature_light
Can you show the temperature as a colour: blue if it is below 20 °C, green from 20 to 25 °C, and red above 25 °C?