Using the Color Sensor

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


Tips and Tricks
When the Color Sensor is in Color mode, red, green, and blue LED lights on the front of the sensor will turn on.
The sensor can detect seven different colors: black, blue, green, yellow, red, white, and brown. An object that is not one of these colors may be detected as “No Color”, or it may be detected as a similar color. 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 Color 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 Color 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 color on a surface or object, because darker shades of color 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 Color 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 flashlight is shined on your robot.


Color Sensor Data
The Color Sensor can give the following data:
DataTypeRangeNotes
Color Numeric0-7Used in Color mode.
0 = No Color
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 Color Sensor detects a black color, then it stops. The program uses the Wait block in the Color Sensor - Compare – Color mode to test for the black color.
Tips and Tricks
If you use this program with the Color Sensor on your robot pointing downwards and close to a light-colored 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 Color Sensor detects a dark color, then it stops. The program uses the Wait block in the Color 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 color, 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 Color 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 Color Sensor detects these colors. The program uses a Switch in the Color Sensor – Measure – Color mode to choose between different Sound blocks based on the color that is detected. A “No Color” case is added and selected as the default so that the EV3 won’t say anything when one of the three colors 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 Color 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 Color Sensor over surfaces of different colors and shades. You will be able to see which colors reflect more light.
Color Sensor Blocks and Modes
The table below shows all of the programming blocks and modes that you can use with the Color Sensor.
BlockModeUse
WaitColor Sensor - Compare - ColorWait for the sensor to detect one of the selected colors.
Wait Color Sensor - Compare - Reflected Light IntensityWait for the reflected light intensity to reach a certain value.
Wait Color Sensor - Compare - Ambient Light IntensityWait for the ambient light intensity to reach a certain value.
WaitColor Sensor - Change - ColorWait for the detected color to change.
WaitColor Sensor - Change - Reflected Light IntensityWait for the reflected light intensity to change by a certain amount.
WaitColor Sensor - Change - Ambient Light IntensityWait for the ambient light intensity to change by a certain amount.
LoopColor Sensor - ColorRepeat a sequence of blocks until one of the selected colors is detected.
Loop Color Sensor - Reflected Light IntensityRepeat a sequence of blocks until the reflected light intensity reaches a certain value.
Loop Color Sensor - Ambient Light IntensityRepeat a sequence of blocks until the ambient light intensity reaches a certain value.
SwitchColor Sensor - Measure - ColorChoose between two or more different sequences of blocks depending on which color is detected.
SwitchColor Sensor - Compare - ColorChoose between two sequences of blocks depending on whether or not one of the selected colors is detected.
Switch Color Sensor - Compare - Reflected Light IntensityChoose between two sequences of blocks depending on the reflected light intensity.
Switch Color Sensor - Compare - Ambient Light IntensityChoose between two sequences of blocks depending on the ambient light intensity.
Color SensorMeasure - ColorMeasure the detected color (0-7) and get the result on a Numeric data wire.
Color SensorMeasure - Reflected Light IntensityMeasure the reflected light intensity (0-100) and get the result on a Numeric data wire.
Color SensorMeasure - Ambient Light IntensityMeasure the ambient light intensity (0-100) and get the result on a Numeric data wire.
Color SensorCompare - ColorCompare the detected color to one or more selected colors, and get the result on a Logic data wire (True if it matches any of the selected colors).
Color SensorCompare - Reflected Light IntensityCompare the reflected light intensity to a threshold, and get the result on a Logic data wire.
Color SensorCompare - Ambient Light IntensityCompare the ambient light intensity to a threshold, and get the result on a Logic data wire.
Data LoggingSee Data Logging.
Color
Quick links