A color sorting robot using the TCS3200 sensor and servo mechanism with Arduino is a classic mechatronics project that demonstrates colour sensing, decision logic, and precise mechanical actuation in one compact system. Used as a demonstration of automated quality control in manufacturing, this project is popular at engineering college exhibitions across India and provides an excellent foundation for understanding industrial sorting systems. This guide covers everything from TCS3200 wiring to servo-controlled sorting gate construction.
Table of Contents
- TCS3200 Color Sensor Overview
- Wiring TCS3200 to Arduino
- Colour Calibration Procedure
- Servo-Controlled Sorting Gate
- Complete Arduino Code
- Adding a Conveyor Belt
- Frequently Asked Questions
TCS3200 Color Sensor Overview
The TCS3200 (and its successor TCS34725) is a colour-to-frequency converter that converts the intensity of filtered light into a square wave output whose frequency is proportional to the detected light intensity. The IC contains an array of 8×8 photodiodes with four types of filters: red, green, blue, and no filter (clear).
How It Works:
- White LEDs on the module illuminate the target object
- Reflected light passes through the selected colour filter
- The photodiode array converts light intensity to a digital frequency output
- Arduino measures this frequency using the
pulseIn()function - Higher frequency = more of that colour in the object
Specifications:
- Power supply: 2.7V to 5.5V (3.3V or 5V compatible)
- Output: Square wave (50% duty cycle) with frequency proportional to colour intensity
- Frequency scaling: S0/S1 pins set output frequency to 100%, 20%, 2% or off
- Filter selection: S2/S3 pins select Red, Green, Blue, or Clear filter
- Optimal sensing distance: 1-2cm from target surface
- Price in India: ₹80-₹150 for a module with white LED illuminators
Wiring TCS3200 to Arduino
The TCS3200 uses 6 digital pins:
- TCS3200 VCC → Arduino 5V
- TCS3200 GND → Arduino GND
- TCS3200 S0 → Arduino Pin 4
- TCS3200 S1 → Arduino Pin 5
- TCS3200 S2 → Arduino Pin 6
- TCS3200 S3 → Arduino Pin 7
- TCS3200 OUT → Arduino Pin 8
- TCS3200 OE → Arduino GND (tie low to enable)
Servo wiring:
- Servo 1 (main sorting gate) → Arduino Pin 9 (PWM)
- Servo 2 (secondary gate, optional) → Arduino Pin 10 (PWM)
- Servo red wire (5V) → External 5V supply (do NOT power multiple servos from Arduino 5V pin)
- Servo black/brown wire → GND (common with Arduino GND)
Colour Calibration Procedure
Calibration is essential — TCS3200 readings vary significantly based on ambient light, distance, and object surface texture. A proper calibration routine stores baseline readings for each colour and uses them for comparison.
#include <Servo.h>
// TCS3200 pins
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define OUT 8
// Calibration storage
int redMin, redMax;
int greenMin, greenMax;
int blueMin, blueMax;
void setupColorSensor() {
pinMode(S0, OUTPUT); pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT); pinMode(S3, OUTPUT);
pinMode(OUT, INPUT);
// Set frequency scaling to 20% (good balance)
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
}
int readColor(int s2val, int s3val) {
digitalWrite(S2, s2val);
digitalWrite(S3, s3val);
delay(10); // Settle time
return pulseIn(OUT, LOW); // Measure pulse width (inverse of frequency)
}
int readRed() { return readColor(LOW, LOW); }
int readGreen() { return readColor(HIGH, HIGH); }
int readBlue() { return readColor(LOW, HIGH); }
void calibrate() {
Serial.println("Calibrating... Place WHITE reference under sensor");
Serial.println("Press enter when ready");
while (!Serial.available()) {} Serial.read();
redMin = readRed();
greenMin = readGreen();
blueMin = readBlue();
Serial.println("Now place BLACK reference under sensor");
while (!Serial.available()) {} Serial.read();
redMax = readRed();
greenMax = readGreen();
blueMax = readBlue();
Serial.println("Calibration complete!");
Serial.print("Red range: "); Serial.print(redMin);
Serial.print(" - "); Serial.println(redMax);
}
String detectColor() {
int r = map(readRed(), redMin, redMax, 255, 0);
int g = map(readGreen(), greenMin, greenMax, 255, 0);
int b = map(readBlue(), blueMin, blueMax, 255, 0);
// Clamp values
r = constrain(r, 0, 255);
g = constrain(g, 0, 255);
b = constrain(b, 0, 255);
Serial.print("R:"); Serial.print(r);
Serial.print(" G:"); Serial.print(g);
Serial.print(" B:"); Serial.println(b);
// Colour classification
if (r > 150 && g < 100 && b < 100) return "RED";
if (g > 150 && r < 100 && b < 100) return "GREEN";
if (b > 150 && r < 100 && g < 100) return "BLUE";
if (r > 150 && g > 150 && b < 80) return "YELLOW";
if (r > 200 && g > 200 && b > 200) return "WHITE";
return "UNKNOWN";
}
Servo-Controlled Sorting Gate
The sorting gate is a rotary flap controlled by a servo. After the colour sensor identifies the object, the servo rotates to direct the object into the appropriate bin.
Physical Design:
- Use a V-shaped or angled chute with a central pivot point
- The servo arm connects to the chute pivot via a 3D-printed linkage
- Servo at 45° → Left bin; Servo at 135° → Right bin; Servo at 90° → Centre/default
- For 3+ colour sorting, use a rotating carousel with one servo and multiple positions, or use 2 servos in series (two binary gates = 4 possible outputs)
Servo sortingGate;
void sortObject(String color) {
sortingGate.attach(9);
if (color == "RED") {
sortingGate.write(45); // Bin 1 (left)
} else if (color == "GREEN") {
sortingGate.write(90); // Bin 2 (centre)
} else if (color == "BLUE") {
sortingGate.write(135); // Bin 3 (right)
} else {
sortingGate.write(90); // Unknown → centre
}
delay(500); // Wait for servo to reach position
delay(1000); // Wait for object to pass through gate
// Reset gate to centre/detection position
sortingGate.write(90);
delay(300);
}
Complete Arduino Code
#include <Servo.h>
// ... (include all declarations from above)
Servo conveyorMotor; // Controls conveyor belt motor speed via ESC
Servo sortingGate;
void setup() {
Serial.begin(9600);
setupColorSensor();
sortingGate.attach(9);
sortingGate.write(90); // Centre position
// Calibrate on startup
calibrate();
Serial.println("Colour Sorter Ready!");
}
void loop() {
// Wait for object to be placed under sensor
// (In a conveyor system, a proximity sensor triggers this)
Serial.println("Place object under sensor...");
delay(2000); // 2 second window to place object
// Detect colour
String color = detectColor();
Serial.print("Detected: "); Serial.println(color);
// Sort object
sortObject(color);
Serial.println("Ready for next object.");
delay(500);
}
Adding a Conveyor Belt
A conveyor belt automates object feeding, making the sorter fully autonomous. You can build a simple belt conveyor from:
- Two wooden/PVC rollers
- Rubber band or thin belt looped around rollers
- One TT motor (or N20 motor) to drive the primary roller
- L298N or TB6612FNG module to control motor speed
Add an HC-SR04 ultrasonic sensor above the conveyor near the colour sensor position. When the sensor detects an object (distance drops below a threshold), stop the conveyor, detect colour, sort, then restart the conveyor:
void loop() {
startConveyor();
// Wait for object to arrive at sensor
while (getDistance() > 3.0) {
delay(50); // Object not here yet
}
stopConveyor();
delay(100); // Let object settle
String color = detectColor();
sortObject(color);
delay(500); // Clear time
}
Frequently Asked Questions
The TCS3200 keeps detecting the wrong colours — how do I improve accuracy?
Ambient light is the most common cause of incorrect readings. Add a cardboard/3D-printed shroud around the sensor and object area to block external light. Increase the number of readings and average them (read each colour 5-10 times and take the mean). Ensure consistent sensing distance by mounting the sensor at a fixed height above the conveyor. Use a dark background (black) for testing to minimise reflections.
Can TCS3200 distinguish similar shades (light red vs dark red)?
Distinguishing similar shades is challenging. The TCS3200 has limited dynamic range and is better at detecting broadly different colours (red, green, blue) than subtle variations. For precise shade detection, upgrade to the TCS34725 (I2C, better dynamic range) or use a camera-based solution with OpenCV colour space analysis.
How do I sort more than 3 colours?
Add a second servo gate in series with the first (2 binary gates = 4 paths). Or use a rotating disc with holes leading to different bins (servo positions at 90°, 180°, 270°, 360°). Or replace the servo gate with a stepper motor for continuous rotation to precisely position the gate at any angle. For 5+ colours, a turntable carousel mechanism is most practical.
What is the maximum conveyor speed for reliable colour detection?
With standard TCS3200 at 20% frequency scaling, a single colour reading takes approximately 50-100ms. Including averaging (5 readings × 100ms = 500ms minimum), the system needs at least 500ms-1s per object. At a conveyor speed of 5cm/s, objects need to be spaced at least 5cm apart. Slow the conveyor when the object is at the sensor position for best reliability.
Add comment