color sorter using arduino

Photo of author

By Jackson Taylor

Home » Electronics Topics » Arduino » color sorter using arduino

As an Amazon Associate we earn from qualifying purchases.

A color sorter picks up an object, works out what color it is and drops it into the matching box. This project does that with an Arduino, a TCS3200 color sensor, a servo and a small motor. It is a good build if you want to practice reading sensors, making decisions in code and moving something in the real world.

The videos below show the sorter in action. The rest of this article explains how it works, shows the circuit and the full program, and covers how to calibrate it so it sorts reliably.

How the color sorter works

The program follows a simple cycle:

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video
  1. The motor turns a carrier that moves objects, one at a time, toward the sensor.
  2. A Hall effect sensor detects when the carrier reaches the right position, and the motor stops.
  3. The program waits a moment, then reads the red, green and blue values from the color sensor.
  4. It compares the values and decides which color the object is.
  5. A servo turns to the angle of the matching box, and the motor starts again.

In the code, the sorter handles five colors: green, yellow, orange, blue and indigo. Each one has its own servo angle.

What you need

Do not power a motor directly from an Arduino pin. Use a transistor or driver module so the pin only carries the control signal.

About the TCS3200 sensor

The TCS3200 shines white light on an object and measures how much of each color comes back. Instead of a voltage, it outputs a square wave whose frequency depends on the light level. The Arduino measures that signal with pulseIn(). For more on the module itself, see our page on the TCS3200 color sensor and our guide to Arduino color sensor basics.

See also
17V-24V DC TO 48V DC BOOST CONVERTER WITH CLOSED LOOP CONTROL USING ARDUINO AND IGBT

Two pairs of pins control the sensor:

  • S0 and S1 set the output frequency scaling. The code sets S0 high and S1 low, which selects the 20% setting.
  • S2 and S3 choose which color filter is active. Low/low reads red, high/high reads green, and low/high reads blue.

The sketch reads the three colors one after another, with a short delay after each filter change. The values it prints are pulse widths, so a lower number means more of that color. That is why the conditions in the code look for the smallest value, for example green < red && green < blue means green is the strongest channel.

CIRCUIT

Circuit diagram of an Arduino color sorter with TCS3200 sensor, servo, motor and Hall sensor

Pin connections used in the code

  • TCS3200 S0 to pin 8, S1 to pin 10, S2 to pin 11, S3 to pin 12
  • TCS3200 OUT to pin 9
  • Servo signal to A0
  • Motor control to A3 (through a transistor or driver)
  • Hall sensor to A5, set as INPUT_PULLUP

Power the sensor module and the servo from the 5 V supply and connect all grounds together. If the servo jitters or the board resets when the motor starts, give the motor and servo their own supply and share only the ground.

PROGRAM

#include <Servo.h> 
Servo myservo1; 
#define motor A3
#define hall A5
const int s0 = 8;
const int s1 = 10;
const int s2 = 11;
const int s3 = 12;
const int out = 9;
int red = 0; 
int green = 0;
int blue = 0;
int position1=1;
void setup()
{  
  Serial.begin(9600);
  pinMode(motor, OUTPUT);
  pinMode(hall,INPUT_PULLUP);  
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(out, INPUT);
  digitalWrite(s0, HIGH);
  digitalWrite(s1, LOW);
  myservo1.attach(A0);
goto_green_box();//delay(500);
//goto_box_1();goto_box_2();goto_box_3();goto_box_4();goto_box_5();
}
void loop()
{
do
{
 motor_on();//    
}while(digitalRead(hall));
delay(100);
motor_off();Serial.println("ok");
delay(1500);
  color();
  if (green < red && green < blue)
  {
   Serial.println(" Green");   goto_green_box();
  }
  else if (blue < red && blue < green && blue < 400)
  {
Serial.println("blueeee");
  if(red>250)
   {
   Serial.println("blue");goto_blue_box();
   }
   else if(red<200 && blue<200 )
   {
   Serial.println("indigo");goto_indigo_box();
   }
  }
  else if (red < blue && red < green && red <250)
  {
   Serial.println("rdddd");
   if (red < 110 && green >140   && blue>150)
   {
   Serial.println("orange");goto_orange_box();
   }
   else if (red < 130 && green <130 && blue <130)
   {
   Serial.println("yellow");goto_yellow_box();
   }
   else if(red<200 && blue<200 && green>200 )
   {
   Serial.println("indigo");goto_indigo_box();
   }
  }
 motor_on();
 delay(100);
}
void servo_rotationtime(void)
{
delay(1000);  
}
void goto_green_box(void)//green
{
myservo1.write(5);   servo_rotationtime();
}
void goto_yellow_box(void)
{
myservo1.write(45);   servo_rotationtime();
}
void goto_orange_box(void)
{
myservo1.write(85);   servo_rotationtime();
}
void goto_blue_box(void)
{
myservo1.write(125);   servo_rotationtime();
}
void goto_indigo_box(void)
{
myservo1.write(170);   servo_rotationtime();
}
void motor_on(void)
{
  digitalWrite(motor, HIGH);  
}
void motor_off(void)
{
  digitalWrite(motor, LOW);  
}
void check_position(void)
{
position1=digitalRead(hall);  
}
void color()
{
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);   delay(1);
  //count OUT, pRed, RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s3, HIGH);  delay(1);
  //count OUT, pBLUE, BLUE
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);  delay(1);
  //count OUT, pGreen, GREEN
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
   Serial.print("R=");   Serial.print(red);   Serial.print("--G=");   Serial.print(green);   Serial.print("--B=");   Serial.print(blue);
 Serial.println("");
}

