You are on page 1of 1

HOME ARDUINO PROJECTS TUTORIALS  HOW IT WORKS  PROJECTS MISCELLANEOUS  

WHAT'S NEW?  DIY VENDING MACHINE - ARDUINO BASED MECHATRONICS PROJECT FOLLOW ME ON:   
8
Shares
Home  Projects

Arduino Color Sorter Project


 Dejan Nedelkovski  Projects  66

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.

Arduino Color Sorter Project

L AT E S T

 
DIY VENDING MACHINE – ARDUINO BASED
MECHATRONICS PROJECT
 Dejan Nedelkovski

Design
POPULAR
All we need for this Arduino 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 rst place, using the Solidworks 3D
modeling software I made the design of the color sorter and here’s its
Arduino Radar Project
working principle:
 Dejan Nedelkovski  333

Ultrasonic Sensor HC-SR04 and


Arduino Tutorial
 Dejan Nedelkovski  90

How To Control a Stepper Motor with A4988 Driver


and Arduino
 Dejan Nedelkovski  90

Arduino TFT LCD Touch Screen


Tutorial
 Dejan Nedelkovski  144

How I2C Communication Works and


How To Use It with Arduino
 Dejan Nedelkovski

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.
F O L LO W M E !
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.

Arduino Project - Color Sorting Machine 3D Model Assembly Solidworks Files 2.01 MB
Download

Arduino Project - Color Sorting Machine 3D Model Assembly STEP File 1.81 MB How To Mechatronics
Download

Arduino Project - Color Sorting Machine Drawings 171.59 KB


Download

Building the Arduino Color Sorter


D I Y P R O J E C T BY C R E AT I V I T Y H E R O
The material that I used for this project is a 3 mm tick berboard. I redraw the parts on the berboard
according to the drawings and using a small hand saw cut all the parts to size.
Arduino
ArduinoBased
BasedDIY
DIYInteractive
InteractiveLED
LEDCo
Co ee
eeTable
Table

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

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.

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.

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

TCS230 TCS3200 Color Sensor…… Amazon / Ebay

Servo Motor ……………………………… Amazon / Ebay

Arduino Board ………………………….. Amazon / Ebay

Switch………………………………..…….. Amazon / Ebay

Power Jack…………………………..…… Amazon / Ebay

*Please note: These are a liate 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, rst we need to program the Arduino and then nish the assembly. Here’s the Arduino Code:

