DIY Obstacle Avoiding Robot
by kinte ryder in Circuits > Arduino
9400 Views, 750 Favorites, 0 Comments
DIY Obstacle Avoiding Robot


This time its a obstacle avoiding robot, which use a ultrasonic sensor , two motors , a motor driver and arduino uno to avoid objects. In this project a servo motor is shown but it is not used. It happens because I didn't find a ultrasonic bracket and I try to make one by my self it was successful but it was programmed to rotate and find the nearest area to go but it didn't rotate properly so I reprogrammed it not to rotate and if it detects a object it must go backwards and turn to right and continue the process. It is not a big problem and you can find a ultrasonic sensor bracket and fix it to the servo motor. I will put both codes, with servo and without servo. And lets make it!!!
Things You Need +








Heres the things you needed to make this project.
- Arduino Uno Board + Cable.
- Ultrasonic Sensor HC-SR04.
- Ultrasonic Sensor bracket(I will tell you how to make but you can buy one if you can find).
- H-Bridge L298N Motor driver.
- Bovine wheel.
- Motors with Wheels.
- A plate or something like in the picture.
- 2 9v batteries.
- 2 9v batteries holders.
- A servo motor sg90.(Optional)
- A half breadboard.
- Jumper wires.
- Small cardboard (Optional).
- Cable ties.
The Bracket (Optional)





Remember that this is optional and you can always buy a ultrasonic sensor bracket. First take a cardboard and fold it as shown and also fix it as shown and make a hole in the centre.Then fix the the bracket to the servo.
Solder the Motors!

Now solder the motors and tape it.
Mount All in Place!


Now mount the motors, arduino uno, H-Bridge L298N Motor Driver, Servo motor with Ultrasonic Sensor or only ultrasonic sensor(its optional) and the half breadboard.
Connections +


Do all the connections as shown in the picture.
Hot Glue and Cable Ties!



Now for the power supply I use a 2 9v batteries. In here we must think far and because of that I hot glue the holders on the motor. And there was a problem in this project. It was that the ultrasonic sensor was folded downwards and it was fixed by hot gluing a small piece of cardboard. Also for the cleanness of the robot I tie all the cables with cable ties.
Programming.........
There are 2 types of coding for this robot one is with the servo and without servo. I have use without the servo. But don't worry because I will put both with the servo and without the servo codes. And for both codes you must download the newping library, which you can download from below URL.
https://bitbucket.org/teckel12/arduino-new-ping/do..
You must put the newping library to the libraries folder.
Now heres the code for with the servo.
#include <Servo.h> //Servo motor library. This is standard library
#include <NewPing.h> //Ultrasonic sensor function library. You must install this library//our L298N control pins const int LeftMotorForward = 7; const int LeftMotorBackward = 6; const int RightMotorForward = 4; const int RightMotorBackward = 5;
//sensor pins #define trig_pin A1 //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; //our servo name
void setup(){
pinMode(RightMotorForward, OUTPUT); pinMode(LeftMotorForward, OUTPUT); pinMode(LeftMotorBackward, OUTPUT); pinMode(RightMotorBackward, OUTPUT); servo_motor.attach(10); //our servo pin
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; delay(100); }
int readPing(){ delay(70); int cm = sonar.ping_cm(); if (cm==0){ cm=250; } return cm; }
void moveStop(){ digitalWrite(RightMotorForward, LOW); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorBackward, LOW); digitalWrite(LeftMotorBackward, LOW); }
void moveForward(){
if(!goesForward){
goesForward=true; digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); } }
void moveBackward(){
goesForward=false;
digitalWrite(LeftMotorBackward, HIGH); digitalWrite(RightMotorBackward, HIGH); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorForward, LOW); }
void turnRight(){
digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorBackward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorForward, LOW); delay(500); digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); }
void turnLeft(){
digitalWrite(LeftMotorBackward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorBackward, LOW);
delay(500); digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); }
And here's the code for without the servo.
#include <NewPing.h> //Ultrasonic sensor function library. You must install this library
//our L298N control pins const int LeftMotorForward = 7; const int LeftMotorBackward = 6; const int RightMotorForward = 5; const int RightMotorBackward = 4;
//sensor pins #define trig_pin A1 //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
void setup(){
pinMode(RightMotorForward, OUTPUT); pinMode(LeftMotorForward, OUTPUT); pinMode(LeftMotorBackward, OUTPUT); pinMode(RightMotorBackward, OUTPUT); }
void loop(){
int distanceRight = 0; int distanceLeft = 0; delay(50);
if (distance <= 20){ moveStop(); delay(300); moveBackward(); delay(400); moveStop(); delay(300);
if (distance >= distanceLeft){ turnRight(); moveStop(); } else{ turnLeft(); moveStop(); } } else{ moveForward(); } distance = readPing(); }
int readPing(){ delay(70); int cm = sonar.ping_cm(); if (cm==0){ cm=250; } return cm; }
void moveStop(){ digitalWrite(RightMotorForward, LOW); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorBackward, LOW); digitalWrite(LeftMotorBackward, LOW); }
void moveForward(){
if(!goesForward){
goesForward=true; digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); } }
void moveBackward(){
goesForward=false;
digitalWrite(LeftMotorBackward, HIGH); digitalWrite(RightMotorBackward, HIGH); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorForward, LOW); }
void turnRight(){
digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorBackward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorForward, LOW); delay(500); digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); }
void turnLeft(){
digitalWrite(LeftMotorBackward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorBackward, LOW);
delay(500); digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); }
Done!!!

And our project is done I wish you very good luck on making a Obstacle Avoiding Robot.
And don't forget to vote me!!?????????