MISSION OBJECTIVE
Learn to create a secret coded message with color.
Help Piperbot and Pip get into Maker’s City by decoding the color sequence from the color wheel. Ask: “How does the Color Sensor interpret color from light that bounces back to the sensor? Can it tell between different shades of the same color?
View student interface at make.playpiper.comMISSION CHARACTERS
MISSION MATERIALS
Computer with USB port and Chrome or Edge browser
Piper Make Base Station or Starter Kit
Color Sensor
MISSION RESOURCES
Learning Goals
- Students will use the Color Sensor to “read” and report colors and to map colors to different words/letters to create a secret code to share.
- Students will understand the coding elements of if/then statements and interface with the Color Sensor using different graphical views on Piper Make.
- Students will understand the fundamental concepts of how they perceive color in the world around them.
Career Connections
Robotics Engineer: Salary $104,600/yr
Automotive Engineer: Salary $95,300/yr
Civil Engineer: Salary $88,050/yr
Electro-Mechanical and Mechtronics Technicians: Salary $60,570/yr
Hardware Diagram
Code Diagram
Tutorial Steps
Color CodedYou're going to need a Color sensor for this project—which you might have on hand if you're one of our earliest Monthly Makers Club members OR if you have an Extended Starter Pack. If not, don't worry -- head over to playpiper.com and search for the Sensor Explorer to get three snazzy sensors.
If you've got your Color sensor though, let's get started! We're going to write some super secret messages using color!
If you've got your Color sensor though, let's get started! We're going to write some super secret messages using color!
Before we start using our sensor, let's go over how we normally see color and how our sensor tries to copy that.
Think about the shirt you have on right now and pick a color on there. It's pretty easy to tell what color it is right? But what if I told you that color was actually what our eyes processed after white light hit your shirt and only the color you see was bounced off?
White light is a mix of every color and most objects reflect only a few colors back - the rest are absorbed.
If you're wearing a blue shirt, the red wavelengths and the green wavelengths and so on all got absorbed by the shirt and only the blue reflected back! Our eyes detect that using cells in the back of the eye called rods and cones.
Think about the shirt you have on right now and pick a color on there. It's pretty easy to tell what color it is right? But what if I told you that color was actually what our eyes processed after white light hit your shirt and only the color you see was bounced off?
White light is a mix of every color and most objects reflect only a few colors back - the rest are absorbed.
If you're wearing a blue shirt, the red wavelengths and the green wavelengths and so on all got absorbed by the shirt and only the blue reflected back! Our eyes detect that using cells in the back of the eye called rods and cones.
The Color sensor works this way, too. It emits a white light and then measures the red, green, and blue wavelengths that bounce back. Based on the amount of red, green, and blue light that bounces back, we can figure out what color the sensor is "looking at."
Click NEXT.
Click NEXT.
Let's start by connecting our Color sensor.
The colors of the wires on your 4-pin cable can vary, so it is important to match the pins on your sensor module to the correct connections on your Breadboard.
Remember, the DIGITAL VIEW is a handy way to see the GPIO pin locations. For this circuit, you will connect:
Important note: If your Color sensor has a switch on the side of the sensor, you MUST turn on to generate the white light that the Color sensor uses to sense which wavelengths of light reflect back to the sensor.
You'll see the light turn on when everything is wired up correctly and the switch is in the correct position.
The colors of the wires on your 4-pin cable can vary, so it is important to match the pins on your sensor module to the correct connections on your Breadboard.
Remember, the DIGITAL VIEW is a handy way to see the GPIO pin locations. For this circuit, you will connect:
- GND to G (ground)
- SDA to GP20
- SCL to GP21
- VCC to 3V
Important note: If your Color sensor has a switch on the side of the sensor, you MUST turn on to generate the white light that the Color sensor uses to sense which wavelengths of light reflect back to the sensor.
You'll see the light turn on when everything is wired up correctly and the switch is in the correct position.
Let's start by getting our Color sensor to do its thing—namely, sensing color! Most colors can be coded using combinations of Red, Green, or Blue. These are called RGB values.
Let's start by storing the RGB value from the Color sensor block into a Variable. This will let us use the value several times in our program without having to read the sensor again each time.
Click the Create variable button in the Variables menu and name it "sensorColor".
Drag out the set sensorColor block from the variables menu and place the color sensor block from the Sensing menu in there.
Let's start by storing the RGB value from the Color sensor block into a Variable. This will let us use the value several times in our program without having to read the sensor again each time.
Click the Create variable button in the Variables menu and name it "sensorColor".
Drag out the set sensorColor block from the variables menu and place the color sensor block from the Sensing menu in there.
Click NEXT.
We want to be able to use the DATA visualizer to look at the data from the Color sensor. This will let us see the same color the sensor is seeing.
To send data to the DATA tab at the bottom of the screen, we need to use a graph block from the Actions menu.
What do you usually think of when you think of a graph? Is it a bunch of bars or maybe points like this?
To send data to the DATA tab at the bottom of the screen, we need to use a graph block from the Actions menu.
What do you usually think of when you think of a graph? Is it a bunch of bars or maybe points like this?
On the graph block, select color data from the menu and then place it below your variable block. Drag out a sensorColor block from the Variable menu and place it in the graph block.
Then, place the set sensorColor and graph color data blocks in a repeat forever block from the Loops menu. Finally, change the wait time on the loop to 1 second.
Then, place the set sensorColor and graph color data blocks in a repeat forever block from the Loops menu. Finally, change the wait time on the loop to 1 second.
Click NEXT.
Open the DATA tab at the bottom of the screen. Then, click the CONNECT and START buttons to run your program.
If you lightly press your Color sensor against a colored piece of paper or other object, the graph in the DATA tab will display the color that the Color sensor is seeing. It will also show the RGB values, and the individual red, green, and blue sensor readings:
If you lightly press your Color sensor against a colored piece of paper or other object, the graph in the DATA tab will display the color that the Color sensor is seeing. It will also show the RGB values, and the individual red, green, and blue sensor readings:
Click STOP, then click NEXT.
Great! Now that we have our variable, where the data will come from, and how the data will be shown, we have to set up our program to interpret the data for us.
To do this, we're going to compare the RGB values coming in from the sensor to known RGB values of different colors. Basically, If the RGB values are close to the RGB values for pink, we want our program to say "Pink".
To do this, we're going to compare the RGB values coming in from the sensor to known RGB values of different colors. Basically, If the RGB values are close to the RGB values for pink, we want our program to say "Pink".
Do you remember when you made an if-else if block in the Reaction Game tutorial?
We want our program to be able to identify five different colors, so we are going to build an if-else if-else if-else if-else if block!
To start, click the blue gear icon on the if block. A new menu will appear. Drag an else if block straight over and connect it under the if. Do this three more times:
We want our program to be able to identify five different colors, so we are going to build an if-else if-else if-else if-else if block!
To start, click the blue gear icon on the if block. A new menu will appear. Drag an else if block straight over and connect it under the if. Do this three more times:
Click the blue gear icon again to close the menu, then click NEXT.
Now let's get our program to interpret those RGB values for us!
Let's start by comparing an RGB value from a specific color (like Pink!) to the RGB value from the sensor. Drag out a compare _ to _ block from the Logic menu. and place it in the first opening of the _ = _ block in the if block:
Let's start by comparing an RGB value from a specific color (like Pink!) to the RGB value from the sensor. Drag out a compare _ to _ block from the Logic menu. and place it in the first opening of the _ = _ block in the if block:
Next, we want to compare a color block from the Values menu to the "sensorColor" variable we set up before. Drag those blocks into the compare block. Then drag out a number value block from the Values menu and add it to the other side of the _ = _ block.
Click NEXT.
If the RGB values are more than 70% similar, it's a pretty good bet that the color being sensed is the one defined in your color block.
To test if the color is similar enough, change the "=" to a greater than ">" and change 0 to 70:
To test if the color is similar enough, change the "=" to a greater than ">" and change 0 to 70:
Now go ahead and change that color block to red. Now if the color "red" block and the sensorColor block are similar enough, then you're probably sensing something red!
Let's drag out a print statement and make the program print "red" when something red is detected:
Let's drag out a print statement and make the program print "red" when something red is detected:
Click NEXT.
Let's do the same thing for four other colors—blue, green, yellow, and pink. Duplicate the blocks and add them into the rest of the big if block.
Change the colors in the color blocks first. Then go ahead and change the print statements and label to say "Yellow", "Blue", "Green", "Pink":
Change the colors in the color blocks first. Then go ahead and change the print statements and label to say "Yellow", "Blue", "Green", "Pink":
When your code looks like the picture above, you're ready to rock! Click NEXT.
Open the CONSOLE tab at the bottom of the screen.
Click START and find things that you can scan with your Color sensor. Is your Color sensor able to match the color you see?
If not, you may need to adjust the colors in the color blocks - you can also use the DATA tab to see what color the Color sensor is seeing to help you make those adjustments!
When you're happy with your program, click STOP, then click NEXT.
Click START and find things that you can scan with your Color sensor. Is your Color sensor able to match the color you see?
If not, you may need to adjust the colors in the color blocks - you can also use the DATA tab to see what color the Color sensor is seeing to help you make those adjustments!
When you're happy with your program, click STOP, then click NEXT.
Remember that the thing being printed in the console is coded by your print statements, which you can change around to spell out different things if you would like. Map different words onto different colors, like "hello" onto red and "world" onto world.
You can make your program "say" a whole sentence by scanning colors in the right order!
You can make your program "say" a whole sentence by scanning colors in the right order!
After you've changed the print statements, click NEXT.
Now click START and put your sensor over something red and then over something blue. Do you see the readout on your console as "Hello World"?
What other coded messages can you create?
Click EXIT to return to the home screen and try out more tutorials!
What other coded messages can you create?
Click EXIT to return to the home screen and try out more tutorials!
Blueprint
OPEN IN BLUEPRINT VIEWER DOWNLOAD
Have an different version? Look for more information on the Support Page.
Troubleshooting Tips
-
Is the Color Sensor not detecting color?
- Ensure that the Color Sensor is on. A small switch on the Sensor must be on for it to work (you will see a white LED turn on when this is correct).
- Ensure that you’ve plugged the wires into the right GPIO. Look at the Digital View to see which pin you’re affecting.
-
How does the Color Sensor work?
- Color Sensors emit a white light and then measure the red, green, and blue wavelengths that bounce back. Based on the amount of red, green, and blue light that bounces back, we can figure out what color the Sensor looks at.
Our customer support specialists are on hand to ensure your implementation of Piper runs seamlessly.
View Support Docs or Contact Support
EXPANDED RESOURCES
Term Glossary
Engineer Someone who designs, builds, or fixes things to solve problems.
Sensor A device that produces an output signal to sense a physical phenomenon.
Conditional Statement A coding instruction used to compare values and express and make decisions, like an if-then statement.
List A sequence of several values grouped under a single name in code. A group of items or components. A tool used to store multiple pieces of information at once.
Standards Alignment
Individual Standards
- CA 3-5.CS.1 Describe how computing devices connect to other components to form a system. (P7.2)
- 3-5.AP.18 Perform different roles when collaborating with peers during the design, implementation, and review stages of program development.

