Using the Colour Sensor

The Colour Sensor can detect the colour or intensity of light that enters the small window on the face of the sensor. The Colour Sensor can be used in three different modes: Colour Mode, Reflected Intensity Mode, and Ambient Intensity Mode.
Colour Mode
In Colour mode, the Colour Sensor can detect the colour of a nearby object, or the colour of a surface near the sensor. You can use the Colour mode to detect, for example, the colour of a LEGO part held close to the sensor, or the colour of different markings on a piece of paper.


Tips and Tricks
When the Colour Sensor is in Colour mode, red, green, and blue LED lights on the front of the sensor will turn on.
The sensor can detect seven different colours: black, blue, green, yellow, red, white, and brown. An object that is not one of these colours may be detected as “No Colour”, or it may be detected as a similar colour. For example, an orange object might be detected as red or yellow, depending on how much red the orange has in it, or as brown or black if the orange is very dark or too far away from the sensor.
Tips and Tricks
The object or surface should be very close to the sensor (but not touching it) to be detected accurately.
Reflected Light Intensity Mode
In Reflected Light Intensity mode, the Colour Sensor detects the intensity of light that enters the sensor. The intensity of the light is measured as a percentage from 0 to 100, with 0 being very dark, and 100 being very bright.

When the Colour Sensor is in Reflected Light Intensity mode, a red LED light on the front of the sensor will turn on. If the sensor is close to an object or surface, this red light will reflect off of the object and then enter the sensor to be detected. You can use this to measure shades of colour on a surface or object, because darker shades of colour will reflect less of the red light back to the sensor.

You can use this mode to, for example, make your robot follow a black line on a white surface. As the sensor passes over the black line, the light measurement will gradually decrease as the sensor gets closer to the black line. This can be used to tell how close the robot is to the line.


Tips and Tricks
The Reflected Light Intensity mode measures the total amount of light entering the sensor. This includes the reflection of the red LED, plus any lights in the room. The sensor should be positioned close to (but not touching) the surface being measured, to reduce the effect of outside light sources.
Ambient Light Intensity Mode
In Ambient Light Intensity mode, like the Reflected Light Intensity mode, the Colour Sensor detects the intensity of light that enters the sensor. The intensity of the light is measured as a percentage from 0 to 100, with 0 being very dark, and 100 being very bright.

In Ambient Light Intensity mode, a blue LED light on the front of the sensor will turn on dimly. This blue light helps you identify that the sensor is in Ambient Light Intensity mode, but it does not affect the light measurement unless an object is very close to the sensor.

You can use this mode to detect the brightness of the room lights, or when other light sources shine on the sensor. You could use this also to detect when the lights to a room are turned on, or when a torch is shone on your robot.


