The TCS3200 colour sensor detects the colour of objects by shining white LEDs onto a surface and measuring the reflected light through red, green, blue, and clear filters. Combined with an Arduino and a servo motor, you can build a colour sorting machine that separates objects by colour — a popular engineering project for college submissions and a genuinely useful concept in industrial quality inspection. This guide covers sensor wiring, colour detection code, and a complete candy sorting machine build.
How the TCS3200 Colour Sensor Works
The TCS3200 has an 8×8 array of 64 photodiodes: 16 with red filters, 16 with green filters, 16 with blue filters, and 16 with clear (no filter). Four white LEDs illuminate the target surface. You select which filter to read using two control pins (S2, S3). The sensor outputs a square wave whose frequency is proportional to light intensity for the selected colour channel.
By reading the frequency for red, green, and blue channels, you get an RGB colour breakdown of whatever object is in front of the sensor. A red object will have a much higher red frequency compared to green and blue.
Pin Configuration
| Pin | Function | Arduino Connection |
|---|---|---|
| S0, S1 | Output frequency scaling | D4, D5 (set to HIGH, HIGH for 100%) |
| S2, S3 | Filter selection | D6, D7 |
| OUT | Frequency output | D8 |
| OE | Output enable (active LOW) | GND (always enabled) |
| VCC | Power | 5V |
| GND | Ground | GND |
Wiring to Arduino
// Pin definitions
const int S0 = 4;
const int S1 = 5;
const int S2 = 6;
const int S3 = 7;
const int sensorOut = 8;
void setup() {
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
// Set frequency scaling to 100%
digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
Serial.begin(9600);
}
Reading RGB Values
void loop() {
int red = readColour(LOW, LOW); // Red filter
int green = readColour(HIGH, HIGH); // Green filter
int blue = readColour(LOW, HIGH); // Blue filter
Serial.print("R: "); Serial.print(red);
Serial.print(" G: "); Serial.print(green);
Serial.print(" B: "); Serial.println(blue);
// Identify colour
if (red < green && red RED detected");
} else if (green < red && green GREEN detected");
} else if (blue < red && blue BLUE detected");
} else {
Serial.println("=> Unknown colour");
}
delay(500);
}
int readColour(int s2State, int s3State) {
digitalWrite(S2, s2State);
digitalWrite(S3, s3State);
delay(20); // Allow settling
int frequency = pulseIn(sensorOut, LOW);
return frequency;
}
Note: Lower frequency values mean stronger colour presence. A red object reflects more red light, giving a lower red frequency compared to green and blue. This is counterintuitive — lower number = more of that colour.
Calibrating for Accurate Detection
For reliable colour detection, calibrate against your specific objects:
- Place a white surface 1-2 cm from the sensor. Record R, G, B values — these are your maximum (most light reflected) values.
- Place a black surface. Record R, G, B — these are your minimum values.
- Map raw frequencies to 0-255 range using these calibration points.
- Test with your actual objects (candy colours, component colours, etc.) and note the RGB ranges for each colour you want to detect.
// Calibration constants (from white and black reference)
const int RED_MIN = 25, RED_MAX = 220;
const int GREEN_MIN = 30, GREEN_MAX = 250;
const int BLUE_MIN = 25, BLUE_MAX = 200;
int mapColour(int raw, int minVal, int maxVal) {
int mapped = map(raw, minVal, maxVal, 255, 0); // Inverted!
return constrain(mapped, 0, 255);
}
Building the Colour Sorting Machine
Here’s a complete colour sorting machine that detects objects (candies, marbles, beads) sliding down a chute and directs them into the correct bin using a servo motor.
Components Needed
- TCS3200 colour sensor module — ₹150-250
- Arduino Uno — ₹400
- SG90 servo motor — ₹60
- IR obstacle sensor (for object detection) — ₹40
- Cardboard or 3D-printed chute and bins — ₹0-200
- Total: ₹650-950
#include <Servo.h>
const int S0 = 4, S1 = 5, S2 = 6, S3 = 7;
const int sensorOut = 8;
const int irSensor = 2; // Object detection
Servo sortServo;
// Servo positions for 3 bins
const int BIN_RED = 30;
const int BIN_GREEN = 90;
const int BIN_BLUE = 150;
void setup() {
Serial.begin(9600);
pinMode(S0, OUTPUT); pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT); pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
pinMode(irSensor, INPUT);
digitalWrite(S0, HIGH); digitalWrite(S1, HIGH);
sortServo.attach(9);
sortServo.write(90); // Centre position
}
void loop() {
// Wait for object to arrive
if (digitalRead(irSensor) == LOW) { // Object detected
delay(200); // Let object settle over sensor
int red = readColour(LOW, LOW);
int green = readColour(HIGH, HIGH);
int blue = readColour(LOW, HIGH);
Serial.print("R:"); Serial.print(red);
Serial.print(" G:"); Serial.print(green);
Serial.print(" B:"); Serial.println(blue);
// Sort based on dominant colour
if (red < green && red < blue) {
Serial.println("Sorting: RED");
sortServo.write(BIN_RED);
} else if (green < red && green < blue) {
Serial.println("Sorting: GREEN");
sortServo.write(BIN_GREEN);
} else {
Serial.println("Sorting: BLUE");
sortServo.write(BIN_BLUE);
}
delay(500); // Wait for object to fall into bin
sortServo.write(90); // Return to centre
delay(300);
}
}
int readColour(int s2, int s3) {
digitalWrite(S2, s2); digitalWrite(S3, s3);
delay(20);
return pulseIn(sensorOut, LOW);
}
Other Applications
1. Paint Colour Matching
Point the sensor at a wall sample and get the RGB values. Map these to the nearest paint brand colour code. Useful for finding matching paint when repainting a section.
2. Fruit Ripeness Detection
Bananas change from green to yellow to brown. Tomatoes go from green to red. The TCS3200 can detect these colour changes, making it useful for agricultural sorting and automated ripeness grading.
3. Quality Inspection
In manufacturing, colour consistency matters — PCB solder joint colour indicates quality, food products must match colour standards, and packaging must be consistent. The TCS3200 provides an affordable inspection sensor for small-scale production.
Frequently Asked Questions
How close should the sensor be to the object?
Optimal distance is 1-2 cm. Too close and the white LEDs may not uniformly illuminate the surface. Too far and ambient light interferes. For consistent results, use a tube or enclosure around the sensor that blocks ambient light and maintains a fixed distance to the object.
Can the TCS3200 detect any colour?
It detects RGB components, so it can distinguish any colour that has distinct RGB ratios. Saturated colours (pure red, green, blue, yellow) are easy. Pastels and dark shades are harder because the RGB differences are smaller. Black objects reflect very little light on all channels, making them hard to distinguish from dark brown or navy blue.
Why do my readings change with ambient light?
External light adds to the reflected light from the sensor’s LEDs. Always shield the sensor from ambient light using a tube or box. The sensor should rely only on its own white LEDs for illumination.
Can I detect more than three colours?
Yes. With proper calibration, you can distinguish 5-8 colours reliably. Define RGB ranges for each target colour based on testing, and use distance-based matching (compare the measured RGB vector to each known colour’s RGB vector) instead of simple “which channel is lowest” logic.
Conclusion
The TCS3200 colour sensor module is an engaging project component that teaches digital signal processing, calibration, and closed-loop control. A colour sorting machine built with a TCS3200, servo, and Arduino makes an excellent engineering project that demonstrates practical automation concepts. The sensor costs about ₹150-250 and works reliably with proper calibration and ambient light shielding.
Find TCS3200 colour sensors and project components at Zbotic’s sensor collection.
Add comment