Using the VEX IQ (1st gen) Color Sensor

The Color sensor uses reflected light to detect an object’s color, hue value, grayscale value, brightness, and proximity.

VEX IQ (1st gen) Color Sensor piece.

The VEX IQ Color Sensor is included in the VEX IQ Super Kit and can also be purchased here.

Using a 2nd Generation VEX IQ Robot? See an overview of 2nd Generation sensors here, and information on the Optical Sensor, the 2nd Generation version of the Color Sensor.


Description of the Color Sensor

The VEX IQ Color Sensor has multiple modes, which allow it to capture different types of information from its environment. The information it collects is affected by the lighting conditions of its environment as well as the distance between the sensor and the object or surface it is reading.


Color wheel demonstrating how the 360 degree circle correlates to a hue value. The color red is at a degree of 0, the color green is at a degree of 120, and the color blue is at a degree of 240.


How the Color Sensor Works

The Color Sensor can detect both color and proximity.

Detecting Colors

VEXcode IQ Brightness of block that reads Color3 brightness in %.

Diagram of a Color Sensor detecting a bright surface and reporting a value of 80%, and then detecting a dark surface and reporting a value of 20%.

When in brightness mode, the Color Sensor is used to detect the intensity of all light in the robot’s environment. The more light that reaches the Color Sensor while it's active, the higher the percentage value sent to the Robot Brain.

VEXcode IQ Set color sensor light block that reads Set Color3 light to 50%.

Color Sensor shown attached to a robot in a dark environment. The Color Sensor's lamp is turned on to illuminate its surroundings.

If the detected brightness percentage is low or unreliable, the Color Sensor’s lamp can be turned on or the lamp’s brightness percentage can be raised by using the following set light to block:

Three VEXcode IQ Color Sensor blocks. The first is Color of block that reads Color3 color name. The second is a Hue of block that reads Color3 hue in degrees. The third is a Color detects block that reads Color3 detects none?

The Color Sensor can report the color it sees as either a color value or as a hue value.

VEXcode IQ Color detects block that reads Color3 detects none? The block's Color dropdown menu is opened to show a list of colors for it to sense. The list reads orange, purple, red violet, violet, blue violet, blue green, yellow green, yellow orange, and red orange.

Color values. There are 14 listed colors that the Color Sensor can detect. The colors missing from the image of the block are red, green, blue, white, and yellow.

Hue value diagram demonstrating how the 360 degrees correlate to a hue value. The color red is at a degree of 0, the color green is at a degree of 120, and the color blue is at a degree of 240.

Hue values. Hue values are like color values but numerical. The hue value ranges from 0 to 360, like degrees. The color values listed above have their own equivalent hue value ranges. 

It is important when detecting colors and hues that the Color Sensor have the proper amount of light to do so. Be sure to test, also known as calibrating, your Color Sensor in different levels of light and with the lamp set at different levels to determine which is the most reliable setting for your robot’s Color Sensor.

Detecting Proximity

The Color Sensor includes an infrared sensor-emitter. The infrared emitter shines an invisible light and then detects its reflection. If most of the infrared light bounces back toward the sensor, it tells the Robot Brain that an object is close.

VEXcode IQ Found an object block that reads Color3 is near object?

Common Uses of the Color Sensor

Color sensors are used in many technology applications where having specific colors on displays or products is important.

Some common uses in daily life include:

  • Color calibration tools check if a digital screen is displaying accurate colors, and then adjust the display settings as needed. It is important for graphic artists and any designer who works with color on a digital screen to be able to see exactly the correct color being displayed. Misunderstandings and wasted resources result when the colors are not accurate.
  • Cameras and camcorders use color sensors to adjust settings based on the lighting conditions they are in, in order to enhance their photos and videos by maximizing light levels and enhancing otherwise dull colors. These sensors also allow a photographer to focus on specific colors in their photos.
  • Color sensors are sometimes used in manufacturing, to quickly inspect if a product has the correct color before it gets shipped to a customer. For example, fruits and vegetables that turn colors when ripe or ready to be eaten can be scanned to ensure that they are the correct color to send to market. 


Some common uses with a VEX IQ Robot include:

  • This sensor can be used to detect the color of an object. View this animation to see a robot drive forward until the Color Sensor detects a Green Cube.

  • This sensor can be used to detect and/or follow a line. View this animation to see a robot using the Color Sensor to drive along a marked line.

  • This sensor can detect whether an object is near.

Color Sensors on a Competition Robot

Remember, it is important when detecting colors and hues that the Color Sensor have the proper amount of light to do so. Be sure to test, also known as calibrating, your Color Sensor each time you arrive at a new competition site because different levels of light can affect how your Color Sensor performs. Test your project with the lamp set at different levels to determine which is the most reliable setting for your robot’s Color Sensor. 

The information collected by the Color Sensor is useful for programming a competitive robot to respond to a variety of conditions. The Color Sensor can make a competition robot better in the following ways:

  • It lets the robot detect the color of an object near the sensor. This is useful if you want the robot to sort differently colored objects, drive up to a specifically colored object, or detect the color of objects as they pass by the sensor.
  • It lets the robot detect how much light is reflected back into the sensor. This allows your robot to drive until it reaches a line on a surface, or even to follow a line.
  • It lets the robot know whether an object or surface is near. This is helpful in determining whether a detected color is a reading from a nearby object or potentially, an anomalous reading of a distant surface or light. 

Coding the Color Sensor in Blocks

VEXcode IQ Found an object block that reads Color3 is near object?

