Interfacing a TCS34725 RGB Color Sensor With Arduino – A Complete Guide
Introduction:
An RGB Color sensor accurately detects an object’s color. There are a lot of uses for color sensors: sorting packages based on color, detecting the freshness of perishable items, correcting lighting color, and controlling light based on screen content. This tutorial will help you set up the TCS34725 RGB Sensor for Arduino boards. We’re using Arduino UNO here. You’ll also find example code to communicate with the RGB color sensor.
What Is An RGB Sensor?
RGB stands for Red, Green, and Blue. An RGB sensor can independently detect the color intensity of red, green, and blue colors and measure brightness. The sensor does this by using an RGB color filter at it’s lens-for example, the red color filter allows only red light to pass through it.
Since the three colors can be used in different proportions to create many colors, RGB sensors can detect several colors using just those three. With an 8-bit RGB color sensor, the red, green, and blue values range from 0 to 255, and 256 * 256 * 256 = ~16 million colors!
Light entering the sensor from the lens falls on a photodiode, whose current output will vary depending on the amount of expose light. The current will be converted to a voltage using a signal conditioner which can be read using an ADC.
Here is the block diagram of the TCS34725 sensor.
The IR block filter in the TCS34725 helps in sensing ambient light. The TCS34725 has an I2C interface (SDA and SCL) that connects to the Arduino Board. The IC also has an optional interrupt output pin which can be used to interrupt Arduino.
The table below gives the pin description of the color sensor IC – TCS34725.
TCS RGB Color Sensor Board with pinout
You can find the datasheet of the TCS34725 color sensor . Component List
Hardware
(for powering Arduino and programming) x 1 Software
Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.
Step-By-Step Instructions To Connect A TCS34725 Color Sensor To Arduino
Here are the step-by-step details to connect the color sensor to your Arduino board. Several versions of the boards are available, but the guide applies to all the variants of the TCS34725 RGB sensor boards.
How To Connect TCS34725 RGB Sensor With Arduino?
Here are the details required to complete the Arduino and the color sensor TCS34725 RGB sensor. The final connection will look like the image below.
Step 1: Unpack and prepare TCS34725 color sensor
There are seven pins available on this particular module. Depending on the module you have, the number of pins may differ. Make sure that you match the pins according to the module.
Step 2: Connect the GND pin on the color sensor module with Arduino
Connect the GND pin of the Arduino (there are multiple GND pins. You can choose one for eases of 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. You can read about the I2C pins and the supported Arduino I2C functions . 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.
This will complete the required connections between the Arduino and the TCS34725 RGB color sensor.
TCS34725 Library Installation And Arduino Code Examples
In this section you’ll install the Adafruit library for the RGB color sensor. This library comes with a host of useful examples which you can easily edit and apply to your projects.
Step 1: Open Library manager
Open the Arduino IDE and 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’ll see the status as “installed”, as shown below.
Step 3: Open the Adafruit example code
You have installed the Adafruit library successfully. Refer to the below image to view the available example codes.
Browse and locate “Adafruit TCS34725” Example Click on the Colorview example. The example will open 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 the Colorview 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 (heloful in driving analog values to the LEDs for varying brightness) by looking at the “~” symbol before the pin label.
I have shown an example here:
The below line initializes the object to handle the color sensor functions.
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
You can read the RGB color values at every fixed interval using the getRGB method, as shown in the code line below.
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 will 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, drop a comment in the comment section.
Sources: