Obstacle Avoiding Robot using Arduino and Ultrasonic Sensor

Published  March 12, 2019   2
Obstacle Avoiding Robot Project using Arduino and Ultrasonic Sensor

An Arduino obstacle avoiding robot is a smart, autonomous robot that detects obstacles on its path using sensors and then navigates around the obstacle by changing direction. This obstacle avoidance robot using Arduino project is a great project for newbies who want to learn about robotics and programming. The robot has been provided with an HC-SR04 ultrasonic sensor for obstacle detection and an Arduino Nano/Uno for processing, so it is a great obstacle avoiding robot project for students and hobbyists. This obstacle avoiding technology is used by military organisations and autonomous vehicle developers, so it is a timely model that includes a real-world application of robotics.

If you have just started to work with Arduino, do check out our Arduino Projects and Tutorials. We have a collection of almost 500+ Arduino projects with Code, Circuit diagrams, and detailed explanations, completely free for everyone to build and learn on their own. 

If you want a more advanced version of this project, check out the Obstacle Avoiding Arduino Car. We previously built Obstacle Avoiding Robot using a Raspberry Pi and using PIC Microcontroller. This time, we will build an Obstacle avoiding robot using an ultrasonic sensor and Arduino. Here, an Ultrasonic sensor is used to sense the obstacles in the path by calculating the distance between the robot and the obstacle. If the robot finds any obstacle, it changes direction and continues moving.

How to build obstacle avoiding robot using an Ultrasonic Sensor 

Before building the obstacle avoidance robot using Arduino, it is important to understand how the ultrasonic sensor works because this sensor will play an important role in detecting obstacles. The basic principle behind the working of an ultrasonic sensor is to note down the time taken by the sensor to transmit ultrasonic beams and receive the ultrasonic beams after hitting the surface. Then, the distance is calculated using the formula. In this project, the widely available HC-SR04 Ultrasonic Sensor is used. To use this sensor, a similar approach will be followed as explained above.

Ultrasonic Sensor HC-SR04

 

So, the Trig pin of HC-SR04 is made high for at least 10 us. A sonic beam is transmitted with 8 pulses of 40KHz each.

Ultrasonic Sensor Timing diagram

 

The signal then hits the surface and returns, and is captured by the receiver Echo pin of HC-SR04. The Echo pin had already hit high at the time, sending high.

Working of Obstacle Avoiding Robot

 

The time taken by the beam to return is saved in a variable and converted to distance using appropriate calculations, like below

Distance= (Time x Speed of Sound in Air (343 m/s))/2

 

We used an ultrasonic sensor in many projects. To learn more about Ultrasonic sensors, check other projects related to Ultrasonic sensor

The components for this obstacle avoiding robot can be found easily. In order to make a chassis, any toy chassis can be used or can be custom-made.  

 

Obstacle Avoiding Robot Components

This comprehensive guide teaches you to build a fully functional Arduino obstacle avoiding robot using readily available components. 

  1. Arduino NANO or Uno (any version)
  2. HC-SR04 Ultrasonic Sensor
  3. LM298N Motor Driver Module
  4. 5V DC Motors
  5. Battery
  6. Wheels
  7. Chassis
  8. Jumper Wires

Component

Specification

Value/Range

Purpose

Arduino Controller

Microcontroller

ATmega328P (Uno/Nano)

Main processing unit

HC-SR04 Sensor

Detection Range

2cm - 400cm

Obstacle detection

 

Accuracy

±3mm

Distance precision

 

Beam Angle

15 degrees

Detection coverage

L298N Motor Driver

Output Current

2A per channel

Motor power control

 

Input Voltage

5V - 35V

Power input range

DC Motors

Operating Voltage

3V - 12V

Robot movement

 

Speed Range

100 - 300 RPM

Optimal performance

Power Supply

Recommended Voltage

7.4V Li-Po or 6xAA

System power

Robot Dimensions

Chassis Size

15cm x 12cm (typical)

Physical footprint

Performance Metrics

Detection Response

< 100ms

Obstacle reaction time

 

Operating Temperature

-10°C to +70°C

Environmental range

Obstacle Avoiding Robot Circuit Diagram

The complete Obstacle Avoiding Robot Circuit Diagram for this project is given below. As you can see, it uses an Arduino Nano. But we can also build an obstacle avoidance robot using Arduino UNO with the same circuit (follow the same pinout) and code. Both boards support the same coding for obstacle avoiding robot applications without modification. This diagram of the wiring of an obstacle avoiding robot is to a level of wiring quality that is viable, robust, consistent operational performance, and easy to troubleshoot.

Arduino Obstacle Avoiding Robot Circuit Diagram

Once the circuit is ready, we have to build our obstacle avoiding car by assembling the circuit on top of a robotic chassis as shown below. 