Colour Sensor Data
The Colour Sensor can give the following data:
DataTypeRangeNotes
Colour Numeric0-7Used in Colour mode.
0 = No Colour
1 = Black
2 = Blue
3 = Green
4 = Yellow
5 = Red
6 = White
7 = Brown
Light Numeric0-100Used in Reflected Light Intensity and Ambient Light Intensity modes. Measures light intensity as a percentage, 0 = darkest, 100 = brightest.
Example 1: Drive until a Black Line is Reached (Method 1)
This program makes a robot drive until the Colour Sensor detects a black colour, then it stops. The program uses the Wait block in the Colour Sensor - Compare – Colour mode to test for the black colour.
Tips and Tricks
If you use this program with the Colour Sensor on your robot pointing downwards and close to a light-coloured surface with a thick black line on it, the robot can drive until it reaches the line.
Example 2: Drive until a Black Line is Reached (Method 2)
This program makes a robot drive until the Colour Sensor detects a dark colour, then it stops. The program uses the Wait block in the Colour Sensor - Compare – Reflected Light Intensity mode to wait until the light intensify becomes less than 50%.
Tips and Tricks
Compared to the method in Example 1 above, this program allows you to adjust how dark the line needs to be, by changing the Threshold Value (here 50%). Also, the robot will stop on any dark colour, not just black.
Example 3: Drive Only When the Room Lights Are On
This program makes a robot drive when the room lights are on and stop when you turn off the lights. The program uses a Switch with the Colour Sensor - Compare – Ambient Light Intensity mode to test whether the light is greater than 20%. The Switch chooses whether to turn the motors on or off. The Switch is repeated in a loop so that the robot will keep reacting to changes in light.
Example 4: Say “Red”, “Green”, and “Blue” when Detected
This program makes the EV3 say “Red”, “Green”, and “Blue” when the Colour Sensor detects these colours. The program uses a Switch in the Colour Sensor – Measure – Colour mode to choose between different Sound blocks based on the colour that is detected. A “No Colour” case is added and selected as the default so that the EV3 won’t say anything when one of the three colours is not seen.
Example 5: Display a Reflected Light Meter
This program puts a graphical light meter on the EV3 Display. The program uses a Colour Sensor block in the Measure – Reflected Light Intensity mode to measure the reflected light (0-100) and get the result on a data wire. The result is then multiplied by 1.78 to scale it to the EV3 screen width (178 pixels) and then used as the width of a filled rectangle shape. The process is repeated in a loop so that the display is continuously updated.
Tips and Tricks
While running this program, try moving the Colour Sensor over surfaces of different colours and shades. You will be able to see which colours reflect more light.
Colour Sensor Blocks and Modes
The table below shows all of the programming blocks and modes that you can use with the Colour Sensor.
BlockModeUse
WaitColour Sensor – Compare - ColourWait for the sensor to detect one of the selected colours.
Wait Colour Sensor – Compare – Reflected Light IntensityWait for the reflected light intensity to reach a certain value.
Wait Colour Sensor – Compare – Ambient Light IntensityWait for the ambient light intensity to reach a certain value.
WaitColour Sensor – Change - ColourWait for the detected colour to change.
WaitColour Sensor – Change – Reflected Light IntensityWait for the reflected light intensity to change by a certain amount.
WaitColour Sensor – Change – Ambient Light IntensityWait for the ambient light intensity to change by a certain amount.
LoopColour Sensor - ColourRepeat a sequence of blocks until one of the selected colours is detected.
Loop Colour Sensor – Reflected Light IntensityRepeat a sequence of blocks until the reflected light intensity reaches a certain value.
Loop Colour Sensor – Ambient Light IntensityRepeat a sequence of blocks until the ambient light intensity reaches a certain value.
SwitchColour Sensor – Measure - ColourChoose between two or more different sequences of blocks depending on which colour is detected.
SwitchColour Sensor – Compare - ColourChoose between two sequences of blocks depending on whether or not one of the selected colours is detected.
Switch Colour Sensor – Compare – Reflected Light IntensityChoose between two sequences of blocks depending on the reflected light intensity.
Switch Colour Sensor – Compare – Ambient Light IntensityChoose between two sequences of blocks depending on the ambient light intensity.
Colour SensorMeasure - ColourMeasure the detected colour (0-7) and get the result on a Numeric data wire.
Colour SensorMeasure – Reflected Light IntensityMeasure the reflected light intensity (0-100) and get the result on a Numeric data wire.
Colour SensorMeasure – Ambient Light IntensityMeasure the ambient light intensity (0-100) and get the result on a Numeric data wire.
Colour SensorCompare - ColourCompare the detected colour to one or more selected colours, and get the result on a Logic data wire (True if it matches any of the selected colours).
Colour SensorCompare – Reflected Light IntensityCompare the reflected light intensity to a threshold, and get the result on a Logic data wire.
Colour SensorCompare – Ambient Light IntensityCompare the ambient light intensity to a threshold, and get the result on a Logic data wire.
Data LoggingSee Data Logging.
Colour
Quick links