DIY-Projects

Obstacle Avoiding Robot using Arduino

obstacle-avoiding-robot

← All DIY projects

An obstacle avoiding robot is one of the best first robotics projects. It drives forward on its own, detects objects in its path with an ultrasonic sensor, and then backs up and turns to find a clear way. In this guide you will build one using an Arduino Uno, an HC-SR04 ultrasonic sensor and an L298N motor driver.

You get the full parts list, wiring tables, ready-to-upload code, troubleshooting tips and upgrade ideas. New to Arduino? Read our essential Arduino sensors guide first, or browse more Arduino mini projects.

🎯Level
Beginner to intermediate
⏱️Build time
About 1 to 2 hours
🧩Board
Arduino Uno or compatible
🧠You will learn
Sensors, motor control, decision logic

How an obstacle avoiding robot works

The robot repeats three steps many times every second:

1. SenseThe HC-SR04 sends out an ultrasonic pulse and listens for the echo. The time the echo takes tells the Arduino how far away the object is.
2. ThinkThe Arduino compares the distance with a safe limit, for example 20 cm.
3. ActIf the path is clear, it drives forward. If not, it stops, reverses and turns, then tries again.

Components required

🧩

Arduino Uno or compatible board

The brain of the robot. It reads the sensor and controls the motors.

📏

HC-SR04 ultrasonic sensor

Measures the distance to objects in front of the robot, from about 2 cm to 400 cm.

⚙️

L298N motor driver module

The Arduino cannot power motors directly. The driver switches the motor current for it.

🛞

2 DC gear motors with wheels

Drive the left and right wheels.

🚗

2WD robot chassis kit

The base that holds everything. Usually includes motors, wheels and a caster wheel.

🔋

Battery pack, 7 to 12 V

Powers the motors and the Arduino. Two 18650 cells in a holder (7.4 V) work well.

🔌

Jumper wires and switch

Connect the modules. A small slide switch makes it easy to turn the robot on and off.

🔄

Optional: SG90 servo

Lets the sensor look left and right for a smarter robot (see upgrades).

Wiring: pin connections

Use these pins so the code below works without changes. Keep the two jumper caps on the ENA and ENB pins of the L298N so the motors run at full speed.

Obstacle avoiding robot wiring diagramArduino Uno connected to an HC-SR04 ultrasonic sensor, an L298N motor driver, two DC motors and a battery pack with a switch.Obstacle avoiding robot: wiring diagramPin positions are simplified for clarity. All grounds must be joined (common ground).Arduino UnoUno or compatibleHC-SR04L298NMotor driverLeft motorRight motorVCC5VGNDGNDTRIGD7ECHOD8D5IN1D6IN2D9IN3D10IN4GNDGNDOUT1OUT2OUT3OUT4VINGND12VGNDSwitchBattery 7-12 V+-Power (+)Ground (-)Signal wires
Circuit diagram: Arduino Uno, HC-SR04, L298N motor driver, two DC motors and battery. Scroll sideways on a phone.

HC-SR04 to Arduino

HC-SR04 pinArduino pin
VCC5V
GNDGND
TRIGD7
ECHOD8

L298N to Arduino

L298N pinArduino pin
IN1D5
IN2D6
IN3D9
IN4D10
GNDGND (common ground)

Motors and battery

ConnectionGoes to
Left motorL298N OUT1 and OUT2
Right motorL298N OUT3 and OUT4
Battery +L298N 12V terminal and Arduino VIN, through the switch
Battery -L298N GND and Arduino GND

Always connect the Arduino GND to the L298N GND. Without a common ground the robot will behave randomly. Wiring problems cause most failures, so see wrong sensor connections and incorrect power supply if something does not work.

Step-by-step assembly

Step 1

Build the base

Fix the two motors and wheels to the chassis and add the caster wheel at the back or front.

Step 2

Mount the electronics

Fix the Arduino and the L298N on the chassis with screws or double-sided tape. Keep the battery pack low and in the middle.

Step 3

Fit the sensor

Mount the HC-SR04 at the front, facing straight ahead and slightly above the ground.

Step 4

Wire the motors

Connect the left motor to OUT1 and OUT2 and the right motor to OUT3 and OUT4.

Step 5

Wire the control pins

