Skip to main content

Arduino colour sensor and servo pro

Colour sensor with servo project
Making
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
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.

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

Finesh

Comments

Post a Comment

Popular posts from this blog

220v ac to 5v dc without transformer

220v ac to 5v dc converted circuit Needed 4*1N4007 diode 100 ohm 1W 470k 1W 225k/400v capasitor 470uf/100v capasitor 220ohm resistor 15v 1W diode led 10uf 25v capasitor 7805 ic 1uf 10v Now the time for circuit design This is the simplest way for converting 220 ac volts to 5v dc volts (usb current) it is use for mobile phone charger and power bank charger. Capasitor in this circuit for give smooth current. Diode in this circuit for convert ac to dc. (Diode bridge)   Capasitor in this circuit. Capacitors in DC Circuits. Capacitors do not play an important role in DC circuits because it is impossible for a steady current to flow across acapacitor. If an uncharged capacitor is connected across the terminals of a battery of voltage then a transient current flows as the capacitor plates charge up. Diode in this circuit for. Diodes can be used as rectifiers, signal limiters, voltage regulators, switches, signal modulators, sign...

how to fire a matchstrick wirelessly using android phone

Copper wire:0.5mm/1mm Copper wire:0.1-0.4mm Motor Arduino esp8266 12E 5v relay module Alkaline battery:5v-9v AA battery:3v Android phone Blynk application No worry about this circuit because this is 100% working and tested. The basic consept of this circuit is below This circuit is possible because mini copper wire get more heat than the bigger wire. Codeing method First you install arduino ide on your pc Then copy and paste this code in your arduino #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> // You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon). char auth[] = "YourAuthToken"; // Your WiFi credentials. // Set password to "" for open networks. char ssid[] = "YourNetworkName"; char pass[] = "YourPassword"; void setup() {   Serial.begi...