1. /* Arduino Project - Color Sorting Machine


2. *
3. * by Dejan Nedelkovski, www.HowToMechatronics.com
4. *
5. */
6. #include <Servo.h>
7.
8. #define S0 2
9. #define S1 3
10. #define S2 4
11. #define S3 5
12. #define sensorOut 6
13.
14. Servo topServo;
15. Servo bottomServo;
16.
17. int frequency = 0;
18. int color=0;
19.
20. void setup() {
21. pinMode(S0, OUTPUT);
22. pinMode(S1, OUTPUT);
23. pinMode(S2, OUTPUT);
24. pinMode(S3, OUTPUT);
25. pinMode(sensorOut, INPUT);
26.
27. // Setting frequency-scaling to 20%
28. digitalWrite(S0, HIGH);
29. digitalWrite(S1, LOW);
30.
31. topServo.attach(7);
32. bottomServo.attach(8);
33.
34. Serial.begin(9600);
35. }
36.
37. void loop() {
38.
39. topServo.write(115);
40. delay(500);
41.
42. for(int i = 115; i > 65; i--) {
43. topServo.write(i);
44. delay(2);
45. }
46. delay(500);
47.
48. color = readColor();
49. delay(10);
50.
51. switch (color) {
52. case 1:
53. bottomServo.write(50);
54. break;
55.
56. case 2:
57. bottomServo.write(75);
58. break;
59.
60. case 3:
61. bottomServo.write(100);
62. break;
63.
64. case 4:
65. bottomServo.write(125);
66. break;
67.
68. case 5:
69. bottomServo.write(150);
70. break;
71.
72. case 6:
73. bottomServo.write(175);
74. break;
75.
76. case 0:
77. break;
78. }
79. delay(300);
80.
81. for(int i = 65; i > 29; i--) {
82. topServo.write(i);
83. delay(2);
84. }
85. delay(200);
86.
87. for(int i = 29; i < 115; i++) {
88. topServo.write(i);
89. delay(2);
90. }
91. color=0;
92. }
93.
94. // Custom Function - readColor()
95. int readColor() {
96. // Setting red filtered photodiodes to be read
97. digitalWrite(S2, LOW);
98. digitalWrite(S3, LOW);
99. // Reading the output frequency
100. frequency = pulseIn(sensorOut, LOW);
101. int R = frequency;
102. // Printing the value on the serial monitor
103. Serial.print("R= ");//printing name
104. Serial.print(frequency);//printing RED color frequency
105. Serial.print(" ");
106. delay(50);
107.
108. // Setting Green filtered photodiodes to be read
109. digitalWrite(S2, HIGH);
110. digitalWrite(S3, HIGH);
111. // Reading the output frequency
112. frequency = pulseIn(sensorOut, LOW);
113. int G = frequency;
114. // Printing the value on the serial monitor
115. Serial.print("G= ");//printing name
116. Serial.print(frequency);//printing RED color frequency
117. Serial.print(" ");
118. delay(50);
119.
120. // Setting Blue filtered photodiodes to be read
121. digitalWrite(S2, LOW);
122. digitalWrite(S3, HIGH);
123. // Reading the output frequency
124. frequency = pulseIn(sensorOut, LOW);
125. int B = frequency;
126. // Printing the value on the serial monitor
127. Serial.print("B= ");//printing name
128. Serial.print(frequency);//printing RED color frequency
129. Serial.println(" ");
130. delay(50);
131.
132. if(R<45 & R>32 & G<65 & G>55){
133. color = 1; // Red
134. }
135. if(G<55 & G>43 & B<47 &B>35){
136. color = 2; // Orange
137. }
138. if(R<53 & R>40 & G<53 & G>40){
139. color = 3; // Green
140. }
141. if(R<38 & R>24 & G<44 & G>30){
142. color = 4; // Yellow
143. }
144. if(R<56 & R>46 & G<65 & G>55){
145. color = 5; // Brown
146. }
147. if (G<58 & G>45 & B<40 &B>26){
148. color = 6; // Blue
149. }
150. return color;
151. }

Description of the code:

So, we need to include the “Servo.h” library, de ne 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 de ne the pins as Outputs and Inputs, set the frequency-scaling for the color sensor, de ne 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 di erent 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.

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 rst “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.

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

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

JLCPCB - Prototype PCBs for $2 + Free Shipping on First Order

China's Largest PCB Prototype Manufacturer, 290,000+ Customers & 8,000+ Online Orders Per Day

10 PCBs Price: $2 for 2-layer, $15 for 4-layer, $74 for 6-layer

Arduino Arduino Project Color Sensor Color Sorter Color Sorting Machine

SHARE ON: Tweet


Stum

R E L AT E D P O S T S

ARDUINO TOUCH SCREEN MUSIC PLAYER AND ALARM MECHATRONICS FINAL YEAR PROJECT
CLOCK PROJECT  Dejan Nedelkovski  54

 Dejan Nedelkovski  29

HOW TO MAKE A COMPASS USING ARDUINO AND DIY VENDING MACHINE – ARDUINO BASED
PROCESSING IDE MECHATRONICS PROJECT
 Dejan Nedelkovski  14  Dejan Nedelkovski

66 RESPONSES

Kevlar July 14, 2016

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 July 17, 2016

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

Arduino Blog – This Arduino machine will sort your Skittles by color July 20, 2016

[…] do it for you? As part of a recent tutorial, Dejan Nedelkovski has built what we calls
the “Arduino Color Sorter” using a TCS3200 color sensor, two hobbyist servo motors, and
an Arduino […]

REPLY

SMM2 July 21, 2016

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

Dejan Nedelkovski July 24, 2016

Thanks!
I have soldered together 4 jumper wires.

REPLY

Mathew Rawson August 9, 2016

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

REPLY

Dejan Nedelkovski August 14, 2016

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

REPLY

putriana August 13, 2016

hello master , I want to ask what the switch and power jack used

REPLY

Dejan Nedelkovski August 14, 2016

Please recheck the article, I have add links so you can see what switch
and power jack can be used.

REPLY

A f Zakuan August 14, 2016

sir, your ideas was brilliant. i want to develop this project for my nal 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 August 14, 2016

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

Atikah Fatmawati August 19, 2016

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

