Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Color Sorting Robot with Dual Servomotors and DC Motor Control

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Color Sorting Robot with Dual Servomotors and DC Motor Control

Circuit Documentation

Summary

This circuit involves an Arduino UNO microcontroller, two SG90 servomotors, a DC motor, a TCS3200 color sensor, an L298N motor driver, and a Li-ion 18650 battery. The Arduino UNO controls the servomotors and the DC motor based on the color detected by the TCS3200 sensor. The L298N motor driver is used to control the DC motor, and the Li-ion battery provides power to the motor driver.

Component List

Arduino UNO

Servomotor SG90 (x2)

DC Motor

Li-ion 18650 Battery

L298N 2A Dual Motor Driver Module with PWM Control

TCS3200

Wiring Details

Arduino UNO

Servomotor SG90 (1)

Servomotor SG90 (2)

DC Motor

Li-ion 18650 Battery

L298N 2A Dual Motor Driver Module with PWM Control

TCS3200

Code Documentation

#include <Servo.h>

// Pin definitions
const int S0 = 4;
const int S1 = 5;
const int S2 = 6;
const int S3 = 7;
const int sensorOut = 8;
const int servoPin1 = 9;
const int servoPin2 = 10;
const int motorENA = 11;
const int motorIN1 = 12;
const int motorIN2 = 13;

// Servo objects
Servo servo1;
Servo servo2;

// Color detection thresholds (calibrate these values)
const int redThreshold = 30;
const int greenThreshold = 30;
const int blueThreshold = 30;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Set pin modes
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
  pinMode(motorENA, OUTPUT);
  pinMode(motorIN1, OUTPUT);
  pinMode(motorIN2, OUTPUT);

  // Initialize servos
  servo1.attach(servoPin1);
  servo2.attach(servoPin2);
  servo1.write(0);
  servo2.write(0);

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

  // Start the motor
  digitalWrite(motorIN1, HIGH);
  digitalWrite(motorIN2, LOW);
  analogWrite(motorENA, 255);
}

void loop() {
  // Pause the motor
  analogWrite(motorENA, 0);

  // Read color values
  int red = readColor(LOW, LOW);
  int green = readColor(HIGH, HIGH);
  int blue = readColor(LOW, HIGH);

  // Print color values for debugging
  Serial.print("R: ");
  Serial.print(red);
  Serial.print(" G: ");
  Serial.print(green);
  Serial.print(" B: ");
  Serial.println(blue);

  // Determine color and actuate servos
  if (red > redThreshold && red > green && red > blue) {
    // Detected red
    servo1.write(90);
    delay(1000);
    servo1.write(0);
  } else if (green > greenThreshold && green > red && green > blue) {
    // Detected green
    servo2.write(90);
    delay(1000);
    servo2.write(0);
  }

  // Resume the motor
  analogWrite(motorENA, 255);
  delay(1000); // Adjust delay as needed
}

// Function to read color values
int readColor(int s2State, int s3State) {
  digitalWrite(S2, s2State);
  digitalWrite(S3, s3State);
  return pulseIn(sensorOut, LOW);
}

This code initializes the Arduino UNO, sets up the pins, and controls the servomotors and the DC motor based on the color detected by the TCS3200 sensor. The motor is paused while the color is being read, and the servos are actuated based on the detected color. The motor then resumes operation.