Follow the tables above for the sensor and the IN1 to IN4 pins.

Step 6

Add power

Connect the battery through the switch. Keep the robot on a stand while you test.

Arduino code

Open the Arduino IDE, paste the code, select your board and port, and click Upload. No extra library is needed.

Obstacle avoiding robot
const int trigPin = 7;
const int echoPin = 8;
const int in1 = 5;   // left motor
const int in2 = 6;
const int in3 = 9;   // right motor
const int in4 = 10;
const int safeDistance = 20;  // cm

long readDistanceCm() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH, 30000);
  if (duration == 0) return 400;      // no echo: path is clear
  return duration * 0.034 / 2;        // convert time to cm
}

void forward() {
  digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
}

void backward() {
  digitalWrite(in1, LOW);  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);  digitalWrite(in4, HIGH);
}

void turnRight() {
  digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);  digitalWrite(in4, HIGH);
}

void stopMotors() {
  digitalWrite(in1, LOW); digitalWrite(in2, LOW);
  digitalWrite(in3, LOW); digitalWrite(in4, LOW);
}

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  long distance = readDistanceCm();
  Serial.println(distance);

  if (distance > safeDistance) {
    forward();
  } else {
    stopMotors();
    delay(200);
    backward();
    delay(400);
    turnRight();
    delay(500);
    stopMotors();
    delay(100);
  }
}

How the code works

  • readDistanceCm() sends a 10 microsecond pulse and times the echo with pulseIn().
  • forward(), backward(), turnRight(), stopMotors() set the L298N inputs to spin the motors in the right directions.
  • loop() drives forward while the path is clear. When something is closer than 20 cm, the robot stops, reverses, turns right and checks again.
  • Serial.println(distance) prints the distance so you can check the sensor in the Serial Monitor at 9600 baud.

This code uses short delay() calls for the turning moves. That is fine here, but for smoother behaviour read about using delay() too much and switch to millis().

Test and tune your robot

📐Change safeDistance. A higher value makes the robot react earlier.
↩️Change the delay after turnRight() to turn more or less.
🔁If a wheel spins the wrong way, swap that motor's two wires on the L298N.
🪑Test on a smooth floor first, then try rooms with furniture.

Troubleshooting

ProblemWhat to check
The robot does not moveCheck the battery, the ENA and ENB jumper caps and the common ground.
It spins in circlesSwap the wires of one motor on the L298N.
Arduino resets when the motors startThe battery is weak or the supply is unstable. Use fresh cells.
It does not see soft or angled objectsUltrasonic waves bounce away from angled and soft surfaces. Test with a flat wall.
Distance reads 0 or 400 all the timeCheck the TRIG and ECHO wires and the 5V and GND connections.

Upgrade ideas

🦾

Add a servo to scan left and right

Mount the sensor on a servo so the robot picks the clearer side.

📱

Control it by Bluetooth

Switch between manual and automatic modes.

🛤️

Make a line follower

Reuse the same chassis with IR sensors.

🖥️

Show distance on a display

Add an LCD to see live readings.

More ideas: see our Top 20 STEM projects and 20 Arduino mini projects.

Frequently asked questions

Which motor driver is best for an obstacle avoiding robot?

The L298N is the most common choice for beginners because it drives two DC motors and is easy to wire. Check the motor driver category for other options.

Can I use an Arduino Nano instead of an Uno?

Yes. The code and pins work the same way. Just make sure you wire to the same digital pins.

Why does my robot hit the wall before it turns?

Increase safeDistance, check that the sensor faces straight ahead and test with fresh batteries so the motors do not slow down.

Do I need a servo motor?

No. The basic robot works without one. A servo is an optional upgrade that lets the sensor look left and right.

Robotics learning with Robopro.in

At Robopro.in, students and makers can find Arduino boards, sensors, motor drivers, motors and robotics kits for building practical projects.

Ready to build your first robot?

Get every part for this project in one place. Schools and STEM labs can also order in bulk.

Conclusion

An obstacle avoiding robot teaches the core ideas of robotics in one simple project: sensing, decision making and motor control. Once it works, you can add a servo, Bluetooth control or a display and keep learning.

Keep reading

Leave a Reply

Your email address will not be published. Required fields are marked *