DIY Obstacle Avoiding Robot

5,258

745

24

This time its a obstacle avoiding robot, which use a ultrasonic sensor , two motors , a motor driver and arduino unoto 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!!!

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Things You Need +

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.

Step 2: 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.

Step 3: Solder the Motors!

Now solder the motors and tape it.

Step 4: 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.

Step 5: Connections +

Do all the connections as shown in the picture.

Step 6: 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.

Step 7: 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); }

Step 8: 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!!?????????

Arduino Contest 2017

Participated in the
Arduino Contest 2017

Wheels Contest 2017

Participated in the
Wheels Contest 2017

Design For Kids Challenge

Participated in the
Design For Kids Challenge

Be the First to Share

    Recommendations

    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    24 Discussions

    None
    Deeiz

    Question 9 months ago on Step 8

    I have set it all up from the schematic and I am pretty confident that it is the same - the only difference is that I have put switches off the batteries. When I power up Arduino board the servo and ultrasonic censor works - however when I power up the motor the servo and ultrasonic sensor stop reacting and the wheels keep going forward. I know the wiring is a bit messy - any help would be greatly appreciated.

    I have used the sketch for the servo.

    Kind regards

    Deeiz

    IMG_9324.jpgIMG_9326.jpgIMG_9325.jpg
    None
    VinayK152

    Question 11 months ago

    Does this work 100%?

    None
    DanM5

    1 year ago

    Nice work. You can also use a PWM signal on pins 3,5,6 and 9 to change the speed of the motors using analogWrite(). Remember that not all pins support PWM.

    2 replies