Log in to LMS
Back to Projects
Electronics Arduino Sensors Beginner

Smart Color Detector

Build a smart color detection and sorting system using a color sensor and servo motor. The system identifies colors and automatically moves the servo to the matching position.

Build time 2–3 hours
Difficulty Beginner
Platform Arduino
Smart Color Detector Project

The Smart Color Detector uses a TCS3200 color sensor to identify the color of an object placed in front of it. The Arduino reads the sensor values and compares them to determine whether the object is red, green, or blue. Once detected, a servo motor rotates to a predefined position, creating a simple automated color sorting system.

Component Qty
Arduino Uno×1
TCS3200 Color Sensor Module×1
SG90 Servo Motor×1
Breadboard×1
Jumper Wires×20
Colored Paper (Red, Green, Blue)×3

Connect the components according to the wiring diagram below.

Key Connections Summary:

  • TCS3200 VCC → Arduino 5V
  • TCS3200 GND → Arduino GND
  • TCS3200 S0 → D4
  • TCS3200 S1 → D5
  • TCS3200 S2 → D6
  • TCS3200 S3 → D7
  • TCS3200 OUT → D8
  • Servo Signal → D9
  • Servo VCC → 5V
  • Servo GND → GND
1

Connect the color sensor

Place the TCS3200 sensor on the breadboard and connect all pins according to the wiring diagram.

2

Connect the servo motor

Attach the servo signal wire to Arduino pin D9. The servo will rotate to different positions based on the detected color.

3

Upload the Arduino code

Open Arduino IDE and upload the following code:


#include 

// ---------------- SENSOR PINS ----------------
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8

// ---------------- SERVO ----------------
Servo myServo;
#define servoPin 9

// ---------------- VARIABLES ----------------
int redFreq, greenFreq, blueFreq;

int r, g, b;

// ---------------- SETUP ----------------
void setup() {

  // Sensor pins
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);

  // Frequency scaling (20%)
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);

  // Servo setup
  myServo.attach(servoPin);
  myServo.write(90); // middle position

  // Serial monitor
  Serial.begin(9600);
}

// ---------------- LOOP ----------------
void loop() {

  // -------- RED --------
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  redFreq = pulseIn(sensorOut, LOW);

  // -------- GREEN --------
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  greenFreq = pulseIn(sensorOut, LOW);

  // -------- BLUE --------
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  blueFreq = pulseIn(sensorOut, LOW);

  // -------- SIMPLE MAP --------
  r = map(redFreq, 1000, 200, 0, 255);
  g = map(greenFreq, 1000, 200, 0, 255);
  b = map(blueFreq, 1000, 200, 0, 255);

  // Print values
  Serial.print("R: "); Serial.print(r);
  Serial.print(" G: "); Serial.print(g);
  Serial.print(" B: "); Serial.println(b);

  // -------- COLOR CHECK --------

  // BLACK (no light)
  if (r < 40 && g < 40 && b < 40) {
    myServo.write(160); // black position
  }

  // WHITE (all high)
  else if (r > 200 && g > 200 && b > 200) {
    myServo.write(140); // white position
  }

  // RED
  else if (r > g && r > b) {
    myServo.write(20); // red position
  }

  // GREEN
  else if (g > r && g > b) {
    myServo.write(60); // green position
  }

  // BLUE
  else if (b > r && b > g) {
    myServo.write(100); // blue position
  }

  // default
  else {
    myServo.write(90);
  }

  delay(500);
}
4

Test the sensor

Show different colored objects to the sensor and monitor the readings in the Serial Monitor.

5

Calibrate color values

Lighting conditions can affect sensor readings. Adjust the detection thresholds for better accuracy.

6

Test the sorting system

Present red, green, and blue objects to the sensor. The servo should rotate to the matching position for each detected color.

Chat with us