Introduction
Build an autonomous robot that detects and navigates around obstacles using ultrasonic and IR sensors. This comprehensive guide covers everything from design through implementation, testing, and deployment.
Build an autonomous robot that detects and navigates around obstacles using ultrasonic and IR sensors.
Build an autonomous robot that detects and navigates around obstacles using ultrasonic and IR sensors. This comprehensive guide covers everything from design through implementation, testing, and deployment.
Send a 10µs trigger pulse to TRIG pin. HC-SR04 transmits 8 cycles of 40kHz ultrasonic burst and listens for echo. ECHO pin goes HIGH when echo is received. Distance = (pulseIn(ECHO_PIN, HIGH) × 343m/s) / 2 (divide by 2 for round-trip). Effective range: 2–400cm, ±3mm accuracy. Temperature affects speed of sound — apply correction: speed = 331.3 + 0.606 × T(°C). Mount sensor at 15–20cm height to avoid floor reflections.
10 components required for this project.
| # | Component | Purpose | Qty |
|---|---|---|---|
| 1 | Arduino Uno | Main controller | x1 |
| 2 | HC-SR04 Ultrasonic Sensor | Front obstacle distance measurement | x1 |
| 3 | Servo Motor (SG90) | Rotating ultrasonic sensor for scanning | x1 |
| 4 | Sharp IR Sensor (GP2Y0A21) | Left and right side obstacle detection | x2 |
| 5 | L298N Motor Driver | Dual drive motor control | x1 |
| 6 | DC Gear Motors + Wheels | Drive system | x2 |
| 7 | 9V Li-ion Battery Pack | Power supply | x1 |
| 8 | Robot Chassis (2-wheel differential) | Physical platform | x1 |
| 9 | LED Indicators (Red/Green) | Status visualization | x2 |
| 10 | Buzzer | Obstacle detection alert | x1 |
Follow these 6 steps carefully.
Send a 10µs trigger pulse to TRIG pin. HC-SR04 transmits 8 cycles of 40kHz ultrasonic burst and listens for echo. ECHO pin goes HIGH when echo is received. Distance = (pulseIn(ECHO_PIN, HIGH) × 343m/s) / 2 (divide by 2 for round-trip). Effective range: 2–400cm, ±3mm accuracy. Temperature affects speed of sound — apply correction: speed = 331.3 + 0.606 × T(°C). Mount sensor at 15–20cm height to avoid floor reflections.
Mount HC-SR04 on servo at front center. Scan 15°–165° in 15° increments, taking distance readings at each angle. Build a distance map array: distances[12]. After each scan, find the maximum distance (clearest direction) and steer toward it. Scanning frequency: one full sweep every 300ms while moving. Stop motion during scan for more accurate readings, or continue moving at reduced speed.
State machine: FORWARD (straight ahead), SCAN (obstacle detected, perform sweep), TURN (rotating to clear direction), REVERSE (if trapped). Trigger SCAN when front distance < 30cm. Find best direction from scan map. If best direction is left, turn left. If right, turn right. If all directions blocked (distance < 20cm), reverse 30cm then rescan. Use hysteresis: must see > 40cm clearance before resuming forward motion.
Sharp IR sensors provide analog distance output (inversely proportional). Calibrate using lookup table: measure actual distance vs ADC reading at 5cm increments. Use side sensors for corridor navigation: maintain equal distance from both walls (wall-following behavior). If left wall < 15cm → steer right. If right wall < 15cm → steer left. This enables the robot to navigate hallways and maze corridors efficiently.
Implement variable speed: full speed on clear path, 50% speed when obstacle detected at 60cm, 25% when at 30cm. For smooth turning, don't stop one motor completely — set one motor to 60% forward and other to 60% reverse (spin-turn) for sharp obstacles, or 100%/40% (arc turn) for gradual course correction. Arc turns are smoother and faster for wide corridors.
Test in: narrow corridors (< 60cm width — robot must navigate precisely), open room with scattered objects, maze structure, and dynamic obstacles (moving person). Note failure cases: transparent obstacles (glass), very dark or very reflective surfaces (ultrasonic gives false readings), obstacles below sensor height (table legs, raised floors). Add IR sensors lower down to detect low obstacles not seen by ultrasonic.
Core code for obstacle_avoidance.ino:
#include <Servo.h>
Servo scanServo;
#define TRIG 7 #define ECHO 8
#define IN1 4 #define IN2 5 #define ENA 9 #define IN3 6 #define IN4 11 #define ENB 10
#define SAFE_DIST 30 // cm
long getDistance() {
digitalWrite(TRIG, LOW); delayMicroseconds(2);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
return pulseIn(ECHO, HIGH) * 0.0343 / 2;
}
int scanAndFindBest() {
int bestAngle = 90; long maxDist = 0;
for(int a = 15; a <= 165; a += 15) {
scanServo.write(a); delay(200);
long d = getDistance();
if(d > maxDist) { maxDist = d; bestAngle = a; }
}
scanServo.write(90);
return bestAngle; // <90=left, 90=straight, >90=right
}
void setMotors(int L, int R) {
analogWrite(ENA, abs(L)); analogWrite(ENB, abs(R));
digitalWrite(IN1, L>0); digitalWrite(IN2, L<=0);
digitalWrite(IN3, R>0); digitalWrite(IN4, R<=0);
}
void loop() {
long dist = getDistance();
if(dist > SAFE_DIST) {
setMotors(200, 200); // Forward
} else {
setMotors(0, 0); // Stop
int best = scanAndFindBest();
if(best < 90) { setMotors(-150, 150); delay(400); } // Turn left
else if(best > 90) { setMotors(150, -150); delay(400); } // Turn right
else { setMotors(-150, -150); delay(600); } // Reverse
}
}
Test Obstacle Avoidance Robot by verifying each subsystem individually before full integration.
Verify power voltages, check ground connections, use serial monitor for debug.
An interactive simulator will be available here — simulate circuits and run code in-browser without hardware.