How to program the EV3 color sensor Pro Preview

As with the two sensors we have covered so far, the color sensor can be used as a condition for the wait block, the switch block or the loop block. The color sensor, as well as the ultrasonic sensor, has several modes. Nevertheless, these modes are substantially different. While the ultrasonic sensor has two modes - one to work in centimeters and one in inches, the color sensor has three modes - one for detecting the color of an object, one for ambient light and one for reflected light.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #812
  • 30 Mar 2018

If you put a wait block on the canvas, then from the drop-down menu select color sensor and then "Compare", you will see the three available modes:

  • Color - If you choose that mode, the sensor will evaluate the color of an object. We use that mode for solving the Rubik's cube, for sorting balls, for programming color code etc. The diode of the sensor emits red, green and blue light. Depending on the intensity of the reflected light in each of the three light spectrums, the robot can differentiate 8 colors. Each color is encoded as a number:
    0 = No color;
    1 = Black;
    2 = Blue;
    3 = Green;
    4 = Yellow;
    5 = Red;
    6 = White;
    7 = Brown;
  • Reflected Light Intensity - If you choose that mode, the sensor will measure the amount of light reflected. We use that mode, when we want to differentiate lighter from darker objects, as in following a line. Here, the diode of the sensor emits red light and measures the amount of light reflected from the surface of the object. The value, returned by the sensor is between 0 and 100. The greater the number is, the lighter the object is.
  • Ambient Light Intensity - If you choose that mode, the sensor will measure the ambient light. That mode is similar to the mode used in the sensors of lamps that turn on on their own when it gets dark. We can use it to build a robot following a light source. In that case the diode does not emit light. The value returned by the sensor is between 0 and 100. The value is small if the room is dark and bigger if the there is light in the room.

Let us take a look at the settings for each of the modes in greater detail.

Color Mode

As with every other sensor, we start with the port number the sensor is connected to. This is shown by the number in the upper right corner of the block. Afterwards, from the first (and only) field we choose upon which color to wait. Note that you can choose more than one color, hence wait for more than one color. So, for instance, if we want to go forward until we detect a red or black line, we just need to select 1 and 5.

Reflected Light Intensity Mode

Again, we can set the number of the port we have connected the sensor to from the number in the upper right corner of the block. The other two settings of the block are similar to the ones of the ultrasonic sensor. From the first parameter we choose whether to wait for:

  • 0 = the value the sensor reads to be equal to the threshold value;

  • 1 = the value the sensor reads to be different from the threshold value;

  • 2 = the value the sensor reads to be greater than the threshold value;

  • 3 = the value the sensor reads to be greater or equal to the threshold value;

  • 4 = the value the sensor reads to be less than the threshold value;

  • 5 = the value the sensor reads to be less than or equal to the threshold value;

From the next parameter, we can set the threshold value with which we compare the current read value of the sensor.

So, if you want to program the robot to move forward until it detects a black line and the sensor reads 23 on black and 48 on the mat, then you need to set the block to wait until the value is less than 35.