Skip to content
Oasis-Knowledge
Sensors

icon picker
TCS-34725: RGB Color Recognition

Interfacing a TCS34725 RGB Color Sensor With Arduino – A Complete Guide

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.
What Is An RGB Sensor
Here is the block diagram of the TCS34725 sensor.
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.
Color sensor TCS34725
The table below gives the pin description of the color sensor IC – TCS34725.
TCS RGB Color Sensor Board with pinout
TCS RGB Color Sensor Board with pinout
Table 2
Pin number
Pin Name
Pin Type
Description
1
1
VDD
Power
Supply voltage
2
2
SCL
Input
I2C Clock signal
3
3
GND
Power
Supply ground reference
4
4
NC
Do not connect
5
5
INT
Output
Open drain interrupt signal
6
6
SDA
Input/Output
I2C Data signal
There are no rows in this table
You can find the datasheet of the TCS34725 color sensor
.

Component List

Hardware

x 3
(for powering Arduino and programming) x 1
x 1
(optional)

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.
Connect A TCS34725 Color Sensor To Arduino

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.
How To Connect TCS34725 RGB Sensor With Arduino

Step 1: Unpack and prepare TCS34725 color sensor

Let us begin with the 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.
Connect the GND pin on the color sensor module with Arduino
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.
Connect the I2C data line next

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)
Connect the I2C Clock line
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 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.
Open Library manager

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.
Search for the Adafruit Library
Once installed, you’ll see the status as “installed”, as shown below.
status as installed

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.
Open the Adafruit example code
Click on File
Select Examples option
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
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.