DIY Obstacle Avoiding Robot

by kinte ryder in Circuits > Arduino

9358 Views, 750 Favorites, 0 Comments

DIY Obstacle Avoiding Robot

DSC02819.png
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 +

DSC02714.JPG
DSC02712.JPG
DSC02710.JPG
DSC02716.JPG
DSC02701.JPG
DSC02743.JPG
DSC02709.JPG
DSC02700.JPG

Heres the things you needed to make this project.

  1. Arduino Uno Board + Cable.
  2. Ultrasonic Sensor HC-SR04.
  3. Ultrasonic Sensor bracket(I will tell you how to make but you can buy one if you can find).
  4. H-Bridge L298N Motor driver.
  5. Bovine wheel.
  6. Motors with Wheels.
  7. A plate or something like in the picture.
  8. 2 9v batteries.
  9. 2 9v batteries holders.
  10. A servo motor sg90.(Optional)
  11. A half breadboard.
  12. Jumper wires.
  13. Small cardboard (Optional).
  14. Cable ties.

The Bracket (Optional)

DSC02653.JPG
DSC02659.JPG
DSC02660.JPG
DSC02662.JPG
DSC02664.JPG

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!

DSC02706.JPG

Now solder the motors and tape it.

Mount All in Place!

DSC02717.JPG
DSC02721.JPG

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 +

Capjmjture.PNG
DSC02747.JPG

Do all the connections as shown in the picture.

Hot Glue and Cable Ties!

DSC02748.JPG
DSC02758.JPG
DSC02820.JPG

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!!!

DSC02819.JPG

And our project is done I wish you very good luck on making a Obstacle Avoiding Robot.

And don't forget to vote me!!?????????