0% found this document useful (0 votes)
26 views33 pages

Full Robot

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views33 pages

Full Robot

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# **Complete Automated Color-

Sorting Robot Code**


**(Motion + Arm + Color
Sensor)**

This code integrates **robot


movement**, **color sensing**,
and a **servo-based sorting
arm** for a fully automated color-
sorting robot.

---

## **Hardware Components
Needed**
1. **Arduino Uno** (or
Mega/Nano)
2. **TCS230/TCS3200 Color
Sensor**
3. **L298N Motor Driver** (for
DC motors)
4. **2-4 DC Motors** (for robot
movement)
5. **Servo Motor** (for sorting
arm)
6. **Ultrasonic Sensor (HC-
SR04)** (optional, for object
detection)
7. **Battery Pack (7.4V-12V)**

---

## **Circuit Connections**

| **Component** | **Arduino
Pin** |
|---------------|----------------|
| **Color Sensor (TCS230)** | |
| S0 | D2 |
| S1 | D3 |
| S2 | D4 |
| S3 | D5 |
| OUT | D6 |
| **Motor Driver (L298N)** | |
| IN1 | D7 |
| IN2 | D8 |
| IN3 | D9 |
| IN4 | D10 |
| ENA (PWM) | D11 |
| ENB (PWM) | D12 |
| **Servo Motor** | A0 |
| **Ultrasonic Sensor (Optional)**
||
| Trig | A1 |
| Echo | A2 |

---

## **Complete Arduino Code**

```cpp
#include <Servo.h>

// **Color Sensor Pins**


#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define sensorOut 6
// **Motor Driver Pins**
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 10
#define ENA 11 // PWM for
Motor A speed
#define ENB 12 // PWM for
Motor B speed

// **Servo for Sorting Arm**


#define SERVO_PIN A0
Servo sortingArm;

// **Ultrasonic Sensor
(Optional)**
#define TRIG_PIN A1
#define ECHO_PIN A2
// **Color Calibration Values
(Adjust Based on Testing)**
int redMin = 50, redMax = 250;
int greenMin = 60, greenMax =
270;
int blueMin = 50, blueMax = 240;
// **Movement Settings**
int searchSpeed = 150; //
Speed while searching
int approachSpeed = 100; //
Slower speed when near object
int turnTime = 500; // Time for
90° turn (adjust based on robot)

void setup() {
Serial.begin(9600);
// **Color Sensor Setup**
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
digitalWrite(S0, HIGH); // Set
frequency scaling to 20%
digitalWrite(S1, LOW);
// **Motor Setup**
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);

// **Servo Setup**
sortingArm.attach(SERVO_PIN);
sortingArm.write(90); // Neutral
position

// **Ultrasonic Sensor Setup


(Optional)**
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

Serial.println("Robot Initialized.
Starting Color Sorting...");
}

void loop() {
searchForObject(); // Robot
moves and searches for objects
if (objectDetected()) { // Checks
if an object is in front
approachObject(); // Moves
closer to the object
String color = readColor(); //
Identifies the color
sortObject(color); // Uses
servo arm to sort
moveBackward(1000); //
Moves back after sorting
delay(1000);
}
}

// **Robot Movement Functions**


void moveForward(int duration) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA,
searchSpeed);
analogWrite(ENB,
searchSpeed);
delay(duration);
stopMotors();
}

void moveBackward(int duration)


{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENA,
searchSpeed);
analogWrite(ENB,
searchSpeed);
delay(duration);
stopMotors();
}

void turnLeft() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA,
searchSpeed);
analogWrite(ENB,
searchSpeed);
delay(turnTime);
stopMotors();
}

void turnRight() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENA,
searchSpeed);
analogWrite(ENB,
searchSpeed);
delay(turnTime);
stopMotors();
}

void stopMotors() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

// **Object Detection (Using


Color Sensor or Ultrasonic)**
bool objectDetected() {
// Option 1: Using Color Sensor
(detects reflectance change)
int red = getRedPW();
if (red < redMax - 50) return
true;

// Option 2: Using Ultrasonic


(uncomment if using)
/*
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration =
pulseIn(ECHO_PIN, HIGH);
int distance = duration * 0.034 /
2;
if (distance < 10) return true; //
Object within 10cm
*/
return false;
}

void searchForObject() {
// Simple search pattern:
Forward, then turn if nothing
found
moveForward(1000);
if (!objectDetected()) {
turnLeft();
moveForward(500);
if (!objectDetected()) {
turnRight();
turnRight();
moveForward(500);
}
}
}

void approachObject() {
// Slower approach for precise
positioning
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA,
approachSpeed);
analogWrite(ENB,
approachSpeed);
delay(800); // Adjust based on
robot speed
stopMotors();
}

// **Color Sensing Functions**


String readColor() {
int red = map(getRedPW(),
redMin, redMax, 255, 0);
int green = map(getGreenPW(),
greenMin, greenMax, 255, 0);
int blue = map(getBluePW(),
blueMin, blueMax, 255, 0);

Serial.print("R:");
Serial.print(red);
Serial.print(" G:");
Serial.print(green);
Serial.print(" B:");
Serial.println(blue);

if (red > 200 && green < 100 &&


blue < 100) return "RED";
if (green > 200 && red < 100 &&
blue < 100) return "GREEN";
if (blue > 200 && red < 100 &&
green < 100) return "BLUE";
return "UNKNOWN";
}
int getRedPW() {
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
return pulseIn(sensorOut,
LOW);
}
int getGreenPW() {
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
return pulseIn(sensorOut,
LOW);
}

int getBluePW() {
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
return pulseIn(sensorOut,
LOW);
}

// **Sorting Arm Function**


void sortObject(String color) {
Serial.print("Sorting: ");
Serial.println(color);
if (color == "RED") {
sortingArm.write(0); // Rotate
arm to RED bin
delay(1000);
}
else if (color == "GREEN") {
sortingArm.write(60); // Rotate
arm to GREEN bin
delay(1000);
}
else if (color == "BLUE") {
sortingArm.write(120); //
Rotate arm to BLUE bin
delay(1000);
}
sortingArm.write(90); // Return
to neutral
}
```

---
## **How It Works**
1. **Robot Movement**
- Moves forward, turns if no
object is
detected.
- Slows down when
approaching an object.

2. **Color Detection**
- Uses the TCS230 sensor to
detect **Red, Green, Blue**
values.
- Maps the readings to
determine the color.

3. **Sorting Mechanism**
- A servo motor moves the arm
to drop the object into the correct
bin.
- Returns to neutral position
after sorting.

---
## **Improvements You Can
Make**
✅ **Add an Ultrasonic Sensor**
for better object detection.
✅ **Use a Conveyor Belt** for
continuous sorting.
✅ **Add an LCD Display** to
show detected colors in real-
time.
✅ **Implement PID Control** for
smoother robot movement.

Would you like any modifications


or additional features? 🚀

You might also like