RoboticsBeginnerPRJ-098

Obstacle Avoidance Robot Using Arduino

Drives on its own and steers around anything in its path.

Built and tested by Anju.A · Industrial Robotics Institute

Circuit diagram of the Obstacle Avoidance Robot using Arduino Uno
Circuit diagram · Arduino Uno
LevelBeginner
Build time2–3 hours
BoardArduino Uno
Parts8 components

What is the Obstacle Avoidance Robot?

A classic autonomous robot that never bumps into things. An HC-SR04 ultrasonic sensor at the front measures the distance ahead; when something is within 20 cm, the Arduino stops, reverses and turns right, then carries on forward.

How it works

  1. The ultrasonic sensor measures the distance in front of the robot.
  2. If the path is clear (20 cm or more), the robot drives forward.
  3. If an obstacle is closer than 20 cm, it stops for half a second.
  4. It reverses for 1 second, turns right for 1 second, then continues.

Parts list

ComponentQty
Arduino Uno1
Ultrasonic Sensor (HC-SR04)1
L298N Motor Driver Module1
BO Gear Motors2
Robot Chassis with Wheels1
18650 Lithium Cells2
2-cell 18650 Battery Holder with Switch1
Jumper WiresAs needed

Software

  • Arduino IDE

Wiring and connections

FromConnect to
Ultrasonic VCC5V
Ultrasonic GNDGND
Ultrasonic TRIGPin 2
Ultrasonic ECHOPin 3
L298N IN1 (left motor)A0
L298N IN2 (left motor)A1
L298N IN3 (right motor)A2
L298N IN4 (right motor)A3
Battery +L298N 12V
Battery −L298N GND
Arduino GNDL298N GND

How to build the Obstacle Avoidance Robot

  1. Build the chassis with both motors, wheels and the battery holder.
  2. Mount the ultrasonic sensor at the front, facing forward.
  3. Connect TRIG to pin 2 and ECHO to pin 3.
  4. Connect L298N IN1–IN4 to A0–A3 and the motors to its outputs.
  5. Power the L298N from the battery pack and join the grounds.
  6. Upload the code and let it roam.

Obstacle Avoidance Robot code

Complete, tested Arduino C++ code for this project. Copy it into Arduino IDE and upload it to the Arduino Uno.

int trigPin = 2;
int echoPin = 3;

int LM1 = A0;
int LM2 = A1;
int RM1 = A2;
int RM2 = A3;

long duration;
int distance;

int getDistance()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  return duration * 0.034 / 2;
}

void forward()   { digitalWrite(LM1, HIGH); digitalWrite(LM2, LOW);  digitalWrite(RM1, HIGH); digitalWrite(RM2, LOW);  }
void backward()  { digitalWrite(LM1, LOW);  digitalWrite(LM2, HIGH); digitalWrite(RM1, LOW);  digitalWrite(RM2, HIGH); }
void right()     { digitalWrite(LM1, HIGH); digitalWrite(LM2, LOW);  digitalWrite(RM1, LOW);  digitalWrite(RM2, HIGH); }
void stopRobot() { digitalWrite(LM1, LOW);  digitalWrite(LM2, LOW);  digitalWrite(RM1, LOW);  digitalWrite(RM2, LOW);  }

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LM1, OUTPUT);
  pinMode(LM2, OUTPUT);
  pinMode(RM1, OUTPUT);
  pinMode(RM2, OUTPUT);
}

void loop()
{
  distance = getDistance();

  if (distance < 20)
  {
    stopRobot();
    delay(500);
    backward();
    delay(1000);
    right();
    delay(1000);
  }
  else
  {
    forward();
  }
}

Real-world uses

  • Robot vacuum cleaners
  • Autonomous delivery robots
  • Robotics competitions
  • Intro to autonomous navigation

Frequently asked questions

What is a Obstacle Avoidance Robot?

A classic autonomous robot that never bumps into things. An HC-SR04 ultrasonic sensor at the front measures the distance ahead; when something is within 20 cm, the Arduino stops, reverses and turns right, then carries on forward.

What components are needed for the Obstacle Avoidance Robot?

You need: Arduino Uno (1), Ultrasonic Sensor (HC-SR04) (1), L298N Motor Driver Module (1), BO Gear Motors (2), Robot Chassis with Wheels (1), 18650 Lithium Cells (2), 2-cell 18650 Battery Holder with Switch (1), Jumper Wires. Software: Arduino IDE.

Is the Obstacle Avoidance Robot project good for beginners?

Yes. It is rated beginner and takes about 2–3 hours to build, using the Arduino Uno. The guide on this page explains every connection and step.

Where can I get the code for the Obstacle Avoidance Robot?

The complete, tested Arduino C++ code is free on this page. Copy it into Arduino IDE and upload it to the board.

Can I buy a kit for the Obstacle Avoidance Robot?

Yes. IRI sells a kit with the parts for this project (PRJ-098). Message IRI on WhatsApp at +91 86060 82634 for the kit price and delivery details.

Industrial Robotics Institute
Logo