In this project, we’ll create a simple color detection system using the TCS3200 color sensor with an Arduino. When the sensor detects a specific color, it will light up the corresponding LED — Red, Green, or Blue. The TCS3200 sensor measures the intensity of different colors by generating a frequency that corresponds to the amount of light in the red, green, and blue spectrum.
Components Required:
- Arduino Board (Uno, Nano, or similar)
- TCS3200 Color Sensor Module
- 3 x LEDs (Red, Green, Blue)
- 3 x 220Ω Resistors (for the LEDs)
- Breadboard and Jumper Wires
- Power Source (USB or 5V Adapter for Arduino)
Wiring the TCS3200 Color Sensor to Arduino
- TCS3200 Pinout:
- VCC : Connect to 5V on the Arduino.
- GND : Connect to GND on the Arduino.
- S0 : Connect to Pin 4 on the Arduino.
- S1 : Connect to Pin 5 on the Arduino.
- S2 : Connect to Pin 6 on the Arduino.
- S3 : Connect to Pin 7 on the Arduino.
- OUT : Connect to Pin 8 on the Arduino. This pin outputs the frequency data.
- LED Connections:
- Red LED: Connect to Pin 9 with a 220Ω resistor in series to limit current.
- Green LED: Connect to Pin 10 with a 220Ω resistor in series.
- Blue LED: Connect to Pin 11 with a 220Ω resistor in series.
Arduino Code for Color Detection
// Define pins for the TCS3200
int S0 = 4;
int S1 = 5;
int S2 = 6;
int S3 = 7;
int OUT = 8;
// Define LED pins
int redLED = 9;
int greenLED = 10;
int blueLED = 11;
// Variables to store color readings
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
void setup() {
// Start Serial Communication
Serial.begin(9600);
// Set TCS3200 pins as output
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(OUT, INPUT);
// Set LED pins as output
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
// Set scaling for frequency (for better readings)
digitalWrite(S0, HIGH); // Frequency scaling
digitalWrite(S1, LOW); // Frequency scaling
// Turn off all LEDs initially
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, LOW);
}
void loop() {
// Read the red color
digitalWrite(S2, LOW); // Set S2 to LOW for red color
digitalWrite(S3, LOW); // Set S3 to LOW for red color
redValue = pulseIn(OUT, LOW); // Measure frequency for red
// Read the green color
digitalWrite(S2, HIGH); // Set S2 to HIGH for green color
digitalWrite(S3, HIGH); // Set S3 to HIGH for green color
greenValue = pulseIn(OUT, LOW); // Measure frequency for green
// Read the blue color
digitalWrite(S2, LOW); // Set S2 to LOW for blue color
digitalWrite(S3, HIGH); // Set S3 to HIGH for blue color
blueValue = pulseIn(OUT, LOW); // Measure frequency for blue
// Print the values to the Serial Monitor
Serial.print("Red: ");
Serial.print(redValue);
Serial.print(" Green: ");
Serial.print(greenValue);
Serial.print(" Blue: ");
Serial.println(blueValue);
// Determine which color is dominant and light up the corresponding LED
if (redValue < greenValue && redValue < blueValue) {
// Red is dominant, light up red LED
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, LOW);
}
else if (greenValue < redValue && greenValue < blueValue) {
// Green is dominant, light up green LED
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
digitalWrite(blueLED, LOW);
}
else if (blueValue < redValue && blueValue < greenValue) {
// Blue is dominant, light up blue LED
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, HIGH);
}
else {
// No dominant color, turn off all LEDs
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, LOW);
}
delay(1000); // Delay to slow down the reading
}
Code Explanation:
- Pin Setup: The code defines the pins for the TCS3200 sensor and the LEDs. The OUT pin (Pin 8) is used to read the frequency of the detected color.
- Frequency Scaling: S0 and S1 pins control the frequency scaling of the sensor’s output. Setting them to HIGH and LOW respectively provides a good frequency range for reading color values.
- Color Detection: The S2 and S3 pins control the color filter. By setting these pins to different combinations of HIGH and LOW, the sensor switches between red, green, and blue color detection.
- LED Control: Based on the frequency readings for each color, the corresponding LED (red, green, or blue) lights up. If no dominant color is detected, all LEDs are turned off.
Testing the Color Detection System
Once you’ve completed the wiring and uploaded the code to your Arduino, you can begin testing the color detection system:
- Open the Serial Monitor in the Arduino IDE (set the baud rate to 9600).
- Place a red, green, or blue object in front of the TCS3200 sensor.
- The Serial Monitor will display the frequency readings for each color (red, green, and blue).
- Depending on the frequency values, the corresponding LED will light up (Red LED for red, Green LED for green, Blue LED for blue).
Conclusion
This project demonstrates how to use the TCS3200 Color Sensor to detect different colors and light up corresponding LEDs using an Arduino. It’s a great starting point for projects like color sorting machines, interactive light displays, or simple color-based automation systems. With the code and wiring provided, you can expand this project further, for example, by adding more color sensors or automating actions based on color detection.