1. Lesson 1 - Color Sensor


In this course, we will construct different military vehicles and constructions. In today's' lesson we will build an Ammo Truck. We will attach to it a new sensor - color sensor. We will learn more about it, how it works and how to program it. Later, we will use it to give different commands to the truck.

Construction and Theory

Build the robot using the instructions below. Pay attention to the way the truck is driven and how it steers. What are the differences with the robots we have built so far?

Once you have finished with the construction of the robot, please attach a color sensor to it.

Clank - LEGO EV3 Ammo Truck

Building instructions for a LEGO Truck. The robot is presented as an Ammo Truck but it can be used for transporting various other objects, too. The construction has a rear drive with each of the rear wheels driven by a Large Lego Motor. The front wheels steer using a Medium Lego Motor. The Brick is placed at the front of the construction which gives the opportunity to easily attach different sensors, based on the task. 

Programming and theory

Before we dive into the color sensor theory and programming, let us start with a few simple robot movement exercises. After we are sure we can easily program the robot to move around, we will continue.

How does the color sensor work?

What is color?

If I ask you what the color of the ball on the picture below is, you will surely answer red without hesitation. All of you know what is meant when we talk about the color of an object, but what is actually color? This is one of the most natural concepts whose meaning and origin people often fail to realize.

Two things are responsible for the colors as we know them - light and our eyes.

For instance the sunlight, which we call white, actually combines all colors. This can be observed using glass triangular prism, as shown below:

Simplified, the difference between the different colors is the amount of energy, the corresponding light ray has. On the table below are shown the different types of light.

The so-called gamma rays "have" the greatest amount of energy, while the radio waves - the least. In the middle, where there are multiple color stripes, we have the light we can see and hence it is called visible light. From the visible light spectrum, the violet light has the greatest amount of energy and the red has the lowest.

We see different objects in different color, depending on what light they absorb and what they reflect. For example, the objects we call black actually absorb all of the light and therefore they absorb a lot of energy. On the other hand, white objects reflect all of the light and absorb far less energy. That is why in the summer we feel hotter wearing black clothes, than wearing white ones.

How do we see colors?

So far we have learned what different colors are. But how do we differentiate them? There are three types of cone cells in our eyes. Each type is sensitive to either red, green or blue light. Each cell sends signals to the brain about the intensity of the light it has detected. Having the information about the amount of red, green and blue in each light ray, our brain is able to "see" different colors.

And how do robots see colors?

Instead of eyes, robots have sensors. Just like people, robots use three types of sensors. Each type detects the intensity of red, green and blue light. Combining the information from the three sensors, the robot can conclude what color it "sees".

LEGO EV3 Color Sensor

The LEGO Color sensor consists of two main parts - a diode which emits light and color sensors which measure the intensity of the reflected light.

How to program the EV3 color sensor

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.

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.

How to check what value the LEGO EV3 color sensor reads?

You can use the brick to check the color the color sensor detects in the following way:

Similarly, you can check the value in reflected light mode:

Programming color code

Now it's time to put all we have learned about the new sensor into practice. We will control the truck using a color code. This means that we will "show" it different colors and, depending on the color, it will execute a different command. For that purpose you will need to construct the same or similar model:

Model to be used for color code

Building instructions for constructing a simple LEGO Model that has red, green, blue and yellow beams. This makes it perfect for operating robots through color code.

Task normal icon Tasks to finish a class

  1. Take pictures and make videos of your robots
  2. Disassemble and arrange the robot
  3. Arrange your workplace
    • It is important to arrange the electronics of the robots on the cover of the box.
    • Wind the programming cable so that it is assembled.
    • Put the robot cables next to the electronics.
    • Wind the mouse cable around the mouse.
    • Wrap the cable of the laptop charger in a way that your teacher will show you.
    • When you turn off the computer, leave it next to the robot box.
  4. Turn off all programs on your computer
  5. Exit FLLCasts  
  6. Shut down your computer
  7. Put your chair under the desk.