Introduction: DIY Obstacle Avoiding Robot
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!!!
Step 1: 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.
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!!?????????

Participated in the
Arduino Contest 2017

Participated in the
Wheels Contest 2017

Participated in the
Design For Kids Challenge
24 Discussions
Question 2 years 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
Question 2 years ago
Does this work 100%?
3 years 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.
Reply 2 years ago
will mini breadboard work
Reply 3 years ago
thankx for the feedback :D:D:D:D:D:D:D:D
2 years ago
will mini breadboard work
3 years ago
Great project like it !
Reply 3 years ago
Thank you
3 years ago
I vote you :D:D:D:D:D:D:D:D:DD:D:D
Reply 3 years ago
I apprciate that
3 years ago
Nice cover picture
Reply 3 years ago
thankx
3 years ago
good
Reply 3 years ago
thank you
3 years ago
Its a good weekend project
Reply 3 years ago
welcome
3 years ago
Nice background for the cover picture?
Reply 3 years ago
Thankx
3 years ago
good work
Reply 3 years ago
Thank you very much