This page will show you how to use the TCS34725 RGB Color Sensor from DF Robot.
This sensor makes use of an Arduino library called DFRobot_TCS34725 which takes most of the hard work out of using the sensor.
To get a copy of this library click on this link
Open the green Code tab and you will see the option to download the library as a zip file.
Click that and the file will show up in your downloads folder.
Copy the zip folder into your libraries folder inside the Arduino folder in your Document folder. No need to unzip it. Next time you start Arduino it will be available.
Connect the colour sensor to your Arduino like this:
Now you need an Arduino file called get_rgb_values. Click here to download file (A screen will open which allows you to download it. Click download icon at top of screen. Look in your Downloads folder to see the downloaded file (if it is not already on your screen) Extract file if necessary and then double click it. If you are lucky it will open straight away, but you may be asked to create a folder to put the file in. If so, just click YES and carry on.
NOTE: From 2024 the colour sensor library has been updated. Here is a version of get_rgb_values that will work with the updated library: new_get_rgb_values
This program will set up the colour sensor and start reading values from it.
You will need to open the Serial Monitor to see the values printed out.
This program assigns red, blue and green values to the variables r, b and g. These tell you how much of each colour the sensor is seeing. However, we want to go further and get the program to identify the colour it is looking at.
Here are some made up examples of r,g,b readings looking at a red spot, and a green line.
Red spot r = 107 g = 59 b= 86
Green line: r = 40, g = 108, b = 96
Form this we can see that if r > 80, the colour is red,
and if g > 90 the colour is green.
Here is some pseudo-code to capture the above ideas:
if (r >80)
colour is red
if ( (g > 90)
colour is green
We can create a variable called colour to store the colour we measure.
Let's make it so that if the colour is red, colour is set to 1; and if the colour is green, colour is set to 2,
Here is some code to do that.
if ( ( r > 80)
{
colour = 1; //set colour to 1
Serial.println(colour);
}
else if ( (g >90)
{
colour = 2; //set colour to 2
Serial.println(colour);
}
else
Serial.println ("No colour detected");
Add this code to the bottom of the Get_rgb_values program, and it should print 1 if it detects the red spot and 2 for a green line.
Now we want to create a function called colour() that will work out what colour is detected whenever we write the word colour().
Note that colour() is a function and colour is a variable.
The function colour() looks like this:
Notice that on the last line, this function returns an answer (hue), and this answer is 1 if the colour (hue) is red and 2 if the colour (hue) is green.
To use this function in your program you have to type it in, at the bottom, after you main loop has closed.
Then, when you want to "call" or use the function in your main loop(), your just write its name, like this:
The colour() function is called near the end of this program, and you can see that the Arduino takes different actions depending on the colour that comes back from the colour() function.
This is exactly how you want to use the colour() function. Whenever the sensor notices green you want to make the robot turn and face towards the red spot, and when the colour goes red you want to turn and face in the next direction.
Here is some pseudo-code for using the colour sensor to navigate around the course.
while(colour() is not green)
head towards the green line
turn to face the red spot
while (colour() is not red)
head towards the red spot
turn to face next green line
etc
Of course, heading towards something means using the compass sensor, so the use of the compass sensor is hidden inside the 'head towards' statement. So before you can combine the compass and colour sensors, you need to learn how to make the robot 'head towards' a target. You can find instructions for that on the LIS3MDL Compass page.