Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO Based Color Sorting Machine with Dual Servo Control

Image of Arduino UNO Based Color Sorting Machine with Dual Servo Control

Circuit Documentation

Summary of the Circuit

This circuit is designed to operate a color sorting machine. It uses an Arduino UNO microcontroller to interface with a TCS3200 color sensor and control two SG90 servomotors. The color sensor detects the color of an object, and based on this color, the Arduino directs the servomotors to sort the object into a designated location. The system is powered by a 5V supply from the Arduino, which is also used to power the servomotors and the color sensor. Ground connections are shared among all components.

Component List

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P.
  • Purpose: Acts as the central processing unit of the circuit, controlling the servomotors and reading data from the color sensor.
  • Pins: IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13.

TCS3200 Color Sensor

  • Description: A color sensor that can detect a wide range of colors.
  • Purpose: To detect the color of objects and send this information to the Arduino.
  • Pins: GND, OUT, S2, S3, VCC, LED, S0, S1.

Servomotor SG90 (2x)

  • Description: A small and lightweight servomotor suitable for small-scale projects.
  • Purpose: To physically move parts of the machine based on commands from the Arduino.
  • Pins: SIG, VCC, GND.

Wiring Details

Arduino UNO

  • 5V to TCS3200 VCC, Servomotor SG90 VCC (both servomotors)
  • GND to TCS3200 GND, Servomotor SG90 GND (both servomotors)
  • D2 to TCS3200 S0
  • D3 to TCS3200 S1
  • D4 to TCS3200 S2
  • D5 to TCS3200 S3
  • D6 to TCS3200 OUT
  • D9 to Servomotor SG90 SIG (first servomotor)
  • D10 to Servomotor SG90 SIG (second servomotor)

TCS3200 Color Sensor

  • VCC to Arduino UNO 5V
  • GND to Arduino UNO GND
  • OUT to Arduino UNO D6
  • S0 to Arduino UNO D2
  • S1 to Arduino UNO D3
  • S2 to Arduino UNO D4
  • S3 to Arduino UNO D5

Servomotor SG90

  • VCC to Arduino UNO 5V
  • GND to Arduino UNO GND
  • SIG to Arduino UNO D9 (first servomotor)
  • SIG to Arduino UNO D10 (second servomotor)

Documented Code

/*Color Sorting Machine by : Omar Wael Morsi*/
/*YouTube : Morsi Hamed  https://www.youtube.com/c/morsihamed */

#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); 
  digitalWrite(S0, HIGH); //20% scaling
  digitalWrite(S1, LOW);
  topServo.attach(9);
  bottomServo.attach(10);
  Serial.begin(9600);
}

void loop() {
  topServo.write(83); //set top servo at the first hole to catch the candy
  delay(500);
  
  for(int i = 83; i >= 30; i--) { //move top servo with the candy to the color sensor position
    topServo.write(i);
    delay(2);
  }
  delay(500);
  
  color = readColor(); //read the value from color sensor and put the bottom servo in the right path
  delay(10);  
  switch (color) {
    case 1:
    bottomServo.write(15);
    break;
    case 2:
    bottomServo.write(60);
    break;
    case 3:
    bottomServo.write(110);
    break;
   
    case 0:
    break;
  }
  delay(300);
  
  for(int i = 30; i >= 0; i--) { //move top servo with candy from the color sensor position to the second hole position
    topServo.write(i);
    delay(2);
  } 
  delay(200);
  
  for(int i = 0; i <= 83; i++) { //return top servo to the first position to catch another candy
    topServo.write(i);
    delay(2);
  }
  color=0;
}

int readColor() { //color sensor function to read the color
  
  // Setting red
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  frequency = pulseIn(sensorOut, LOW);
  int R = frequency;
  Serial.print("R= ");
  Serial.print(frequency);
  Serial.print("  ");
  delay(50);
  
  // Setting Green
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  frequency = pulseIn(sensorOut, LOW);
  int G = frequency;
  Serial.print("G= ");
  Serial.print(frequency);
  Serial.print("  ");
  delay(50);
  
  // Setting Blue
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  frequency = pulseIn(sensorOut, LOW);
  int B = frequency;
  Serial.print("B= ");
  Serial.print(frequency);
  Serial.println("  ");
  delay(50);
  
  if(R<65 & R>50 & G>83 & G<95 & B>60 & B<70){
    color = 1; // Red
  }
  if(G<70 & G>55 & R<75 & R>65 &B<65 & B>54){
    color = 2; // Green
  }
  if (B<60 &B>40 & G>68 & G<85 & R>75 & R<95){
    color = 3; // Blue
  }
  return color;  
}
/*i hope this was helpful for you please check tutorial video in my youtube channel*/
/*and if you have any question i will be happy answering it :)*/
/*Servo tutorial : https://youtu.be/FXCSVss93XU*/
/*Color Sensor tutorial : https://youtu.be/IqVtrWKNfCw*/
/* https://www.youtube.com/playlist?list=PLJRh_zZSG_PT0lJAzx0eTCyPwk_obpJjt */

This code is responsible for controlling the color sorting machine. It initializes the servomotors and the color sensor, then enters a loop where it positions the servomotors based on the color detected by the sensor. The readColor function reads the color by measuring the frequency of the output signal from the color sensor when illuminated by red, green, and blue light, respectively. The color is determined based on predefined frequency ranges for each color.