Print

Hummingbird Color Sensor

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

Hummingbird Color Sensor

Programming Language

Snap!, BirdBlox, MakeCode / JavaScript

Objective & Learning Goals

Students will create a color sensor using the Hummingbird Tri-Color LED and light sensor.

Standards

COMPUTER SCIENCE TEACHERS’ ASSOCIATION (CSTA)

CSTA Standards are split into different grade levels: 3-5, 6-8, and 9-10. Working with the Hummingbird Robotics Kit meets multiple standards across these grade-level delegations.
Visit this page for a more detailed explanation of how working with the Hummingbird Robotics Kit applies to meeting CSTA Standards.

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: nested if else blocks, Boolean statements using and and or, variables, and creating your own blocks. The code shown here will be in Snap!, but you can use similar code to make a color sensor in BirdBlox or MakeCode.

Making a Color Sensor

To make a color sensor, you will need a tri-color 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 felt or construction paper to test your sensor.

To create a color sensor, first wrap electrical tape around the sides of the tri-color LED. 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!

Next, connect the two components to a piece of cardboard. They must be as close together as possible, and the end of the LED should be level with the end of the light sensor. Here, we have used a metal brad to connect the light sensor to the cardboard.

Programming with a Color Sensor

Now it is time to write a program to identify colors. Start by creating a block named Read Color and three variables named R, G, and B. In the Read Color block, you want to flash the LED red, then green, then blue. For each color, you should measure the amount of light reflected and save it in the appropriate variable. In the program below, there is a short wait between each Hummingbird Tri-LED block and the corresponding set block. This is to ensure that the LED is the right color before measuring the reflected light.

Once you use the Read Color block in your program, the R, G, and B variables contain the amount of reflected red, green, and blue light. You can use these variables to determine the color of the surface. Here we will examine some code to distinguish red, green, blue, and black surfaces.

If all of the reflected light variables (R, G, and B) have small values, this means that the surface doesn’t reflect a lot of light of any color. This corresponds to the color black.

If the surface isn’t black, it must be red, green, or blue. If the B variable is larger than both the R and G variables, we will decide that the surface is blue.

If the surface isn’t black or blue, then we know that it must be red or green. If G is bigger than R, it is green. Otherwise, the surface is red.

You can also use more complex logic to distinguish other colors. For example, a yellow surface will reflect high levels of both red and green light.

When testing your color sensor, keep in mind that the sensor must be very close to the surface (less than one centimeter) to detect the color.

Now you are ready to use your color sensor in a project. The video below shows a shows a robot that sorts candy by color, but you can create all sorts of things!