Interfacing Color Recognition Sensor with ESP32

What is a Color Recognition Sensor?

A Color Recognition Sensor like the TCS3200 or TCS34725 detects colors by measuring the intensity of reflected red, green, blue, and clear light. These sensors are widely used in automation, robotics, and industrial applications.

Working Principle of Color Sensor

The color sensor uses photodiodes to detect RGB light and converts the light intensity into frequency or digital values that can be read by the ESP32.

  • The sensor emits light onto the surface.
  • Reflected light is captured and filtered into red, green, and blue components.
  • The sensor outputs values corresponding to the intensity of each color component.

Formula: Color Value = (Sensor Output Frequency or Intensity Reading)

Components Required

  • ESP32 board
  • TCS3200 or TCS34725 Color Sensor
  • Jumper wires
  • Breadboard (optional)

Pin Configuration of TCS3200/TCS34725

  • VCC: Connects to 3.3V or 5V power supply (depending on sensor variant)
  • GND: Connects to ESP32 GND
  • S0-S3: Control the scaling and color filter selection
  • OUT: Outputs the frequency signal related to the color intensity

Some TCS sensors work at 3.3V, others at 5V. Use logic level shifter if required.

Wiring Color Sensor to ESP32

  • VCC -> 3.3V (or 5V if supported)
  • GND -> GND
  • OUT -> GPIO 18
  • S0 -> GPIO 14
  • S1 -> GPIO 27
  • S2 -> GPIO 26
  • S3 -> GPIO 25

Arduino Code for ESP32 + Color Sensor

1#define S0 14
2#define S1 27
3#define S2 26
4#define S3 25
5#define sensorOut 18
6
7int redFrequency = 0;
8int greenFrequency = 0;
9int blueFrequency = 0;
10
11void setup() {
12  pinMode(S0, OUTPUT);
13  pinMode(S1, OUTPUT);
14  pinMode(S2, OUTPUT);
15  pinMode(S3, OUTPUT);
16  pinMode(sensorOut, INPUT);
17
18  Serial.begin(115200);
19
20  digitalWrite(S0, HIGH);
21  digitalWrite(S1, LOW);
22}
23
24void loop() {
25  digitalWrite(S2, LOW);
26  digitalWrite(S3, LOW);
27  redFrequency = pulseIn(sensorOut, LOW);
28
29  digitalWrite(S2, HIGH);
30  digitalWrite(S3, HIGH);
31  greenFrequency = pulseIn(sensorOut, LOW);
32
33  digitalWrite(S2, LOW);
34  digitalWrite(S3, HIGH);
35  blueFrequency = pulseIn(sensorOut, LOW);
36
37  Serial.print("R = ");
38  Serial.print(redFrequency);
39  Serial.print(" | G = ");
40  Serial.print(greenFrequency);
41  Serial.print(" | B = ");
42  Serial.println(blueFrequency);
43
44  delay(500);
45}

Code Explanation (Line-by-Line)

  • #define S0 14: Defines GPIO 14 for sensor scaling control pin S0
  • #define sensorOut 18: Defines GPIO 18 for sensor frequency output
  • digitalWrite(S0, HIGH); digitalWrite(S1, LOW);: Sets the scaling frequency to 20%
  • pulseIn(sensorOut, LOW);: Reads the frequency of each color filter
  • Serial.print(...): Prints the color values to the Serial Monitor

Applications

  • Object color detection in robotics
  • Sorting systems in industries
  • Smart agricultural color grading
  • Product quality inspection
  • Educational STEM projects

Conclusion

Interfacing a color recognition sensor like TCS3200 with ESP32 enables smart color detection for a wide range of projects. With basic wiring and a simple Arduino sketch, accurate color data can be collected for further automation or classification.