1. Hardware setup

An RGB LED is made of three LEDs: a red one, a blue one and a green one. Some RGB LEDs have the anode terminal (or "+" terminal) in common and some have the cathode terminal (or "-" terminal) in common. The one we are going to use in our project is a Common Cathode RGB LED.



Micro:bit pinsRGB LED pins
0Red (via resistor)
1Green (via resistor)
2Blue (via resistor)
GNDCommon Cathode


Like with all LEDs we are going to use a resistor (220 Ohms, in our case) in series in order to limit the current, thus protecting the LED and the micro:bit.

2. Program
At startup we initialize red, green and blue variables with values chosen randomly between 0 and 1023. This way we have always a different starting color.


Then we can choose a changing direction for each color: some could go up and others could go down. This way the pattern. We set these values by setting directionRed, directionBlue and directionGreen to either “1” or “-1”.


In the forever loop, we will change the level of each color according to the direction set in the first step: if the direction is -1, then the level of color is going to decrease by 1 step, and if the direction is 1, then the color is going to increase by the same quantity.


When the level reaches a limit, either the low limit or the high limit, the direction is going to change (from -1 to 1 or from 1 to -1). In doing so, we only use addition to change the level, but the color level can go both ways.


The last step is to send the values to respective pins.

Ways to improve:


  1. If we want a random direction every time the system starts, one way is to set the direction depending on the level of color chosen first. Thus, if the value of red is an even number, then the direction is positive (1), else, if the value of red is odd, then the direction is negative (-1). This will give more randomness.


2. Visual representation of color level


To have a visual representation of the color level, we could display the level on LED matrix. The red color level will be display on column 0, green on column 2 and blue on column 4. We can use functions for this task.



Then we call the functions from the forever loop.