How the code works

Moving and stopping the carrier

The do...while loop at the start of loop() keeps the motor on until the Hall sensor changes state. Because the pin uses the internal pull-up, it reads high until the magnet passes and pulls it low. The motor then stops, and a 1.5 second delay lets the object settle before the reading.

See also
FOR POLYTECHNIC COLLEGES

Reading the color

The color() function switches the S2 and S3 pins to select each filter, measures the pulse width and stores the results in red, blue and green. It also prints them to the Serial Monitor in the form R=...--G=...--B=..., which is very useful for tuning.

Choosing the box

The if and else if blocks compare the three values and apply fixed thresholds to separate similar colors such as yellow, orange and blue from indigo. Each result calls a function that writes an angle to the servo: 5 for green, 45 for yellow, 85 for orange, 125 for blue and 170 for indigo. If you want a different layout, change these angles to match where your boxes sit.

The Serial.println messages such as “blueeee” and “rdddd” are debugging output. They do nothing to the sorting, and you can change or remove them.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

Calibrating the sorter

The threshold numbers in the sketch depend on the sensor, the lighting, the distance to the object and the objects themselves. They will probably need adjusting for your build.

  1. Upload the sketch and open the Serial Monitor at 9600 baud.
  2. Place one sample of each color at the sensor position and note the R, G and B values that print.
  3. Repeat a few times, since readings vary a little between attempts.
  4. Update the numbers in the if conditions so they fit the ranges you recorded.
  5. Test again with mixed objects and fine-tune.

Keep the sensor at a fixed distance from the objects and block outside light with a small shroud. Changes in room lighting are the most common reason a sorter works one hour and fails the next. If you want to scale raw readings into a standard range, the map() function is one way to do it.

Troubleshooting

  • The motor never stops: check the Hall sensor wiring and magnet position. The sketch waits for the pin to go low.
  • Wrong colors: recalibrate the thresholds and shield the sensor from ambient light.
  • Servo twitches or the Arduino resets: power the servo and motor separately from the board and share the ground.
  • All values look the same: check the S2 and S3 wiring and make sure the object is close enough to the sensor.
See also
AUTOMATIC VOLTAGE STABILIZER USING MSP430

Ideas for improvements

  • Add an LCD or OLED display to show the detected color and a count for each box.
  • Store thresholds in variables at the top of the sketch so they are easy to edit.
  • Add more boxes and colors, and a reading of white or no object.
  • Reuse the same sensor skills in a line follower robot that reacts to colored lines.

FAQ

Why do lower numbers mean a stronger color?

The TCS3200 outputs a frequency, and the code measures the length of the pulse. A stronger color gives a higher frequency, which means a shorter pulse and a smaller number.

Can I use a different color sensor?

Yes, but you will need to change the reading function and the thresholds. Other sensors communicate differently, for example over I2C, so the color() function would need to be rewritten.

Why does my sorter confuse similar colors?

Colors like yellow and orange give close readings, and lighting changes them further. Fix the sensor distance, block outside light and record real readings from your own objects to set the thresholds.

Do I need the Hall sensor?

The sketch uses it to stop the carrier at the same spot every time. You could replace it with another position sensor or a timed stop, but a consistent stopping point gives more repeatable readings.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works