Color Sensor TCS
Interfacing TCS34725 Color Sensor with ESP32
Interfacing TCS34725 Color Sensor with ESP32 demonstrates how the TCS34725 color sensor can be interfaced with an ESP32 board to detect and identify different colors using RGB and clear-channel data.
The TCS34725 is an RGB color sensor that detects the intensity of red, green, blue, and clear light components.
TCS34725 Color Sensor → I²C → ESP32 → Serial Monitor
Objectives
- To study the working of the TCS34725 color sensor.
- To interface the TCS34725 color sensor with the ESP32 board.
- To read red, green, blue, and clear-channel values from the sensor.
- To identify colors based on the sensor readings.
- To display the sensor readings and detected color on the Serial Monitor.
Software
- Arduino IDE
- Adafruit TCS34725 Library
Hardware Modules
- ESP32 Board
- TCS34725 Color Sensor
- PC / Laptop
- Connecting wires
Theory
Introduction to TCS34725 Color Sensor
The TCS34725 is a digital color sensor used to measure the intensity of different components of visible light.
The sensor provides four raw data channels:
- R – Red channel
- G – Green channel
- B – Blue channel
- C – Clear channel
The sensor communicates with the ESP32 using the I²C communication protocol.
Light → TCS34725 → RGB/Clear Data → ESP32
The RGB values can be compared to determine the approximate color detected by the sensor.
I²C Communication
The TCS34725 communicates with the ESP32 using the I²C interface.
The I²C interface requires two communication lines:
| Signal | ESP32 Pin | Function |
|---|---|---|
| SDA | GPIO 21 | Serial Data |
| SCL | GPIO 22 | Serial Clock |
The SDA line is used for transferring data between the ESP32 and the sensor.
The SCL line provides the clock signal required for I²C communication.
Connections
| TCS34725 Pin | ESP32 | Function |
|---|---|---|
| SDA | GPIO 21 | I²C Data |
| SCL | GPIO 22 | I²C Clock |
| VCC | 3.3V | Power supply |
| GND | GND | Ground |

Sensor Configuration
The TCS34725 sensor is initialized using the Adafruit TCS34725 library.
The program uses the following settings:
| Parameter | Value |
|---|---|
| Integration Time | 50 ms |
| Gain | 4X |
The integration time determines the period over which the sensor collects light information.
The gain controls the amplification of the sensor signal.
Color Detection
The program reads four raw values from the TCS34725:
| Value | Description |
|---|---|
| R | Red light intensity |
| G | Green light intensity |
| B | Blue light intensity |
| C | Clear-channel light intensity |
The program compares these values using predefined conditions to identify the approximate color.
The colors detected by the program are:
- BLACK
- WHITE
- RED
- GREEN
- BLUE
- YELLOW
- UNKNOWN
Color Detection Conditions
The program uses the following conditions for color detection.
| Condition | Detected Color |
|---|---|
| C < 100 | BLACK |
| R > 1500, G > 1500, B > 1500 | WHITE |
| R > G × 1.5 and R > B × 1.5 | RED |
| G > R × 1.3 and G > B × 1.3 | GREEN |
| B > R × 1.3 and B > G × 1.2 | BLUE |
| R > B × 1.5 and G > B × 1.3 | YELLOW |
| None of the above conditions | UNKNOWN |
Note: The detected color depends on the lighting conditions, sensor position, surface properties, and the threshold values used in the program.
Algorithm
The following algorithm is used to interface the TCS34725 color sensor with the ESP32:
- Start the Arduino IDE.
- Include the Wire library for I²C communication.
- Include the Adafruit_TCS34725 library.
- Define GPIO 21 as the SDA pin.
- Define GPIO 22 as the SCL pin.
- Create the TCS34725 sensor object.
- In the void setup() function, start Serial communication at 115200 baud.
- Initialize the I²C communication using GPIO 21 and GPIO 22.
- Initialize the TCS34725 sensor.
- If the sensor is not detected, display an error message and stop execution.
- In the void loop() function, read the raw red, green, blue, and clear values.
- Display the sensor values on the Serial Monitor.
- Compare the sensor readings with the predefined conditions.
- Display the detected color.
- Wait for 500 ms.
- Repeat the process continuously.
The overall operation is:
Light → TCS34725 → R/G/B/C Values → Color Comparison → Detected Color
Working
The ESP32 communicates with the TCS34725 sensor through the I²C interface.
During initialization, the ESP32 configures:
SDA = GPIO 21
SCL = GPIO 22
The program then checks whether the TCS34725 sensor is available.
If the sensor is detected, the ESP32 continuously reads the raw RGB and clear-channel values.
TCS34725 → R, G, B, C → ESP32
The program compares the values according to the predefined conditions.
For example, when the red component is significantly higher than the green and blue components, the program identifies the detected color as RED.
Similarly, the program checks for green, blue, yellow, black, and white.
If none of the conditions are satisfied, the program displays UNKNOWN.
Serial Monitor Output
The sensor values are displayed in the following format:
R:xxxx G:xxxx B:xxxx C:xxxx -> COLOR
For example:
R:1800 G:500 B:450 C:2200 -> RED
The Serial Monitor displays the raw RGB and clear-channel values followed by the detected color.
Program
// © 2026, Cognifront
// TCS34725 Color Detection - ESP32
// SDA = GPIO 21, SCL = GPIO 22
#include <Wire.h>
#include <Adafruit_TCS34725.h>
#define SDA_PIN 21
#define SCL_PIN 22
Adafruit_TCS34725 tcs(
TCS34725_INTEGRATIONTIME_50MS,
TCS34725_GAIN_4X
);
void setup() {
Serial.begin(115200);
Wire.begin(SDA_PIN, SCL_PIN);
if (!tcs.begin()) {
Serial.println("TCS34725 NOT FOUND!");
while (1);
}
Serial.println("TCS34725 Ready");
}
void loop() {
uint16_t r, g, b, c;
tcs.getRawData(&r, &g, &b, &c);
Serial.print("R:");
Serial.print(r);
Serial.print(" G:");
Serial.print(g);
Serial.print(" B:");
Serial.print(b);
Serial.print(" C:");
Serial.print(c);
Serial.print(" -> ");
if (c < 100)
Serial.println("BLACK");
else if (r > 1500 && g > 1500 && b > 1500)
Serial.println("WHITE");
else if (r > g * 1.5 && r > b * 1.5)
Serial.println("RED");
else if (g > r * 1.3 && g > b * 1.3)
Serial.println("GREEN");
else if (b > r * 1.3 && b > g * 1.2)
Serial.println("BLUE");
else if (r > b * 1.5 && g > b * 1.3)
Serial.println("YELLOW");
else
Serial.println("UNKNOWN");
delay(500);
}
Observation
Observe the output on the Serial Monitor at a baud rate of 115200.
The Serial Monitor displays the raw values of:
- Red (R)
- Green (G)
- Blue (B)
- Clear (C)
The program then displays the corresponding detected color.
The output is updated approximately every 500 ms.
Result
The ESP32 successfully interfaces with the TCS34725 color sensor using I²C communication.
The sensor provides raw red, green, blue, and clear-channel readings, and the ESP32 compares these values to identify BLACK, WHITE, RED, GREEN, BLUE, YELLOW or UNKNOWN colors.