An RGB color sensor allows you to precisely detect colors in your Arduino projects, with various industrial applications as well. They are used in sorting packages by color, assessing the freshness of perishables, correcting lighting colors, and adjusting ambiance lighting based on screen content.
This guide will walk you through creating an Arduino project using the TCS34725 RGB Sensor. You'll find a step-by-step guide for connecting the sensor to an Arduino UNO, along with example code to communicate with the sensor. Let's dive in!
Arduino And TCS34725 RGB Color Sensor Project
Hardware Components
(for powering Arduino and programming) x 1 Software
Step-By-Step Instructions To Connect A TCS34725 Color Sensor To Arduino
The following section gives step-by-step details to connect the color sensor to your Arduino board.
Step 1: Identify the pins you will be connecting.
Several versions of the boards are available, as you can see from the image below. The guide applies to all the variants of the TCS34725 RGB sensor boards.
Step 2: Connect the GND pin on the color sensor module with Arduino
Connect the GND pin of the Arduino (there are many GND pins. You can choose the one which eases the connection) to the GND pin on the RGB sensor.
Always start with the ground connection so both boards will have a common reference before making other connections.
Step 3: Connect the I2C data line next
Connect the Pin A4 on the Arduino to the SDA pin on the color sensor IC.
Step 4: Connect the I2C Clock line
Connect the pin A5 on the Arduino to the SCL pin on the color sensor IC (orange wire in the below picture)
A note about I2C lines. Pins A4 and A5 on the Arduino pins have I2C as one of the functions. If the I2C is used.
Step 5: Connect the power pin
Connect the Sensor pin labelled VIN to the 5 V pin on the Arduino. The RGB color sensor module also provides 3.3 V. You can leave the 3.3 V pin unconnected on the RGB color sensor. The required connections between the Arduino and the TCS34725 RGB color sensor are now complete.
TCS34725 Library Installation And Arduino Code Examples
This section will show how to install the Adafruit library for the RGB color sensor. The library comes with a host of good examples which you can easily edit and apply to your projects. Let’s get started.
Step 1: Open Library manager
Open the Arduino IDE, Click on Tools menu. Select “Manage Library” from the options available.
Step 2: Search for the Adafruit Library
Once you type “TCS” in the search bar, you will find a list of matching libraries available. Select the “Adafruit TCS34725” from the library options.
Click on the install button to install the Arduino Library.
Once installed, you can see the status as “installed”, as shown in the figure below.
Step 3: Open the Adafruit example code
To view the available example code, please refer to the below image of the toolbar.
Clicking this will open the ‘colorview’ example in the IDE. The code is given below:
#include "Wire.h"
#include "Adafruit_TCS34725.h"
// Pick analog outputs, for the UNO these three work well
// use ~560 ohm resistor between Red & Blue, ~1K for green (its brighter)
#define redpin 3
#define greenpin 5
#define bluepin 6
// for a common anode LED, connect the common pin to +5V
// for common cathode, connect the common to ground
// set to false if using a common cathode LED
#define commonAnode true
// our RGB -> eye-recognized gamma color
byte gammatable[256];
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
void setup() {
Serial.begin(9600);
//Serial.println("Color View Test!");
if (tcs.begin()) {
//Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1); // halt!
}
// use these three pins to drive an LED
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
// thanks PhilB for this gamma table!
// it helps convert RGB colors to what humans see
for (int i = 0; i < 256; i++) {
float x = i;
x /= 255;
x = pow(x, 2.5);
x *= 255;
if (commonAnode) {
gammatable[i] = 255 - x;
} else {
gammatable[i] = x;
}
//Serial.println(gammatable[i]);
}
}
void loop() {
float red, green, blue;
tcs.setInterrupt(false); // turn on LED
delay(60); // takes 50ms to read
tcs.getRGB(&red, &green, &blue);
tcs.setInterrupt(true); // turn off LED
Serial.print("R:\t"); Serial.print(int(red));
Serial.print("\tG:\t"); Serial.print(int(green));
Serial.print("\tB:\t"); Serial.print(int(blue));
Serial.print("\n");
analogWrite(redpin, gammatable[(int)red]);
analogWrite(greenpin, gammatable[(int)green]);
analogWrite(bluepin, gammatable[(int)blue]);
}
In is example, you will read the object’s color and drive an RGB LED to represent the detected color. This example uses an additional LED that is connected to Arduino pins, as shown in the code below:
#define redpin 3
#define greenpin 5
#define bluepin 6
The pins 3, 5, and 6 of the Arduino UNO are PWM compatible. You can easily find the pins on the Arduino, which are PWM compatible (helpful in driving analog values to the LEDs for varying brightness) by looking at the “~” symbol before the pin label:
The below lines initializes the object to handle the color sensor functions. You can read the RGB color values at every fixed interval using the getRGB method, which is demonstrated in the next line.
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
tcs.getRGB(&red, &green, &blue);
Once you have the RGB values, you can post the color information on a screen or a terminal or drive the LED with the same color. The terminal window should have data similar to the image shown below.
We are recreating the color of the object using an RGB LED. The below lines will drive the LED to match the detected color.
analogWrite(redpin, gammatable[(int)red]);
analogWrite(greenpin, gammatable[(int)green]);
analogWrite(bluepin, gammatable[(int)blue]);
You can convert this code into a worthwhile project by adding your logic. If you have any questions, leave a comment on the doc!
FAQ’s About The Arduino RGB Color Sensor TCS34725
1) What are the applications?
The applications of RGB color sensors are
RGB LED backlight control Measuring color temperature of ambient light Measuring ambiance light for automatic display control Product color verification and sorting Industrial automation and many more 2) How do you connect Arduino to the RGB Color sensor?
You can use the I2C interface to connect Arduino to the RGB color sensor TCS34275.
Depending on the sensor type, you may have to use UART, I2C, or SPI interface.
In the article, you can see how a TCS34275 is connected to an Arduino using I2C lines.
3) Can Arduino detect colors?
Arduino can detect colors when you use a color sensor along with it.
TCS34275 is an RGB color sensor that lets you quickly see objects’ colors.
The connection details, example code, and working principle of an RGB sensor are explained in the sections above.
5) How many colors can an RGB sensor detect?
The RGB color sensors detect the amount of Red, Green, and Blue colors.
Since the three colors can be used with different proportions to create many colors, RGB sensors too can detect several colors.
With an 8-bit RGB color sensor, the red, green, and blue colors can take any values from 0 to 255.
It means, in total, we can have 256 * 256 * 256 = ~16 million colors!
Hence, you can use color sensors to detect a vivid range of colors in your next color detection project.
Conclusion
This article covers all the critical information needed to build an Arduino color sensor project in this article. Did you have any Challenges building your projects or lessons learned? Please share it with us by commenting on this doc!
References