# Hummingbird Color Sensor

Learn how to create your own color sensor for the Hummingbird.

#### Programming Language

Snap! Level 4, Scratch

#### Teacher Materials

Materials available for teachers.

Have you ever wished that there was a color sensor for the Hummingbird? Then we have good news – you can make one yourself! First, one caveat – this is a fairly advanced topic. Before you try it, you will want to make sure that you are comfortable with these topics in Scratch or Snap: nested if else blocks, Boolean statements using and and or, variables, and creating your own blocks.

### Making a Color Sensor

To make a color sensor, you will need a tricolor LED and a light sensor. You can determine the color of a surface by measuring the amount of red, green, and blue light that the surface reflects. Imagine that you are trying to identify a red surface. When you shine a red light at the surface, a lot of light will be reflected. When you shine a green or blue light at the surface, only a little light will be reflected.

Important Note: It is harder to identify the color of a shiny surface. We recommend using construction paper for this tutorial.

To create a color sensor, first wrap electrical tape around the sides of the LED and sensor. This will help ensure that the light must reflect off the surface before it reaches the sensor. Be sure not to cover the end of the LED or sensor! Next, tape the two components together to fix the position of the sensor relative to the LED. Connect the tricolor LED and light sensor to your Hummingbird board.

### Programming with a Color Sensor

Now it is time to use Scratch (or Snap) to write a program to identify colors. Start by creating a block to calibrate your sensor. You will calibrate the color sensor by measuring the amount of red, green, and blue light reflected from a piece of black construction paper. A black surface should reflect very little light of any color. This means that the values we measure for a black surface are essentially noise caused by excess light shining on our sensor. Calibration helps us to remove this noise.

To calibrate the sensor, you want to flash the LED red, then green, then blue. For each color, you should measure the amount of light reflected. In the program below, the reflected light is saved in the variables blackR, blackG, and blackB. There is a short wait between each HB triLED block and the corresponding set block. This is to ensure that the LED is the right color before measuring the reflected light.

Once you have calibrated the color sensor, you want to create another block that identifies the color of a surface. The first part of this is very similar. You want to measure the amount of red, green, and blue light that is reflected. The only difference is that when you measure the reflected light, you want to subtract the calibration value for that color. For instance, when the red light is shining, you want to subtract blackR from the value of the light sensor to measure the amount of reflected red light. This value is saved in the variable R.

Now you have the amount of reflected red, green, and blue light. You can use these to determine the color of the surface. This requires some advanced Boolean logic and nested if else blocks. It looks complicated, but we will take it one step at a time.

If one of the reflected light variables (R, G, and B) is negative, this means that no surface is close enough to the sensor to reflect light. In this case, we set the variable color to the value “none.” Because we want to check if ANY of the variables is negative, we use or blocks in the Boolean expression.

If none of the variables are negative, we move to the else portion of the first if else. Here we have another if else. We know that the surface is black if all of the reflected light values are close to zero. The calibration process ensures that this is true. We check that each variable is less than 5. These statements are combined with and blocks because ALL of the values must be less than 5 for a black surface.

If the surface is not black, we move to the else portion of the second if else. Now we know that the surface must be red, green, or blue (because we have decided those are the only colors we are dealing with right now). If the red value is the largest, then the surface must be red. This is checked in the third if else block.

If the surface is not red, we move to the else portion of the third if else. The surface must be green or blue. If G is larger than B, then the surface is green. Otherwise, it is blue.

Now that you have created these two blocks, you can use your color sensor in a wide variety of projects! The simple program shown below will identify the color or a surface; sample code is given in ColorSensor.sb2 (link above).

The video below shows a project where different colored cards control the robot. Each color triggers a different fact about lemurs and a different action by the robot. By using colored cards, you can make your robot identify different people, or you can create a smart dollhouse that recognizes different inhabitants. Be sure to share what you make!

### Identifying Different Colors

So far, this tutorial has demonstrated how to make a color sensor that can identify four colors: red, green, blue, and black. It is possible to use the same sensor to identify more colors, as shown in the second half of the Hummingbird Color Sensor video. However, this requires more complex program logic. For example, the sample code given in ColorSensor2.sb2 uses ten if and if else blocks instead of the four shown in the code above. ColorSensor2.sb2 can identify eight colors: black, red, green, blue, yellow, purple, turquoise, and white.

This sample code identifies the combination colors yellow, purple, turquoise, and white by looking for combinations of RGB values. For instance, purple reflects a lot of red and blue but does not reflect much green. Six Boolean variables are used to determine the color. redHigh, greenHigh, and blueHigh are true when these colors are above a threshold. redCloseBlue, on the other hand, is true when the red and blue values are close to each other. redCloseGreen and greenCloseBlue are defined similarly.

A portion of the decision statements that determine the color is shown below. These are the statements that are executed when redHigh is true. If the red value is close to both the blue value and the green value, then we know that all three values are high. This means that the color must be white. If the red value is close to green but not blue, then the color must be yellow. The outer else deals with the cases where red is not close to green. If red is close to blue, then the color is purple. Otherwise, the color is red if the red value is larger than the green and blue values. This innermost if block ensures that a combination of colors such as R = 41, G = 77, and B = 20 is not misidentified as red. Can you write a program that identifies more than eight colors?