RGB Color Sensor using TCS34725 Module
01 Overview
Color sensor? No problem — the TCS34725 is one of the best color sensors on the market. It has RGB (Red, Green, Blue) and clear light sensing elements, along with an infrared blocking filter integrated on the chip that minimizes the infrared spectral components of the light, allowing accurate color measurement even under varied lighting conditions.
In this activity, you will learn how the TCS34725 works with the help of some electronics components. This is a pretty straightforward exercise that helps you easily understand how the TCS34725 works.
Project Use Case
This project is a good starting point for anyone wanting to add color detection to a project — from sorting objects by color to building custom RGB LED effects that react to a scanned color.
02 Hardware and Software Components
Gather everything below before you start.
Hardware Components
| Component | Description |
|---|---|
| Arduino UNO | Main microcontroller that reads the TCS34725 and drives the RGB LED. |
| TCS34725 Color Sensor | Light-to-digital converter that detects RGB and clear light values. |
| Breadboard | Platform for building and testing the sensor circuit. |
| LED | Displays the color detected by the sensor. |
| 3x 220 Ohm Resistor | Current-limiting resistors for the RGB LED's red, green, and blue pins. |
| Jumper Wires | Connects the sensor and LED to the Arduino and breadboard. |
Software Tools
| Software | Description |
|---|---|
| Arduino IDE | Used to write, compile, and upload the sketch to the Arduino UNO. |
Libraries Used
| Library | Purpose |
|---|---|
| Adafruit_TCS34725.h | Driver library for reading RGB and clear light data from the TCS34725. |
| Wire.h | Enables I2C communication between the Arduino and the TCS34725. |
03 Application Discussion
This project tests how the TCS34725 works with the help of some electronic components. The TCS34725 is a light-to-digital converter that contains a 3x4 photodiode array composed of red-filtered, green-filtered, blue-filtered, and clear (unfiltered) photodiodes. It also has a photodiode coated with an IR-blocking filter — a filter that passes infrared light while completely blocking other wavelengths — which blocks infrared light for use under varied light conditions. In short, it's a device that provides a digital readout of RGB (Red, Green, Blue) and clear light sensing values.
The basic application built here uses the TCS34725 to detect RGB colors and reproduce them on an RGB LED.
04 Hardware Setup
Wire the TCS34725 to the Arduino UNO over I2C, and connect the RGB LED's red, green, and blue pins through 220 Ohm resistors to three PWM-capable digital pins (this project uses pins 3, 5, and 6 — see the Code Breakdown section).
commonAnode to false in the code (see Code Breakdown).
05 Software Setup
Follow these steps before uploading the sketch.
Step 1 — Install the Libraries
Open Arduino IDE → Sketch → Include Library → Manage Libraries, then search for and install Adafruit TCS34725. The Wire library is included with the Arduino IDE by default.
Step 2 — Upload the Code
- Open the Arduino IDE and paste in the code from the Code section below.
- Select Tools → Board → Arduino Uno and the correct COM port.
- Click Verify to compile, then click Upload.
- Open the Serial Monitor at 9600 baud to view the color readings.
06 Code
Copy the file below into the Arduino IDE as described in the Software Setup section. Read the Code Breakdown section to understand what each part does.
rgb_color_sensor.ino
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#define redpin 3
#define greenpin 5
#define bluepin 6
#define commonAnode true
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);
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() {
uint16_t clear, red, green, blue;
tcs.setInterrupt(false); // turn on LED
delay(60);
tcs.getRawData(&red, &green, &blue, &clear);
tcs.setInterrupt(true);
Serial.print("C:\t"); Serial.print(clear);
Serial.print("\tR:\t"); Serial.print(red);
Serial.print("\tG:\t"); Serial.print(green);
Serial.print("\tB:\t"); Serial.print(blue);
// Figure out some basic hex code for visualization
uint32_t sum = clear;
float r, g, b;
r = red; r /= sum;
g = green; g /= sum;
b = blue; b /= sum;
r *= 256; g *= 256; b *= 256;
Serial.print("\t");
Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
Serial.println();
//Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" "); Serial.println((int)b );
analogWrite(redpin, gammatable[(int)r]);
analogWrite(greenpin, gammatable[(int)g]);
analogWrite(bluepin, gammatable[(int)b]);
}
07 Code Breakdown
Here is what each part of the code does.
Pin Definitions
#define redpin 3
#define greenpin 5
#define bluepin 6
This is where you assign the RGB LED's pins on the Arduino. Here, pin 3 is the red pin, pin 5 is the green pin, and pin 6 is the blue pin. Since a common-anode RGB LED is used, its common pin connects to VCC — if you use a common-cathode LED instead, that pin should be grounded.
Common Anode Setting
#define commonAnode true
If you're using a common-anode RGB LED, set this to true. If you're using a common-cathode RGB LED, set it to false.
LED Pin Modes
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
This assigns the RGB LED to be the output driven by the sensor readings — these three pins are used to drive the LED.
Writing the Color to the LED
analogWrite(redpin, gammatable[(int)r]);
analogWrite(greenpin, gammatable[(int)g]);
analogWrite(bluepin, gammatable[(int)b]);
analogWrite() is used here because the output can be any value in a range, unlike digitalWrite(), which only outputs HIGH or LOW.
08 Conclusion
There are many applications for RGB sensors, especially in industry. Many mango-packing facilities use color sensors like this one to sort ripe mangoes (yellow) from raw ones (green), and many vegetable-sorting companies use color sensors for similar separation by color. This project is a simple starting point — understanding how the TCS34725 reads and reports color opens the door to a wide range of sorting, detection, and feedback applications.
09 References
-
TCS34725 Datasheet — Adafruit
(https://cdn-shop.adafruit.com/datasheets/TCS34725.pdf) -
Adafruit TCS34725 RGB Color Sensor — Product Page
(https://www.adafruit.com/product/1334)
10 Project Authors
- Kevin Montenegro
- Lukie-Mar Lonzaga
- Miles Darren Bagnol
- Elijah Joseph Orias