Arduino Obstacle Avoiding Robot Project Hardware

 

Obstacle Avoiding Robot - Arduino Code

The complete program with a demonstration video is given at the end of this project. The program will include setting up the HC-SR04 module and outputting the signals to the Motor Pins to move the motor in the corresponding direction. No libraries will be used in this project. The obstacle avoiding robot Arduino code is similarly constructed professionally in order to follow best practices applicable to embedded programming.

First, define the trig and echo pins of HC-SR04 in the program. In this project, the trig pin is connected to GPIO9 and the echo pin is connected to GPIO10 of Arduino NANO.

int trigPin = 9;      // trig pin of HC-SR04
int echoPin = 10;     // Echo pin of HC-SR04

 

Define pins for the input of the LM298N Motor Driver Module. The LM298N has 4 data input pins used to control the direction of the motor connected to it.

int revleft4 = 4;       //REVerse motion of Left motor
int fwdleft5 = 5;       //ForWarD motion of Left motor
int revright6 = 6;      //REVerse motion of Right motor
int fwdright7 = 7;      //ForWarD motion of Right motor

 

LM298N Motor Driver Module

 

In the setup() function, define the data direction of utilised GPIO pins. The four Motor pins and the Trig pin are set as outputs, and the echo pin is set as OUTPUT and Echo Pin is set as Input.

pinMode(revleft4, OUTPUT);      // set Motor pins as output
pinMode(fwdleft5, OUTPUT);
pinMode(revright6, OUTPUT);
pinMode(fwdright7, OUTPUT); 
pinMode(trigPin, OUTPUT);         // set trig pin as output
pinMode(echoPin, INPUT);          //set echo pin as input to capture reflected waves

 

In the loop() function, get the distance from HC-SR04, and based on the distance, move the motor direction. The distance will show the object distance coming in front of the robot. The Distance is taken by emitting a beam of ultrasonic up to 10 us and receiving it after 10us. To learn more about measuring distance using an Ultrasonic sensor and Arduino, follow the link.

digitalWrite(trigPin, LOW);
delayMicroseconds(2);   
digitalWrite(trigPin, HIGH);     // send waves for 10 us
delayMicroseconds(10);
duration = pulseIn(echoPin, HIGH); // receive reflected waves
distance = duration / 58.2;   // convert to distance
delay(10);

 

If the distance is greater than the defined distance means there is no obstacle in its path and it will move in the forward direction.

            if (distance > 19)            
            {
            digitalWrite(fwdright7, HIGH);                    // move forward
            digitalWrite(revright6, LOW);
            digitalWrite(fwdleft5, HIGH);                                
            digitalWrite(revleft4, LOW);                                                       
            }

 

If the distance is less than the defined distance to avoid an obstacle means there is some obstacle ahead. So in this situation robot will stop for a while and move backwards after then stop again for a while, and then take a turn in another direction.

              if (distance < 18)
             {
             digitalWrite(fwdright7, LOW);  //Stop                
             digitalWrite(revright6, LOW);
             digitalWrite(fwdleft5, LOW);                                
             digitalWrite(revleft4, LOW);
             delay(500);
             digitalWrite(fwdright7, LOW);      //movebackword         
             digitalWrite(revright6, HIGH);
             digitalWrite(fwdleft5, LOW);                                
             digitalWrite(revleft4, HIGH);
             delay(500);
             digitalWrite(fwdright7, LOW);  //Stop                
             digitalWrite(revright6, LOW);
             digitalWrite(fwdleft5, LOW);                                
             digitalWrite(revleft4, LOW);  
             delay(100);  
             digitalWrite(fwdright7, HIGH);       
             digitalWrite(revright6, LOW);   
             digitalWrite(revleft4, LOW);                                 
             digitalWrite(fwdleft5, LOW);  
             delay(500);
            }

The above techniques turn your basic obstacle avoidance robot using Arduino into an intelligent autonomous system, allowing for high-level navigation between multiple arbitrary structures.

Project Summary and GitHub Repository

The project has a full description of the system design, its functionality, and the key implementation details. It includes circuit diagrams, a description of the code, and step-by-step instructions for other hobbyists to build the project.You can easily refer to the complete source code and other files in the GitHub repository.

code and schematics icondownload icon

Frequently Asked Questions on Arduino Obstacle Avoiding Robot

⇥ What components will I need for an obstacle avoiding robot using Arduino? 
Necessary items: Arduino Uno/Nano microcontroller, HC-SR04 ultrasonic sensor, L298N motor driver module, 2 x 5V DC motors, robot chassis, wheels, battery pack (7.4V preferably), jumper wires.

