Obstacle Avoiding Robot
Build a smart robot that detects and avoids obstacles using an ultrasonic sensor and Arduino.
What is an Obstacle Avoiding Robot?
An obstacle avoiding robot is a mobile robot that can detect objects in its path and change direction to avoid hitting them.
It uses an ultrasonic sensor to measure the distance to objects ahead. When an object is too close, the robot stops and turns to find a clear path.
This is one of the most popular beginner robotics projects because it teaches sensor integration, decision making, and motor control.
Components Required
You will need:
- Arduino Uno
- HC-SR04 ultrasonic sensor
- L298N motor driver
- 2 DC motors with wheels
- 1 caster wheel
- Robot chassis
- 9V battery or battery pack
- Jumper wires
- Optional: servo motor for rotating the sensor
How the Ultrasonic Sensor Works
The HC-SR04 ultrasonic sensor works like a bat's echolocation system.
It sends out a high-frequency sound pulse from the Trig pin.
The sound bounces off objects and returns to the Echo pin.
The Arduino measures the time between sending and receiving the sound.
Using the speed of sound (343 m/s), it calculates the distance to the object.
Distance = (Time × Speed of Sound) ÷ 2
Divided by 2 because sound travels to the object and back
The sensor can measure distances from 2 cm to 400 cm with good accuracy.
Step 1: Assemble the Robot
Mount the two DC motors on the rear of the chassis and attach wheels.
Attach the caster wheel to the front.
Mount the Arduino and L298N motor driver on the chassis.
Mount the ultrasonic sensor at the front of the robot, facing forward.
Make sure the sensor has a clear view ahead without any part of the chassis blocking it.
Step 2: Wiring Connections
- HC-SR04 VCC to Arduino 5V
- HC-SR04 GND to Arduino GND
- HC-SR04 Trig to Arduino pin 7
- HC-SR04 Echo to Arduino pin 8
- Motor A to L298N OUT1 and OUT2
- Motor B to L298N OUT3 and OUT4
- L298N IN1 to Arduino pin 5
- L298N IN2 to Arduino pin 6
- L298N IN3 to Arduino pin 9
- L298N IN4 to Arduino pin 10
- Battery to L298N 12V and GND
- L298N GND to Arduino GND
Step 3: Programming the Robot
The program follows a simple logic:
- Continuously measure the distance to the nearest object
- If distance is greater than 20 cm — move forward
- If distance is less than 20 cm — stop, then turn right
- After turning, check distance again before moving forward
The distance measurement function sends a pulse on the Trig pin, then reads the Echo pin to calculate time.
You can improve the robot by adding a servo motor to rotate the sensor left and right, checking multiple directions before choosing the best path.
Advanced: Adding a Servo Scanner
For a smarter robot, mount the ultrasonic sensor on a servo motor.
When an obstacle is detected, the servo rotates the sensor left and right to scan the area.
The robot then turns toward the direction with the most open space.
This makes the robot much better at navigating complex environments.
Troubleshooting
- Robot does not detect obstacles — check ultrasonic sensor wiring and ensure Trig/Echo pins are correct
- Robot detects obstacles too late — increase the detection distance threshold in your code
- Sensor gives inconsistent readings — add a small delay between measurements
- Robot turns the wrong way — swap motor wires or adjust code logic
- Robot moves erratically — check battery charge and motor connections
!Key Points
- Ultrasonic sensors measure distance using sound wave reflection
- The robot moves forward when the path is clear and turns when obstacles are detected
- A threshold distance (usually 20 cm) triggers the avoidance behavior
- Adding a servo scanner makes the robot smarter at navigation
- This project teaches sensor integration, conditional logic, and motor control
