Arduino Color Sorter Project

In this article I will show you how you can make an Arduino Color Sorter. You can watch the following video or read the written article below.

Design


All we need for this project is one color sensor (TCS3200) and two hobbyist servo motors, which makes this project quite simple but yet very fun to build it. In the first place, using the Solidworks 3D modeling software I made the design of the color sorter and here’s its working principle:

Color Sorting Machine Arduino Project Solidworks Model

  • Initially, the colored skittles which are held in the charger drop into the platform attached on the top servo motor.
  • Then the servo motor rotates and brings the skittle to the color sensor which detects its color.
  • After that the bottom servo motor rotates to the particular position and then the top servo motor rotates again till the skittle drop into the guide rail.

Here you can download the 3D model, as well as, the drawings with all dimensions needed for building this Arduino project.

Here you can download that file:

 

Building the Arduino Color Sorter


The material that I used for this project is a 3 mm tick fiberboard. I redraw the parts on the fiberboard according to the drawings and using a small hand saw cut all the parts to size.

Color-Sorter-Fiberboard-parts

Once I got all the parts ready, I started assembling them. First I assembled the outer parts using a glue gun.

Color Sorter Assembling

Then using all-purpose glue I glued the two servo motors on their platforms and attached them to the assembly.

After that again using a glue I attached the guide rail on the bottom servo motor as well as the support and the platform needed for the top servo motor.

Color Sorting Machine Servo Motors

Next, I inserted a switch and a power jack for powering the Arduino with a 5V adapter and on the third platform I inserted the color sensor.

Color Sorting Machine TCS3200 Color Sensor

I connected the components together according to the following circuit schematics.

Arduino Color Sorting Machine Circuit Schematic

You can get the components needed for this Arduino Project from links below:

  • TCS230 TCS3200 Color Sensor…… Amazon
  • Servo Motor ……………………………… Amazon
  • Arduino Board ………………………….. Amazon
  • Switch………………………………..…….. Amazon
  • Power Jack…………………………..…… Amazon

*Please note: These are affiliate links. I may make a commission if you buy the components through these links.
I would appreciate your support in this way!

Arduino Color Sorter Source Code


At this point, first we need to program the Arduino and then finish the assembly. Here’s the Arduino Code:

/*     Arduino Project - Color Sorting Machine
 *
 *  by Dejan Nedelkovski, www.HowToMechatronics.com
 *
 */
#include <Servo.h>

#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define sensorOut 6

Servo topServo;
Servo bottomServo;

int frequency = 0;
int color=0;

void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);

  // Setting frequency-scaling to 20%
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);

  topServo.attach(7);
  bottomServo.attach(8);

  Serial.begin(9600);
}

void loop() {

  topServo.write(115);
  delay(500);
  
  for(int i = 115; i > 65; i--) {
    topServo.write(i);
    delay(2);
  }
  delay(500);
  
  color = readColor();
  delay(10);  

  switch (color) {
    case 1:
    bottomServo.write(50);
    break;

    case 2:
    bottomServo.write(75);
    break;

    case 3:
    bottomServo.write(100);
    break;

    case 4:
    bottomServo.write(125);
    break;

    case 5:
    bottomServo.write(150);
    break;

    case 6:
    bottomServo.write(175);
    break;
    
    case 0:
    break;
  }
  delay(300);
  
  for(int i = 65; i > 29; i--) {
    topServo.write(i);
    delay(2);
  } 
  delay(200);
  
  for(int i = 29; i < 115; i++) {
    topServo.write(i);
    delay(2);
  }
  color=0;
}