⇥ May I use an Arduino Uno instead of an Arduino Nano to replace the Arduino Nano for this project?
Yes, the Arduino Uno and Nano are both great for obstacle avoiding robots and have the same pin configuration and code structure. The Arduino Nano is advantageous for its size (18x45mm compared to 68x53mm), weight, and easier chassis integration.

⇥ Why does my obstacle avoiding robot not detect obstacles properly? 
Two common problems as to why your robot might not detect obstacles are: improper wiring of the HC-SR04, insufficient power supply (the sensor needs to see a stable 5V), ultrasonic "reflection" interference from surfaces, improper mounting angles of the sensor, and timing issues with the code. Don't forget to check your distance calculation formula: distance = duration * 0.034 / 2, as well as the longer your trigger pulse, the better (10 microseconds minimum). 

⇥ How can I further improve the detection range and accuracy? 
To improve detection, you could mount the HC-SR04 on a servo motor to allow it to "scan" 180 degrees, do some distance filtering in your code, include a couple of sensors to detect obstacles from the sides, and vary the detection threshold distance according to robot speed. You could also try "upgrading" to ultrasonic sensors like the JSN-SR04T, which have a longer detection range. 

So this is how a robot can avoid obstacles in its path without getting stuck anywhere. You can find the coding for obstacle avoiding robot and the video below

Smart Bots in Action: More Arduino Robot Builds

Experience the excitement of building robots that do more than just move; they react, adapt, and respond.

Simple Light Following Robot using Arduino UNO

Simple Light Following Robot using Arduino UNO

Learn to create a simple light-following robot using Arduino, LDR sensors, and an MX1508 motor driver. This beginner-friendly project includes detailed instructions, components list, and coding examples.

Human Following Robot Using Arduino and Ultrasonic Sensor

Human Following Robot Using Arduino and Ultrasonic Sensor

In this project, we will build Human Following Robot using Arduino and Ultrasonic Sensor. This robot can track and follow a person autonomously, making them useful in various scenarios like assistance in crowded areas, navigation support, or even as companions.

 DIY Fire Fighting Robot using Arduino and Ultrasonic Sensor

DIY Fire Fighting Robot using Arduino and Ultrasonic Sensor

In this project, we will make a fire fighting robot using arduino. This fire fighting robot can be used in the modern world which navigates areas and extinguishes fire in a given environment with avoiding any obstacles it faces in its.

Complete Project Code

/* Obstacle Avoiding Robot Using Ultrasonic Sensor and Arduino NANO
 */
int trigPin = 9;      // trig pin of HC-SR04
int echoPin = 10;     // Echo pin of HC-SR04
int revleft4 = 4;       //REVerse motion of Left motor
int fwdleft5 = 5;       //ForWarD motion of Left motor
int revright6 = 6;      //REVerse motion of Right motor
int fwdright7 = 7;      //ForWarD motion of Right motor
long duration, distance;
void setup() {
  
  delay(random(500,2000));   // delay for random time
  Serial.begin(9600);
  pinMode(revleft4, OUTPUT);      // set Motor pins as output
  pinMode(fwdleft5, OUTPUT);
  pinMode(revright6, OUTPUT);
  pinMode(fwdright7, OUTPUT);
  
  pinMode(trigPin, OUTPUT);         // set trig pin as output
  pinMode(echoPin, INPUT);          //set echo pin as input to capture reflected waves
}
void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);   
  digitalWrite(trigPin, HIGH);     // send waves for 10 us
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH); // receive reflected waves
  distance = duration / 58.2;   // convert to distance
  delay(10);
    // If you dont get proper movements of your robot then alter the pin numbers
  if (distance > 19)            
  {
    digitalWrite(fwdright7, HIGH);                    // move forward
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft5, HIGH);                                
    digitalWrite(revleft4, LOW);                                                       
  }
  if (distance < 18)
  {
    digitalWrite(fwdright7, LOW);  //Stop                
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft5, LOW);                                
    digitalWrite(revleft4, LOW);
    delay(500);
    digitalWrite(fwdright7, LOW);      //movebackword         
    digitalWrite(revright6, HIGH);
    digitalWrite(fwdleft5, LOW);                                
    digitalWrite(revleft4, HIGH);
    delay(500);
    digitalWrite(fwdright7, LOW);  //Stop                
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft5, LOW);                                
    digitalWrite(revleft4, LOW);  
    delay(100);  
    digitalWrite(fwdright7, HIGH);       
    digitalWrite(revright6, LOW);   
    digitalWrite(revleft4, LOW);                                 
    digitalWrite(fwdleft5, LOW);  
    delay(500);
  }
}
Video

Have any question related to this Article?

Comments

code is invalid this guy misleading people so plz dont waste your time on this.

Add New Comment

Login to Comment Sign in with Google Log in with Facebook Sign in with GitHub