Dejan Nedelkovski August 26, 2016

Yes, sure it will work.

REPLY

sayali October 1, 2016

hello sir , i have the same question as my arduino


nano is not working with pc. i am not able to program
arduino nano what should i do ?? should i use uno
instead of nano ???? please help ..

Dejan Nedelkovski October 1, 2016

Sure you can use a di erent Arduino as well.

lucky September 12, 2016

What’s this design tool name ?

REPLY

Dejan Nedelkovski September 25, 2016

Solidworks.

REPLY

Nabil September 26, 2016

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

Dejan Nedelkovski October 1, 2016

Yes, the readinds are that much stable, the same case was with mine
sensor. Though, the closer the sensor is to the color the better.

REPLY

John September 29, 2016

What’s the adapter you use? 5V/1A or 5V/2A?

REPLY

Dejan Nedelkovski October 1, 2016

I used 5V/2A but 5V/1A could do the job as well.

REPLY

Piyal October 1, 2016

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

Dejan Nedelkovski October 1, 2016

Adjust the values of the topServo.write() function, or the range of the


for loop.

REPLY

No mu hyun October 4, 2016

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

REPLY

Dejan Nedelkovski October 14, 2016

Well simply increase the berboard whole and the sensor will t.

REPLY

Kartik Jain October 9, 2016

Which jumper wires have you used ?

REPLY

Dejan Nedelkovski October 14, 2016

Normal ones…

REPLY

sayali October 14, 2016

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
di erent angle for same color
what should i do ?

REPLY

Dejan Nedelkovski October 18, 2016

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 di erent 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 October 20, 2016

the color sensor reading vary randomly. orange ,red,


blue skittles are getting sorted randomly. is the
sensor faulty ?

Dejan Nedelkovski October 22, 2016

Yes, the sensor is the problem. You must adjust the


program to your readings.

Mustafa October 20, 2016

hi..İt is great project! I did this but bottom servo not working.

REPLY

Sayali October 23, 2016

I think the output of colour sensor is not connected with arduino,


check it once @Mustafa

REPLY

cesar November 26, 2016

hello , i´ve tried to replicate your project but my servos just wont move, and i´ve been
using di erent 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

Dejan Nedelkovski December 17, 2016

What kind of motor are us using? Did your try to swap the two
motors? Is it working that way?

REPLY

Mishi December 6, 2016

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

REPLY

Dejan Nedelkovski December 16, 2016

It can be anything. Can you try to be more speci c. Also double check
your connection.

REPLY

Sas December 11, 2016

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

Dejan Nedelkovski December 16, 2016

Thanks. What kind of servo motor are you using? Double check your
connection.

REPLY

Nilay Kundu February 9, 2017

Sir how to give power supply to Arduino?? Can i


connect it directly with 5v battery or i have to use a
Arduino power supply module..please help or give a
link of power supply to buy online.

Dejan Nedelkovski February 21, 2017

Well if you have a 5V battery of adapter you can


connect it directly to the 5V pin of the Arduino.

Keith February 16, 2017

Hi, and Thanks for the inspiration for my next project. Just waiting on the delivery of my
TCS3200 sensor now.

While reading and understanding your code I’ve spotted 2 typo errors (which don’t a ect
the functionality, of course, just my initial understanding of how it works). In lines 116
and 128 you repeat the comment “// printing RED color frequency” but those lines
should say GREEN and BLUE respectively.

Thanks again for such a clear and simple approach.

REPLY

fummy March 20, 2017

Hai sir.For this project we have to connect arduino board by usb and with power adapter
or not.

REPLY

Dejan Nedelkovski March 21, 2017

You can use any of them.

REPLY

Ann March 23, 2017

Hi, please help me, when I try to put blue item on the color sensor, it gives out green
color

REPLY

Dejan Nedelkovski March 27, 2017

You must manually adjust the sensor values to match the real color.

REPLY

lakshman April 19, 2017

how???? can i do that???

lakshman March 24, 2017

can i use SG90 9g servo motor for both positions?? by using thin(very light) plywood…!!!!!!

REPLY

Dejan Nedelkovski March 27, 2017

Yes sure, they would be ne.

REPLY

lakshman March 30, 2017

thank you very much….i am doing this project as my


major project

Tommy April 17, 2017