// Custom Function - readColor()
int readColor() {
  // Setting red filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int R = frequency;
  // Printing the value on the serial monitor
  Serial.print("R= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(50);

  // Setting Green filtered photodiodes to be read
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int G = frequency;
  // Printing the value on the serial monitor
  Serial.print("G= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(50);

  // Setting Blue filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  int B = frequency;
  // Printing the value on the serial monitor
  Serial.print("B= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.println("  ");
  delay(50);

  if(R<45 & R>32 & G<65 & G>55){
    color = 1; // Red
  }
  if(G<55 & G>43 & B<47 &B>35){
    color = 2; // Orange
  }
  if(R<53 & R>40 & G<53 & G>40){
    color = 3; // Green
  }
  if(R<38 & R>24 & G<44 & G>30){
    color = 4; // Yellow
  }
  if(R<56 & R>46 & G<65 & G>55){
    color = 5; // Brown
  }
  if (G<58 & G>45 & B<40 &B>26){
    color = 6; // Blue
  }
  return color;  
}

Description of the code:

So, we need to include the “Servo.h” library, define the pins to which the color sensor will be connected, create the servo objects and declare some variables needed for the program. In the setup section we need to define the pins as Outputs and Inputs, set the frequency-scaling for the color sensor, define the servo pins and start the serial communication for printing the results of the color read on the serial monitor.

In the loop section, our program starts with moving the top servo motor to the position of the skittle charger. Note that this value of 115 suits to my parts and my servo motor, so you should adjust this value as well as the following values for the servo motors according to your build.

Next using the “for” loop we will rotate and bring the skittle to the position of the color sensor. We are using a “for” loop so that we can control the speed of the rotation by changing the delay time in loop.

Next, after half a second delay, using the custom made function, readColor() we will read the color of the skittle. Here’s the code of the custom function. Using the four control pins and the frequency output pin of the color sensor we read color of the skittle. The sensor reads 3 different values for each skittle, Red, Green and Blue and according to these values we tell what the actual color is. For more details how the TCS3200 color sensor works you can check my previous detailed tutorial about it.

Arduino Color Sensing Tutorial TCS230 Color Sensor

Here are the RGB values that I got from the sensor for each skittle. Note that these values can vary because the sensors isn’t always accurate. Therefore, using these “if” statements we allow the sensor an error of around +-5 of the tested value for the particular color. So for example if we have a Red skittle, the first “if” statement will be true and the variable “color” will get the value 1. So that’s what the readColor() custom function does and after that using a “switch-case” statement we rotate the bottom servo to the particular position. At the end we further rotate the top servo motor until the skittle drops into the guide rail and again send it back to the initial position so that the process can repeated.

Finishing the Design


After uploading the code I secured the Arduino Board using a glue gun.

Color Sorting Machine Arduino Board

Then using a transparent plastic bottle I made the charger and together with the top part glued it to assembly and finished the project.

Arduino Color Sorter Project

Feel free to ask any question in the comments section below.

39 Responses

  1. Kevlar

    Hi, great project!

    I’m just wondering how the arduino is powered. So the code is uploaded to the arduino from the computer, but then it is unplugged and powered with an adapter? Is this then just plugged into a wall outlet?

    Reply
    • Dejan Nedelkovski

      Hi there and thanks! Yes, when uploading the Arduino is connected and powered via the USB of the computer. After uploading the code the Arduino is powered by a 5V adapter, which can be seen from the circuit schematics provided in this article.

      Reply
  2. SMM2

    Hi, I was just wondering how you connected the GND and 5V from all three components directly to the Arduino Nano without having to use a breadboard or a shield. Since there is only one pin for 5V and two pins for GND and you have 3 components.

    Also really cool project!

    Reply
  3. Mathew Rawson

    Can you by chance send me the files in a pdf form my computer cant open the slddrw file with out buying a 5 dollar app.

    Reply
    • Dejan Nedelkovski

      The drawing files are in a .rar files which is quite common file format which can be easily opened or/and extracted, even online. I hope you can find a way to open it.

      Reply
  4. Afif Zakuan

    sir, your ideas was brilliant. i want to develop this project for my final year project (FYP) . Can you guide me more on this project ? i think i want to add sound after the sorting for the development. hope to hear from u soon 🙂

    -malaysia-

    Reply
    • Dejan Nedelkovski

      Thanks. Well I think the video and the article it self covers everything you need in order to make this project, you got the 3D model, drawings, circuit schematics and source code.

      Reply
  5. Atikah Fatmawati

    Hello sir, i am really inspired by your doing. Anyway i just want to know since you are using Arduino Nano, will it work the same way if i use Arduino Uno? I need a lot of guidance. Thanks

    Reply
  6. Nabil

    Hi Dejan,

    You did a great project. I am trying to use the TCS3200 Color sensor with 4-led on the board directly (no legs … the sensor has round shape …please see this link:(LINK REMOVED). I have noticed that the reading of colors varies a lot while testing the sketch in arduino. Does it means that these led around should be closed to the sensor ship to get accurate values as on your project, or shall I buy one as on your project ?
    Kindly advise ,
    Regards,
    Nabil

    Reply
  7. Piyal

    Hi sir
    How to adjust the value in Top servo motor rotates and brings the skittle to the color sensor which detects its color.

    Reply
  8. No mu hyun

    I bought the TCS230 TCS3200 color sensor above. So it arrived today. But the color sensor doesnt’t fit on the fiberboard. the square on the fiberboard is small to put the sensor. How do you put it into fiberboard?
    the square size of the fiberboard is 2.5cm X 1.3cm.
    but the size of the color sensor is 3cm X 1.3cm.

    Reply
  9. sayali

    i am done with model and the project is working well. just the color sensor is getting confuse :/
    i have set the frequencies as per my color sensor . but still the bottom servo rotates at different angle for same color 🙁
    what should i do ?

    Reply
    • Dejan Nedelkovski

      Well the sensor isn’t stable and makes errors when reading. Plus its readings depends on several factors like obviously the color you are reading but also the ambient lighting as well. So if it’s darker you will probably get different readings compared if you are using the same color object and test it on daylight.
      In order to make it working you must test the color readings for each object and according to your readings adjust the R, G and B that are found values at the bottom of the code.

      Reply
      • sayali vatsa

        the color sensor reading vary randomly. orange ,red, blue skittles are getting sorted randomly. is the sensor faulty ?

    • Sayali

      I think the output of colour sensor is not connected with arduino, check it once @Mustafa

      Reply
  10. cesar

    hello , i´ve tried to replicate your project but my servos just wont move, and i´ve been using different power supplies even a 5v-2A power supply but it doesnt work,
    could you tell me what power supply did you use to power your electronics???
    any advice you can give me??’

    Reply
  11. Mishi

    Hi. I followed your instructions and code but the two servos are not running. what might be the reason?

    Reply
  12. Sas

    Hi, Mr. Dejan
    Nice project

    I try same project and copy your code into my Mega2560. I have following the schematic diagram. But, my bottom servo not working. Whats wrong?

    Thank you

    Reply

Leave a Reply

Your email address will not be published.