Basic Color Sensor Programming


​The color sensor detects colors in the environment. You can use it to program your VEX IQ robot to respond to different colors. For example, you can make the robot STOP when detecting the color red, and GO when detecting the color Green. In this tutorial, you will learn about the color sensor. You will use the default ‘3-color mode’ to detect green, red, and blue, and print messages based on the color detected.

Hardware required

  • VEX IQ Brain
  • Color Sensor

Program

Let's create a program that prints the detected color onto the VEX IQ brain.

Open the Modkit for VEX editor. Drag and drop a color sensor and assign its port number. Make sure the port number matches that of the physical color sensor.

Next, click on the 'Blocks' tab and make sure that you editing the 'Brain'. Drag and drop blocks of code to match the image below. The 'when START' block runs once each time your program runs. The ‘forever’ block repeatedly runs the code inside, allowing your program to change and respond as it runs.

Download your program, and run it on the physical VEX IQ brain. We've provided a graphic that you can use to test your program (see attachments below); simply print out the color wheel (page 1). Point the color sensor at the color wheel and observe the color printed on the VEX IQ brain. The default mode of the color sensor is '3-color'. The benefit of using this mode is that it easily distinguishes between the main colors red, green, and blue. The limitation is that it does not recognize colors such as yellow, orange, and purple.

Next, let's modify the program to perform different actions based on the color it detects. Drag and drop code blocks to match the image below. If the color sensor detects GREEN when the program is run, then the message "Ready to go!" will be printed. Otherwise, the message "Waiting for green signal..." will be printed.

Download your program, and run it on the physical VEX IQ brain. We've provided a second graphic that you can use to test your program (see attachments below); simply print out the green go! circle (page 2).

In this tutorial, you used the color sensor to detect colors in the environment and printed messages based on the detected color.There is so much more that you can do! Check out our other Getting Started guides.

Updated 47 months ago

What You'll Need