The EV3 Color Sensor is used to detect colors or light intensity. The sensor is very useful for both line following and obstacle detection.
Facing down, towards the surface, the Color Sensor can distinguish between 7 colors or measure the intensity of the reflected light, making it ideal for line following. By emitting its own light, the sensor can work even in absolute darkness to determine the color or brightness of the surface.
When facing forward, the light intensity will change when nearing an obstacle. This behavior of the Color Sensor is used for obstacle detection, turning the Color Sensor into a proximity sensor. Note that this will work only for short distances of 2 cm or less between the sensor and an obstacle. For greater distances, we recommend using the Ultrasonic Sensor.
The Color Sensor block supports two methods:
- get.ColorID - Used to identify colors, the ColorID method will return a different number for each detected color. The color ID's are as follows:
Note that the color sensor cannot identify all colors and may stray when confronted with unassigned colors.
- get.Red - Used to measure the intensity of the reflected light. Since black absorbs light while White reflects it, the returned value can distinguish between bright and dark colors. Dark colors return values as low as 0, while bright colors can return values as high as 100. This makes the Color Sensor getRed method ideal for line following using Proportional or PID control algorithms.
You can also use the Wait Until block to pause the code until the robot detects a specific color or crosses a certain threshold.
In this example, the robot will drive forward for as long as the Color Sensor is over a bright surface:
The program will also print to the console the current light intensity value (0-100).