Obstacle Avoiding Robot(Robotics Project)
The Line Following Robot is a simple autonomous robot that follows a predefined path marked by a contrasting line on the ground. It utilizes infrared sensors to detect the line and adjust its movement accordingly. This project demonstrates the basic principles of robotics, including sensor integration, decision-making, and motor control.
Project : 10
OBSTACLE
AVOIDING ROBOT
Introduction
The Line Following Robot is a simple autonomous robot that
follows a predefined path marked by a contrasting line on the ground. It
utilizes infrared sensors to detect the line and adjust its movement
accordingly. This project demonstrates the basic principles of robotics,
including sensor integration, decision-making, and motor control.
If you want to post your project ,research or any document related to Ai,Ml,IoT,Robotics then please email us with your image ,details and your project at blog@nepatronix.org
Components Required
- 1) Arduino
Board (or compatible microcontroller)
- 2) Infrared
Sensors (2x)
- 3) Motor
Driver (L298N or similar)
- 4) DC
Motors (2x)
- 5) Robot
Chassis
- 6) Wheels
(2x)
- 7) Power
Source (Battery pack or similar)
- 8)Jumper
Wires(20x)
Pin Configuration
- Motor
Driver:
- in1,
in2, in3, in4: Control pins for motor direction
- Infrared
Sensors:
- R_S:
Right sensor pin
- L_S:
Left sensor pin
Libraries Used
No external libraries are used in this project.
Circuit Diagram:
If you want to post your project ,research or any document related to Ai,Ml,IoT,Robotics then please email us with your image ,details and your project at blog@nepatronix.org
Code:
#include
<ESP32Servo.h>
boolean goesForward =
false;
int distance = 100;
Servo servo_motor;
#define trigPin 5
#define echoPin 18
long duration;
#define in1 27
#define in2 26
#define in3 25
#define in4 33
#define servoPin 15
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT); // Sets the trigPin
as an Output
pinMode(echoPin, INPUT); // Sets the echoPin
as an Input
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
servo_motor.attach(servoPin);
servo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 20) {
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight();
delay(300);
distanceLeft = lookLeft();
delay(300);
if (distance >= distanceLeft) {
turnRight();
moveStop();
}
else {
turnLeft();
moveStop();
}
} else {
moveForward();
}
distance = readPing();
}
int lookRight() {
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}
int lookLeft() {
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}
int readPing() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10
micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave
travel time in microseconds
duration = pulseIn(echoPin, HIGH);
//
Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(20);
if (distance == 0) {
distance = 250;
}
return distance;
}
void moveForward() {
if (!goesForward) {
goesForward = true;
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
}
void moveBackward() {
goesForward = false;
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void turnRight() {
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(500);
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void turnLeft() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
delay(500);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
}
void moveStop() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
Working
- Initialization:
- Set
up the necessary pins for motor control and sensor input.
- Main Loop:
- Continuously
monitor the state of the infrared sensors.
- Based
on the sensor readings, adjust the robot's movement to follow the line.
- Forward Movement:
- If
both sensors detect the line, move forward.
- Right Movement:
- If only the right sensor detects the line, turn right to re-align with the line.
- Left
Movement:
- If
only the left sensor detects the line, turn left to re-align with the
line.
- Stop:
- If
both sensors lose track of the line, stop the robot.
Testing
- Setup:
- Assemble
the robot chassis, attach wheels and motors.
- Mount
the infrared sensors close to the ground with one sensor on each side of
the robot.
- Line
Following:
- Place
the robot on a track with a contrasting line.
- Observe
the robot's behavior as it follows the line.
- Adjust
the sensitivity of the sensors if needed.
- Edge
Cases:
- Test
the robot's performance on sharp turns, intersections, and varying line
thicknesses.
- Ensure
that the robot can recover from deviations and continue following the
line accurately.
Conclusion
The Line Following Robot project demonstrates a fundamental
concept in robotics and automation. By using infrared sensors to detect a
line's presence, the robot can navigate predefined paths autonomously. This
project serves as a starting point for more advanced robotics applications such
as maze-solving robots, industrial automation, and self-driving vehicles.
Further enhancements to this project could include adding PID control for
smoother movement, integrating wireless communication for remote control, and
implementing obstacle avoidance algorithms for increased versatility.