The <Found an object> block is a Boolean reporter block that reports a condition as either true or false. Boolean blocks, like the <Found an object> block fit inside blocks with hexagonal (six-sided) inputs for other blocks.

The <Found an object> Boolean block reports 'true' if the 'true' if the Color sensor detects an object, and 'false' if the sensor does not. To learn more about Boolean blocks visit the Help or the Block Shapes and Meaning article.

 

VEXcode IQ blocks project that uses a Color Sensor to drive forwards until it finds an object. The project reads When started, drive forward, wait until Color3 found an object, and then stop driving.

In this example, the <Found an object> block is used to detect proximity along with a [Wait until] block, to make the robot drive forward until an object is detected.

VEXcode IQ Color detects block that reads Color3 detects green?

The <Color detects> block is also a Boolean reporter block that reports a condition as either true or false. The <Color detects> block reports 'true' if the 'true' if the Color sensor detects the selected color and 'false' if the sensor detects any other color. To learn more about Boolean blocks visit the Help or the Block Shapes and Meaning article.

 

VEXcode IQ blocks project that uses a Color Sensor to drive forwards until it detects the color green. The project reads When started, drive forward, wait until Color3 detects green, and then stop driving.

In this example, the <Color detects> block is used along with a [Wait until] block to cause the robot to drive forward until the Color Sensor detects a green object. The robot will then stop driving. This is illustrated in the first video above.

VEXcode IQ Brightness of block that reads Color3 brightness in %.

The (Brightness of) block reports the amount of light detected by the Color Sensor. It is a reporter block used inside other blocks with circular spaces.

The (Brightness of) block reports a range of 0% to 100%.

 

VEXcode IQ blocks project that uses a downwards-facing Color Sensor to follow a marked line. The project reads When started, set Color3 light to 25%. Next, forever if Color3 brightness in % is greater than 25 then spin LeftMotor forward for 35 degrees, else spin RightMotor forward for 35 degrees.

In this example, the (Brightness of) block is used to have the robot detect and follow a line, as shown in the second video above.

VEXcode IQ Hue of block that reads Color3 hue in degrees.

The (Hue of) block reports the hue of the color detected by the Color Sensor. It is a reporter block used inside other blocks with circular spaces.

The (Brightness of) block reports a range of 0 to 360.

 

VEXcode IQ blocks project that uses an Optical Sensor to drive forward until it reaches an object, and then turn right if it's red or left if it isn't. The project reads When started, print Color3 hue in degrees on Brain, drive forward, and then wait until Color3 found an object. If Color3 hue in degrees is greater than 330 and Color3 hue in degrees is less than 30 then turn right for 90 degrees, else turn left for 90 degrees.

In this example, the (Hue of) block is used to have the robot check a range of hue values that correspond to the color red, and to turn right for 90 degrees if those values are detected by the sensor. If any other hue values are detected, the robot will turn left for 90 degrees.

The (Hue of) block can be useful when it is necessary for the robot to detect certain colors when ambient light conditions may be inconsistent.

Coding the Color Sensor in Python

Note: To code an VEX IQ (1st generation) Bumper Switch in Python, it must be connected to a VEX IQ (2nd generation) Brain. The VEX IQ (1st generation) Brain does not support Python.

color_3.is_near_object()

The color.is_near_object command reports True when a Color sensor detects an object or surface close to the front of the sensor and False when a Color Sensor does not detect an object or surface close to the front of the sensor.

Note: The name of the Color Sensor that appears in the command corresponds to the name it is given in the configuration. 

drivetrain.drive(FORWARD)
while not color_3.is_near_object():
wait(20, MSEC)
drivetrain.stop()
In this example, a While loop with a not condition is used with the color.is_near_object command to make the robot drive forward until the the Color Sensor detects an object close to the front of the sensor.
color_3.color()

The color.color command reports the color currently being detected by the Color Sensor.

drivetrain.drive(FORWARD)
while not color_3.GREEN():
wait(20, MSEC)
drivetrain.stop()
In this example, a While loop with a not condition is used with the color.color command to make the robot drive forward until the Color Sensor detects a green object. The robot will then stop driving. This is illustrated in the first video above.
color_3.brightness()

The color.brightness command reports the amount of light detected by the Color Sensor. It reports a range of values from 0% - 100%.

color_3.set_light(25, PERCENT)
while True:
if color_3.brightness() > 25:
# If the Color Sensor detects light, then the robot will shift left
LeftMotor.spin_for(FORWARD, 35, DEGREES)
else:
# If the Color Sensor detects darkness, then the robot will shift right
RightMotor.spin_for(FORWARD, 35, DEGREES)
wait(20, MSEC)
In this example, the color.brightness command is used to have the robot detect and follow a line, as shown in the second video above.
color_3.hue()

The color.hue command reports the hue of the color detected by the Color Sensor. It reports a range of hue values from 0 to 360.

brain.screen.print(color_3.hue())
drivetrain.drive(FORWARD)
while not color_3.is_near_object():
wait(20, MSEC)
if color_3.hue() > 330 and color_3.hue() < 30:
drivetrain.turn_for(RIGHT, 90, DEGREES)
else:
drivetrain.turn_for(LEFT, 90, DEGREES)

In this example, the color.hue command is used to have the robot check a range of hue values that correspond to the color red, and to turn right for 90 degrees if those values are detected by the sensor. If any other hue values are detected, the robot will turn left for 90 degrees.

The color.hue command  can be useful when it is necessary for the robot to detect certain colors when ambient light conditions may be inconsistent.

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: