Select Page

# LED Colours – learning RGB colours using micro:bit

by | Oct 11, 2019

In this project we set a special LED to a range of colours. Some LEDs can display more than just white light. Some can display a range of up to 16 million colours.

You’ll need: Plug the USB cable into USB socket on your computer so that you can flash you code onto the micro:bit.

Connect the 4 crodile clips to pins 0, 1, 2 and GND on the micro:bit.

Connect the other end to the red, green, blue and GND connectors on the LED board.

It doesn’t matter which colour cable you use but you must make sure they connect to the correct connector at each end. This is how it should look: After the project is complete you can swap the USB cable for the battery pack to use it away from the computer.

## RGB LED Code

### Colours

Colours are defined using a system called RGB, which stands for Red, Green, Blue.

A colour is made by setting a value for red, blue and green, where each value is between 0 and 255, where 255 is the brightest. The number of possible combinations is more than 16 million.

For example, to create red, set a value for red but leave blue and green set to 0. So:

• red is 255, 0, 0 (bright)
• blue is 0,127, 0 (not so bright)
• green is 0, 0, 63 (even less bright)

Or combine colours, like these:

• Orange = 255, 127, 0
• Purple = 127, 0, 255
• Brown is 127, 63, 31

To test everything is working, type in this code, which should show a blue light. Can you work out why?

``````# RGB LED colours using micro:bit
# Richard Zealley 2019

# Import micro:bit libraries
from microbit import *

# Colour test - blue
pin0.write_analog(0)
pin1.write_analog(0)
pin2.write_analog(255)
``````

### Experimenting

• Try moving the 255 value to different pins. What colours do you get?
• Try increasing the value from 255. What happens?
• What happens if you set a value for more than one colour?

You are changing the depth of each of the three colours by changing the voltage applied to each pin.

The maximum value that you can use is 1023 for each colour.

``````# RGB LED colours using micro:bit
# Richard Zealley 2019

# Import micro:bit libraries
from microbit import *

# Colour test 2
pin0.write_analog(1023)
pin1.write_analog(0)
pin2.write_analog(1023)
``````

### Flashing Colours

Just as we did in the Pulse project, we can use the micro:bit’s sleep function to make the LED flash.

For example, we can make it flash between reg, green and blue using this code:

We are using a variable called pulse again to make it easier to change the ime between each colour.

See how we turn the LED off at end.

``````# RGB LED colours using micro:bit
# Richard Zealley 2019

# Import micro:bit libraries
from microbit import *

pulse = 1000;

# Red then pause
pin0.write_analog(1023)
pin1.write_analog(0)
pin2.write_analog(0)
sleep(pulse)

# Greenn pause
pin0.write_analog(0)
pin1.write_analog(1023)
pin2.write_analog(0)
sleep(pulse)

# Blue then pause
pin0.write_analog(0)
pin1.write_analog(0)
pin2.write_analog(1023)
sleep(pulse)

# Turn off
pin0.write_analog(0)
pin1.write_analog(0)
pin2.write_analog(0)
``````

### Using a function

Functions are good way to avoid repeating code over and over. Looking at the code above, we can simplify this by creating a function to set the three colours.

The functions has three arguments, one for each colour. We’ll use these when we call the function to set each of the three colours.

#### Challenge

Can you make the LEDs flash in a continuous loop, like in the Pulse project?

``````# RGB LED colours using micro:bit
# Richard Zealley 2019

# Import micro:bit libraries
from microbit import *

pulse = 1000;

# function to set LED to three colours
def set_colour(red, green, blue):
pin0.write_analog(red)
pin1.write_analog(green)
pin2.write_analog(blue)

# Red then pause
set_colour(1023, 0, 0)
sleep(pulse)

# Green then pause
set_colour(0, 1023, 0)
sleep(pulse)

# Blue then pause
set_colour(1023, 0, 0)
sleep(pulse)

# Turn off
set_colour(0, 0, 0)``````

### Random Colours

Finally, we can use Pythons random number generator to create some random, flashing colours.

Note that we need to add some extra libraries at the top of the code to be able to use the random number functions.

#### Challenge

Can you also make the time between each colour random too?

``````# RGB LED colours using micro:bit
# Richard Zealley 2019

# Import micro:bit libraries
from microbit import *

# Import random number functions
from random import randint

pulse = 1000;

def set_colour(red, green, blue):
pin0.write_analog(red)
pin1.write_analog(green)
pin2.write_analog(blue)

# Loop forever
while True:
# Create a random colur and pause
set_colour(randint(0, 1023), randint(0, 1023), randint(0, 1023))
sleep(pulse)``````