BBC micro:bit
Colour Sensing 2


The colour sensor can return RGB values for you if you write a different value to the sensor before reading.

Reading from colour sensors is not as straightforward as you think. You need to experiment a little to be able to reliably pick out a colour.

micro:bit circuit

Here is my code for working out some colour readings.

from microbit import *

def read_RGB():
    rgb =,4)
    return rgb[2],rgb[1],rgb[0]
while True:
    r,g,b = read_RGB()

The RGB values are printed to the REPL using the Mu program.

Using These Values

My Lego line following track has a range of different shades of colour on it. I used these for testing. Here are some of the readings I got,

ColourRGB Values
Dark Blue0,22,72
Light Blue74,207,255
Dark Green0,7,0
Lighter Green0,38,20
Light red145,0,0
Dark Red93,12,13
Light Orange255,175,32

For most of these the values were quite stable but occasionally wobbled in one or more of the colour channels by up to half a dozen.

The first thing to note is that these don't correspond to the values you might use to produce the colour with an RGB LED. You have to remember that you are dealing with reflected, not emitted light. The two things are not as compatible as you might think.

In order to reliably read a colour, you first need to work out the readings you are getting from the particular shade of that colour that you want to read. There is not a whole lot of difference to my eye between the yellow and orange colours I read for the table above. You could call them both yellow if you wanted and no one would think you strange.

Armed with a value you get for your colour, you also need to account for the 'wobble' and allow a little tolerance. All in all you need 6 values to reliably read a colours. That is, a low and high value for each of the colour channels.

You also need to consider that some of the colours or shades are effectively unusable. The dark green I read for my table is so close to the reading for black that it is not worth using in an application.

A really nice use of this feature would be to play music with the pitch and duration of the notes being determined by the length and colour of a line on a track.