How to make an obstacle avoiding robot using Arduino & Ultrasonic
sensor
The process of this robot
When powering up this robot will move forward. Also, the ultrasonic sensor
calculates the distance to the front obstacle. In this case, if the front distance
is less than or equal to 10cm, the robot will stop. Then, the servo motor is
turned left side and right side and the ultrasonic sensor receives the distance
on both sides. Afterward, the Arduino board calculates the side of the longest
distance. Then, the robot turns to that side and moves forward. Also, this
process is continuous.
The required components are given below.
Arduino UNO board x1
Ultrasonic sensor x 1
Servo motor x 1
L298N motor driver x 1
Gear motor x 2
Robot wheel x 3 Li-ion battery holder x 1
Li-ion battery x 2
Jumper wires
Foam board
Step 1
Firstly, identify these components.
Step 2
Secondly, cut a piece of foam board as follows.
Step 3
Next, glue the gear motors.
Step 4
Then, glue the Arduino UNO board and L298N motor driver board to
the robot chassis.
Step 5
OK, next connect the robot wheels for the gear motors.
Step 6
Then, glue the Ultrasonic sensor and servo motor as follows.
Step 7
Now, connect these components.
Step 8
Next, attach the rotating wheel to the robot chassis using a piece of
foam board.
Step 9
After, glue the Li-ion battery holder and connect it to the motor driver
board.
Step 10
Next, create the robot head and attach it. Afterwards, connect this
robot to the computer.
Code:
#include <Servo.h>
Servo myservo;
byte servostart = 105;
int distanceleft = 0;
int distanceright = 0;
long t, cm;
//Motor one
#define ENA 9
#define IN1 2
#define IN2 3
//Motor two
#define IN3 4
#define IN4 5
#define ENB 10
//Sensor
#define Trig 6
#define Echo 7
#define Speed 255
void setup() {
myservo.attach(11);
//Motor one
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
//Motor two
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
//Sensor
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
Serial.begin(9600);
start();
void loop() {
getdistance();
Serial.println(cm);
int leftdistance = 0;
int rightdistance = 0;
if (cm <= 20) {
Stop();
delay(200);
leftdistance = leftsee();
rightdistance = rightsee();
if (leftdistance >= rightdistance) {
turnleft();
delay(200);
Stop();
} else {
turnright();
delay(200);
Stop();
}
} else {
forward();
Serial.println("forward");
void forward() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
void turnright() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
void turnleft() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
void Stop() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
void start() {
delay(3000);
for (int a = 0; a < 4; a++) {
myservo.write(servostart);
delay(50);
myservo.write(40);
delay(50);
myservo.write(90);
delay(50);
myservo.write(servostart);
int leftsee() {
myservo.write(servostart);
delay(1000);
myservo.write(175);
delay(1000);
distanceleft = getdistance();
myservo.write(servostart);
return distanceleft;
int rightsee() {
myservo.write(servostart);
delay(1000);
myservo.write(5);
delay(1000);
distanceright = getdistance();
myservo.write(servostart);
return distanceright;
int getdistance() {
digitalWrite(Trig, LOW);
delayMicroseconds(4);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
t = pulseIn(Echo, HIGH);
cm = t / 29 / 2;
return cm;
}
Code explanation
Firstly, the servo motor library is included.
#include <Servo.h>
Servo myservo;
Next, the motor PINs and sensor PINs are defined.
//Motor one
#define ENA 9
#define IN1 2
#define IN2 3
//Motor two
#define IN3 4
#define IN4 5
#define ENB 10
//Sensor
#define Trig 6
#define Echo 7
In the setup function,
void setup() {
myservo.attach(11);//Includes servo motor PIN
//The motor pins are set as output pins
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
//Motor two
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
//The ultrasonic sensor trig pin set as output pin and the echo pin set as an
input pin.
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
//The serial monitor begin
Serial.begin(9600);
//The robot start function is called. It is described below.
start();
In the loop function,
void loop() {
//Gets the distance values
getdistance();
Serial.println(cm);
int leftdistance = 0;
int rightdistance = 0;
// These values are checked using the IF condition. If the values are less than
or equal to 20, the robot stops.
if (cm <= 20) {
Stop();
delay(200);
//The servo motor rotates to the left and takes the distance using the
ultrasonic sensor
leftdistance = leftsee();
//The servo motor rotates to the right and takes the distance using the
ultrasonic sensor
rightdistance = rightsee();
//If the distance to the left is greater than the distance to the right, the robot
turns left
if (leftdistance >= rightdistance) {
turnleft();
delay(200);
Stop();
//If the distance to the right is greater than the distance to the left, the robot
turns right
} else {
turnright();
delay(200);
Stop();
//Otherwise, the robot moves forward
} else {
forward();
Serial.println("forward");
Motor rotation functions,
void forward() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
void turnright() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
void turnleft() {
analogWrite(ENA, Speed);
analogWrite(ENB, Speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
void Stop() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
The robot start function,
void start() {
delay(3000);
for (int a = 0; a < 4; a++) {
myservo.write(servostart);
delay(50);
myservo.write(40);
delay(50);
myservo.write(90);
delay(50);
myservo.write(servostart);
The servo motor turn left function,
int leftsee() {
myservo.write(servostart);
delay(1000);
myservo.write(175);
delay(1000);
distanceleft = getdistance();
myservo.write(servostart);
return distanceleft;
The servo motor turn right function,
int rightsee() {
myservo.write(servostart);
delay(1000);
myservo.write(5);
delay(1000);
distanceright = getdistance();
myservo.write(servostart);
return distanceright;
The ultrasonic sensor function,
int getdistance() {
digitalWrite(Trig, LOW);
delayMicroseconds(4);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
t = pulseIn(Echo, HIGH);
cm = t / 29 / 2;
return cm;
Step 12
Now, select the board and port. After, upload this code to the Arduino board.
Step 13
Lastly, put the batteries into the battery holder and switch ON this robot.
https://www.youtube.com/watch?v=pYX7bHzucxo