Obstacle Avoiding Robot
An autonomous 2WD obstacle-avoiding robot built with Arduino Uno, an HC-SR04 ultrasonic sensor, and an SG90 servo that pans the sensor to scan left and right paths. An L298N dual H-bridge drives the motors with dual-speed PWM control, executing90° left, 90° right, or 180° turnaround escapes with fail-safe rejection of invalid readings.
Status
Completed
Technologies
1
Overview
The project implements a low-cost autonomous 2WD robot that navigates indoor environments without human supervision. An Arduino Uno serves as the central processing unit, running a complete navigation loop from power-on to continuous operation. An HC-SR04 ultrasonic sensor measures obstacle distance using the time-of-flight principle, with valid readings confined between2.0 cm and 200.0 cm. An SG90 micro-servo mechanically pans the sensor across a 120-degree arc, allowing a single sensor to evaluate both left and right escape paths. An L298N dual H-bridge module drives two TT gear motors through forward, reverse, and pivot configurations. The control logic operates as a deterministic state machine covering cruising, obstacle detection within a 30 cm threshold, side scanning, and three-way decision making. Speed is managed through two distinct PWM duty cycles, 130/255 for smooth cruising and 230/255 for high-torque pivoting. A fail-safe validation layer rejects out-of-range readings and echo timeouts by halting the chassis instead of steering on false data.
11
Total Component
130 to 230 of 255 PWM
Motor Speed Range
30 cm
Detection Threshold
2470 TK (BDT)
Project Cost
Key Features
- Autonomous Navigation — The robot navigates indoor environments continuously without any human supervision, making all sensing, decision, and movement decisions independently.
- HC-SR04 Ultrasonic Sensing — Measures obstacle distance using the time-of-flight principle with validated readings confined between 2.0 cm and 200.0 cm for reliable perception.
- SG90 Servo Pan Scanning — Mechanically pans the ultrasonic sensor across a 120-degree arc, allowing a single sensor to evaluate both left and right escape paths.
- Three-Way Avoidance Logic — Executes a 90-degree left turn, a 90-degree right turn, or a 180-degree turnaround depending on which direction offers the greater clearance.
- Dual-Speed PWM Control — Manages motor speed through two distinct PWM duty cycles, using 130/255 for smooth cruising and 230/255 for high-torque pivoting.
- Fail-Safe Sensor Validation — Rejects out-of-range readings and echo timeouts by halting the chassis, preventing erratic turns caused by false sensor data.
Technology Stack
Language
Screenshots



Timeline
Feasibility Analysis and Component Selection
Reviewed existing obstacle-avoiding approaches, identified the limitations of single-sensor designs, and finalized the hardware list including Arduino Uno, L298N driver, HC-SR04, SG90 servo, and 2WD chassis within a sub-BDT 3,000 budget.
Hardware Assembly and Wiring
Assembled the chassis, mounted the motors and caster wheel, installed the HC-SR04 on the servo bracket, and completed all pin connections including the removal of the L298N ENA and ENB jumpers with a shared common ground.
Firmware Development and Control Logic
Implemented the distance measurement routine, dual-speed PWM motor control, servo pan scanning, and the deterministic state machine covering cruising, detection, scanning, and three-way escape decisions with fail-safe validation.
Testing, Calibration and Documentation
Calibrated the open-loop90-degree and 180-degree turn durations against actual floor response, executed twelve designed test cases, validated the system output over the Serial Monitor, and compiled the complete project report.
Challenges
- Single-sensor lateral evaluation — A single forward-facing HC-SR04 cannot detect obstacles to the left or right, so the SG90 servo had to physically pan the sensor across a 120-degree arc, adding roughly 1.05 seconds of scanning latency before any escape decision could be made.
- Open-loop turn calibration — With no encoder or IMU feedback available, the 90-degree and 180-degree pivots rely purely on timed motor bursts of 400 ms and 800 ms, causing rotation drift as battery voltage drops from 7.4 V toward 6.2 V.
- Ultrasonic sensor uncertainty — The HC-SR04 produces false readings on angled or soft surfaces, operates with a blind zone below 2 cm, and returns zero on echo timeout, requiring strict validation to prevent the robot from swerving or crashing.
- Servo-induced measurement error — The ultrasonic sensor cannot measure accurately while the servo is still moving, requiring deliberate 350 ms and 450 ms settle delays before each lateral reading, which further extends the avoidance cycle.
- Power supply interference — The SG90 servo draws current spikes that caused Arduino brownout resets when powered from the 5V pin, and the shared 7.4 V battery rail introduced noise into the motor signals, demanding a dedicated regulated supply and a mandatory common ground.
- Motor and sensor polarity mismatches — Hardware assembly introduced reversed motor terminals and inconsistent driver logic that initially made the chassis spin instead of drive forward, requiring careful troubleshooting of the IN1/IN2 and IN3/IN4 pin definitions and terminal wiring.
Solutions
- Resolved by implementing a timed servo pan sequence using the Servo library, sweeping to 150 degrees for the left reading and 30 degrees for the right reading before always restoring the sensor to 90 degrees center for straight-ahead cruising.
- Resolved by empirically calibrating the turn durations against the actual floor surface and battery level, tuning TURN_90_TIME to 400 ms and TURN_180_TIME to 800 ms so that both rotations completed accurately on the test floor.
- Resolved by adding a strict validation layer inside the distance function that rejects any reading below 2.0 cm, above 200.0 cm, or a zero echo duration, returning an invalid marker that halts the chassis instead of steering on false data.
- Resolved by inserting deliberate settle delays of 350 ms after the left sweep and 450 ms after the right sweep, giving the servo mechanism enough time to complete its movement before the ultrasonic reading is captured.
- Resolved by separating the servo supply from the Arduino rail using an external 5V regulated source, and by joining all ground lines together across the battery, L298N, HC-SR04, servo, and Arduino to establish a single common reference.
- Resolved by verifying the physical terminal wiring against the pin definitions and correcting the IN1/IN2 and IN3/IN4 assignments in the source, restoring the correct forward, reverse, and pivot behaviour for both motors.