Hi,
For the if statements at the bottom of your code, why do you only use two of the colors
(i.e.
if(R32 & G55){
color = 1; // Red
}) -> only red and green are used. Why don’t you use red, green, and blue in each of the
statements?
Thanks,

REPLY

Dejan Nedelkovski May 11, 2017

Hi, that’s because the sensor outputs were not that precise and
accurate and I was getting very similar values, close to each other, for
each R, G and B. So in order to better separate, or recognize the colors
I made the “if” statements in such a way to use only two colors for
de ning the colors I needed.

REPLY

Amela Dz May 7, 2017

Hello! How to manually adjust the sensor values to match the colors? Thanks for the
answer

ps great project!

REPLY

Dejan Nedelkovski May 11, 2017

Print the values you are getting into the Serial monitor and then adjust
them in the readColor() custom function.

REPLY

Carlos May 8, 2017

Hey there.
I built the whole model and got the same exact pieces as you but… My TCS3200 has red
leds on the board, not white ones.
Also, the values it reads are all around 15000 for each color (R, G or B).
Any idea on why this happens?

REPLY

Dejan Nedelkovski May 11, 2017

Hi there, well you probably have a di erent version of the module, but
it should be a problem, you can make it work. You just have to adjust
the values for each color in the “if” statements in the readColor()
custom function.

REPLY

cognitio May 10, 2017

Hello, I have one question – while trying to setup everything as explained here, I can’t get
my sensor motor to rotate based on colour change. It simply stays non moving. Motor is
ne, replacing it with working one yields same result. Do you have any hint as to what
could be the issue? Thanks.

REPLY

Dejan Nedelkovski May 11, 2017

Hello, the problem is probably that the values you are getting from
your color sensor doesn’t match with my values. First you must see
what values you are getting from your color sensor for a particular
color, and then adjust these values in the readColor() custom function,
where the last “if” statements are.

REPLY

Chau May 10, 2017

Hello Dejan,
This is a really good project but I have a problem. One of the servomotors moves at the
same directions repeatedly but the bottom servomotor doesn’t move at all. What should
I do?

REPLY

Dejan Nedelkovski May 11, 2017

You are not getting the same values for the colors as mine, so you
should adjust them.

REPLY

José Carrasco June 10, 2017

Hey man, i have a little (huge) problem, when i try to upload the program it says
“`readColor` was not declared in this scope” i don´t know why it happens and i checked
the whole program but it´s the same as this page´s.
I´m so bad with Arduino so i hope you can help me to solve it.

REPLY

Dejan Nedelkovski June 17, 2017

You have a typo in the code, you probably haven’t copied it properly.

REPLY

BHAVESH CHAUHAN September 7, 2017

nice project…the dimensions are in mm i guess..i was confused with the scale 1:5 in the
diagrams..

REPLY

call me Billy September 11, 2017

hi sir
I’m sorry if i can’t write in well
i wanna ask something, can this arduino with color sensor to read average color in 2 sec
for “telling” what is this based on color
like a red color in apple, because apple not really red color. I think if we take an average
value in few sec, this arduino can tell this an apple, ect
if you can help me please contact me, cause I’m interested from yours idea

REPLY

Dejan Nedelkovski September 11, 2017

Sure it can be used for such a purpose. But it depends what will you
compare the red apples to. For example if you compare them to green
apples it would be probably be easy to distinguish the colors.

REPLY

call me Billy September 15, 2017

that is i mean just read red apples or green


but, the next is
the output is countering the “apple” and the
information output displayed on lcd
for example, i put the green and red apple in the
pipes integrated with the sensor, automatically it will
sort how many the green apples in the 1 round of
sort and how many too in red apple
i really need help from you sir

L E A V E A R E P LY

Your email address will not be published.

Comment

Name* Email*

Website

S U B MIT

H O W T O M E C H AT R O N I C S F O L L O W H O W T O M E C H AT R O N I C S O N DISCLOSURE

SOCIAL MEDIA
HowToMechatronics is an education website in the HowToMechatronics is a participant in the Amazon
area of Mechanical, Electrical and Computer Services LLC Associates Program, an a liate
Engineering. Tutorials, Tips, Tricks, How It Works, advertising program designed to provide a means
Projects, Examples, Source Codes, Download les for sites to earn advertising fees by advertising and
and much more can be found here. linking to Amazon.com

Copyright © 2018 HowToMechatronics.com. All rights


reserved. Terms and Conditions

You might also like