An Arduino-based autonomous robot that uses an HC-SR04 ultrasonic sensor mounted on a servo to scan its surroundings and navigate around obstacles in real time using an L293D motor driver.
The Obstacle Avoidance Robot is a fully autonomous wheeled robot built on an Arduino UNO. An HC-SR04 ultrasonic sensor continuously measures the distance to objects ahead. When an obstacle is detected within 20 cm, the robot stops, reverses slightly, then uses a servo to scan left and right — choosing the direction with more clearance to navigate around the obstacle. When the path is clear, it drives forward again.
- 📡 Ultrasonic sensing — HC-SR04 measures front distance in real time using time-of-flight
- 🔄 Servo-mounted scanning — sensor sweeps left (160°) and right (20°) to compare clearance on both sides
- 🧠 Autonomous decision-making — robot selects the better direction based on distance comparison
- 🚗 Full motor control — forward, backward, turn left, turn right, and stop via L293D motor driver
- 🖥️ Serial monitoring — front, left, and right distances streamed via Serial Monitor (9600 baud)
- 🔋 External battery powered — 9V battery drives the motors independently of the Arduino
| Component | Quantity |
|---|---|
| Arduino UNO | 1 |
| HC-SR04 Ultrasonic Sensor | 1 |
| SG90 Servo Motor | 1 |
| L293D Motor Driver IC | 1 |
| DC Gear Motors | 2 |
| 9V Battery | 1 |
| Breadboard | 1 |
| Jumper Wires | As needed |
| Component | Pin | Arduino Pin |
|---|---|---|
| HC-SR04 | VCC | 5V |
| HC-SR04 | GND | GND |
| HC-SR04 | TRIG | Digital 9 |
| HC-SR04 | ECHO | Digital 10 |
| Servo Motor | Signal | Digital 3 |
| Servo Motor | VCC | 5V |
| Servo Motor | GND | GND |
| L293D | IN1 | Digital 6 |
| L293D | IN2 | Digital 7 |
| L293D | IN3 | Digital 4 |
| L293D | IN4 | Digital 5 |
| L293D | VCC1 (Logic) | 5V |
| L293D | VCC2 (Motor) | 9V Battery (+) |
| L293D | GND | GND |
Built and simulated in Tinkercad
Breadboard View:
Schematic View:
- Arduino IDE (1.8.x or 2.x)
Servo.hlibrary — included with the Arduino IDE by default
- Clone the repository
git clone https://github.com/deep-chatterjee/Obstacle-Avoidance-Robot.git
cd Obstacle-Avoidance-Robot-
Open the sketch
- Open
Obstacle_Avoidance_Robot.inoin the Arduino IDE - Select your board: Arduino UNO
- Select the correct COM port
- Open
-
Upload to your board
- Click Upload
- Open Serial Monitor (baud rate:
9600) to observe live distance readings
HC-SR04 measures front distance
↓
frontDistance > 20 cm?
↙ ↘
YES NO
↓ ↓
Move Forward Stop Robot
↓
Move Backward (400ms)
↓
Servo scans LEFT → measure leftDistance
↓
Servo scans RIGHT → measure rightDistance
↓
leftDistance > rightDistance?
↙ ↘
YES NO
↓ ↓
Turn Left Turn Right
↓
Resume Forward
Obstacle-Avoidance-Robot/
├── Obstacle_Avoidance_Robot.ino # Arduino sketch
├── Obstacle_Avoidance_Robot_Circuit.png # Tinkercad breadboard view
├── Obstacle_Avoidance_Robot_Schematic.png # Tinkercad schematic view
└── README.md
- Using
pulseIn()with the HC-SR04 for accurate distance measurement - Controlling a servo motor to physically sweep a sensor for environmental scanning
- Driving dual DC motors in both directions using an L293D H-bridge motor driver
- Implementing autonomous decision-making logic based on real-time sensor comparisons
- Managing multiple
delay()-based timing sequences for coordinated robot movement
- Replace
delay()-based timing with a non-blocking approach usingmillis()for smoother movement - Add PWM speed control via the L293D enable pins for variable motor speed
- Implement a continuous 180° servo sweep instead of fixed left/right snapshots
- Add IR sensors on the sides for faster edge and wall detection
- Enable Bluetooth control (HC-05) for a manual override mode
- Mount on a proper robot chassis for real-world testing beyond simulation
Deep Chatterjee
GitHub
This project is licensed under the MIT License — see LICENSE for details.

