obstacle avoiding robot using Arduino
obstacle avoiding robot using Arduino
sensor
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.
Ultrasonic sensor x 1
Servo motor x 1
Gear motor x 2
Step 1
Firstly, identify these components.
Step 2
Step 3
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
Step 7
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;
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
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;
Stop();
delay(200);
leftdistance = leftsee();
rightdistance = rightsee();
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);
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
#include <Servo.h>
Servo myservo;
//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
void setup() {
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);
Serial.begin(9600);
start();
void loop() {
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.
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
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();
} 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);
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;
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