Obstacle Avoiding Robot using Arduino
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.
Beginner to intermediate
About 1 to 2 hours
Arduino Uno or compatible
Sensors, motor control, decision logic
How an obstacle avoiding robot works
The robot repeats three steps many times every second:
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.
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.
HC-SR04 to Arduino
| HC-SR04 pin | Arduino pin |
|---|---|
| VCC | 5V |
| GND | GND |
| TRIG | D7 |
| ECHO | D8 |
L298N to Arduino
| L298N pin | Arduino pin |
|---|---|
| IN1 | D5 |
| IN2 | D6 |
| IN3 | D9 |
| IN4 | D10 |
| GND | GND (common ground) |
Motors and battery
| Connection | Goes to |
|---|---|
| Left motor | L298N OUT1 and OUT2 |
| Right motor | L298N 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
Build the base
Fix the two motors and wheels to the chassis and add the caster wheel at the back or front.
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.
Fit the sensor
Mount the HC-SR04 at the front, facing straight ahead and slightly above the ground.
Wire the motors
Connect the left motor to OUT1 and OUT2 and the right motor to OUT3 and OUT4.
Wire the control pins
Follow the tables above for the sensor and the IN1 to IN4 pins.
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.
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
Troubleshooting
| Problem | What to check |
|---|---|
| The robot does not move | Check the battery, the ENA and ENB jumper caps and the common ground. |
| It spins in circles | Swap the wires of one motor on the L298N. |
| Arduino resets when the motors start | The battery is weak or the supply is unstable. Use fresh cells. |
| It does not see soft or angled objects | Ultrasonic waves bounce away from angled and soft surfaces. Test with a flat wall. |
| Distance reads 0 or 400 all the time | Check 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.
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.