Obstacle Avoiding Car Using Servo Motor | How to use Servo Motor with Obstacle Avoiding Robot
Introduction:
We are glad that you Avoided many obstacles and found a straight path, just like our Obstacle Avoiding Robot. Do you want your Obstacle Avoiding Robot to solve a maze? Do you want to Upgrade your Obstacle Avoiding Robot? Today, we will be making an obstacle-avoiding robot using Servo Motor. Go through this article till the end and make your Obstacle Avoiding Robot using Servo Motor.
What is Servo Motor?
Components Required:
1)
Arduino Uno
2)
Car Chassis
3)
Motor Driver L298N
4)
Servo Motor
5)
Ultrasonic Sensor
6)
12V Battery Pack
7) Jumper Wires
Circuit Diagram
Follow the Circuit Diagram given below to make your Obstacle
Avoiding Robot
Following is the code for this project
Note: You first need to install the two libraries (Servo and NewPing) to execute this code. The direction for installing is Arduino IDE>>Sketch>>Include Library>>Manage Libraries>>Type the name of library>>Install.
#include <Servo.h> //Servo motor library | |
#include <NewPing.h> //Ultrasonic sensor function library | |
const int LeftForward = 10;// L298N control pins | |
const int LeftBackward = 11; | |
const int RightForward = 12; | |
const int RightBackward = 13; | |
const int ENA = 3; | |
const int ENB = 5; | |
#define trig_pin A1 //sensor pins - analog input 1 | |
#define echo_pin A2 //analog input 2 | |
#define maximum_distance 200 | |
boolean goesForward = false; | |
int distance = 100; | |
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function | |
Servo servo_motor; //servo name | |
void setup(){ | |
pinMode(RightForward, OUTPUT); | |
pinMode(LeftForward, OUTPUT); | |
pinMode(LeftBackward, OUTPUT); | |
pinMode(RightBackward, OUTPUT); | |
servo_motor.attach(9); //servo pin | |
servo_motor.write(90); | |
delay(2000); | |
distance = readPing(); | |
delay(100); | |
distance = readPing(); | |
delay(100); | |
distance = readPing(); | |
delay(100); | |
distance = readPing(); | |
delay(100); | |
} | |
void loop(){ | |
analogWrite(ENA, 100); | |
analogWrite(ENB, 100); | |
int distanceRight = 0; | |
int distanceLeft = 0; | |
delay(50); | |
if (distance <= 20){ | |
moveStop(); // obstacle probably on the route forward, so stop | |
delay(300); | |
moveBackward(); | |
delay(400); | |
moveStop(); | |
delay(300); | |
distanceRight = lookRight(); | |
delay(500); | |
distanceLeft = lookLeft(); | |
delay(500); | |
if (distance >= distanceLeft){ | |
turnRight(); // calculate in which direction the obstacle is more far | |
moveStop(); | |
} | |
else{ | |
turnLeft(); | |
moveStop(); | |
} | |
} | |
else{ | |
moveForward(); | |
} | |
distance = readPing(); | |
} | |
int lookRight(){ | |
servo_motor.write(180); | |
delay(500); | |
int distance = readPing(); | |
delay(100); | |
servo_motor.write(115); | |
return distance; | |
} | |
int lookLeft(){ | |
servo_motor.write(0); | |
delay(500); | |
int distance = readPing(); | |
delay(100); | |
servo_motor.write(115); | |
return distance; | |
delay(100); | |
} | |
int readPing(){ | |
delay(70); | |
int cm = sonar.ping_cm(); | |
if (cm==0){ | |
cm=250; | |
} | |
return cm; | |
} | |
void moveStop(){ | |
digitalWrite(RightForward, LOW); | |
digitalWrite(LeftForward, LOW); | |
digitalWrite(RightBackward, LOW); | |
digitalWrite(LeftBackward, LOW); | |
} | |
void moveForward(){ | |
if(!goesForward){ | |
goesForward=true; | |
digitalWrite(LeftForward, HIGH); | |
digitalWrite(RightForward, HIGH); | |
digitalWrite(LeftBackward, LOW); | |
digitalWrite(RightBackward, LOW); | |
} | |
} | |
void moveBackward(){ | |
goesForward=false; | |
digitalWrite(LeftBackward, HIGH); | |
digitalWrite(RightBackward, HIGH); | |
digitalWrite(LeftForward, LOW); | |
digitalWrite(RightForward, LOW); | |
} | |
void turnRight(){ | |
digitalWrite(LeftForward, HIGH); | |
digitalWrite(RightBackward, HIGH); | |
digitalWrite(LeftBackward, LOW); | |
digitalWrite(RightForward, LOW); | |
delay(500); | |
digitalWrite(LeftForward, HIGH); | |
digitalWrite(RightForward, HIGH); | |
digitalWrite(LeftBackward, LOW); | |
digitalWrite(RightBackward, LOW); | |
} | |
void turnLeft(){ | |
digitalWrite(LeftBackward, HIGH); | |
digitalWrite(RightForward, HIGH); | |
digitalWrite(LeftForward, LOW); | |
digitalWrite(RightBackward, LOW); | |
delay(500); | |
digitalWrite(LeftForward, HIGH); | |
digitalWrite(RightForward, HIGH); | |
digitalWrite(LeftBackward, LOW); | |
digitalWrite(RightBackward, LOW); | |
} |
Hardware Implementation and Demo:
For Detailed Hardware setup, we suggest you Watch our
YouTube Video
Endnote
We hope that you were able to upgrade your Obstacle Avoiding
Robot. In case of any query Feel free to comment below or contact us at support@deeplift.tech
Happy Learning!
Comments
Post a Comment