Autonomous Obstacle Avoiding Robot with Ultrasonic Sensor & SG90 Servo
Build an intelligent self-driving robot that scans its surrounding environment across 180 degrees using an acoustic radar, detects walls and physical barriers, and calculates the clearest path forward in real time.
1. Aim of the Project
Project Objective
The aim is to develop a self-navigating obstacle avoidance rover that uses an HC-SR04 ultrasonic rangefinder mounted on an SG90 servo motor to continuously measure forward clearance, look left and right when an obstruction is encountered (< 25 cm), compare open distances, and pivot away from obstacles autonomously.
2. Interactive 3D Assembly & Circuit Wiring Model
Rotate the 3D model 360 degrees, zoom in/out, disassemble/explode parts to inspect individual hardware layers, toggle realistic circuit wires, and click any component to inspect its engineering specifications.
3. Laboratory Video Masterclass Tutorial
Step-by-step video walkthrough covering breadboard circuit assembly, wiring verification, and testing. Enrolled students and instructors can access video streaming below.
2. Learning Outcomes
Echolocation Principles
Learn how bats and submarines calculate distance by timing 40 kHz high-frequency acoustic sound waves reflecting off physical objects.
Servo Angle Positioning
Control exact angular positions (0°, 90°, 180°) using PWM signal pulses with the Arduino Servo.h library.
Pathfinding Decision Logic
Program dynamic spatial decision-making: compare distance vectors and select the maximum clearance trajectory.
3. Technologies Learned
myservo.write(angle))4. Required Components
| Component Name | Quantity | Specification | Interface |
|---|---|---|---|
| Arduino Uno | 1 | Central microcontroller | GPIO Control |
| HC-SR04 Ultrasonic Sensor | 1 | Acoustic distance sensor (2cm - 400cm range) | Trig / Echo Pins (D11, D12) |
| SG90 Micro Servo Motor | 1 | 9g 180-degree positional actuator | PWM (Pin D3) |
| L298N Motor Driver + 2 Motors | 1 | Dual H-Bridge Driver | Pins D5, D6, D9, D10 |
| Ultrasonic Servo Mounting Bracket | 1 | Mounts HC-SR04 sensor to servo arm horn | Hardware Mount |
5. Circuit Connection Table
| Component Pin | Arduino Pin | Function |
|---|---|---|
| HC-SR04 Trig | Pin D11 | Sends 10us ultrasonic burst trigger |
| HC-SR04 Echo | Pin D12 | Receives reflected sound wave duration |
| SG90 Servo Signal (Orange) | Pin D3 | PWM servo angle control |
| L298N IN1, IN2, IN3, IN4 | Pins D5, D6, D9, D10 | Left and Right wheel motor controls |
6. Complete Arduino Source Code
/*
* Project: Autonomous Obstacle Avoiding Robot with Servo Radar
* Author: ElectronLab STEM Curriculum
*/
#include <Servo.h>
const int TRIG_PIN = 11;
const int ECHO_PIN = 12;
const int SERVO_PIN = 3;
// Motor Pins
const int IN1 = 5;
const int IN2 = 6;
const int IN3 = 9;
const int IN4 = 10;
Servo radarServo;
const int OBSTACLE_DISTANCE_LIMIT = 25; // in cm
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
radarServo.attach(SERVO_PIN);
radarServo.write(90); // Look forward
delay(1000);
}
int getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 30000);
if (duration == 0) return 300; // Timeout
return duration * 0.034 / 2;
}
void loop() {
int distanceForward = getDistance();
if (distanceForward > OBSTACLE_DISTANCE_LIMIT) {
moveForward();
} else {
stopMotors();
delay(200);
moveBackward();
delay(400);
stopMotors();
// Look Right
radarServo.write(20);
delay(500);
int distanceRight = getDistance();
// Look Left
radarServo.write(160);
delay(500);
int distanceLeft = getDistance();
// Return servo to center
radarServo.write(90);
delay(300);
// Decide best path
if (distanceLeft >= distanceRight && distanceLeft > OBSTACLE_DISTANCE_LIMIT) {
turnLeft();
delay(500);
} else if (distanceRight > distanceLeft && distanceRight > OBSTACLE_DISTANCE_LIMIT) {
turnRight();
delay(500);
} else {
turnAround();
delay(900);
}
}
}
void moveForward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void moveBackward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void turnLeft() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void turnRight() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void turnAround() {
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);
}
7. Working Principle
The ultrasonic sensor emits 40 kHz sonic pulses and measures the round-trip echo return time. Using the speed of sound formula Distance = (Duration × 0.034 cm/us) / 2, the controller continuously evaluates proximity. When an obstacle is within 25 cm, the robot stops, swings the sensor 70 degrees left and right, and dynamically maneuvers towards the highest clearance opening.
8. Troubleshooting Guide
Ultrasonic Sensor Reports Zero or Constant Maximum
Check the Trigger and Echo pin wiring. Ensure the pulseIn() timeout parameter is included to prevent infinite blocking on missed echoes.
Servo Jitters When Motors Run
Servo motors draw peak current surges during motion. Place a 100uF to 470uF electrolytic capacitor across the 5V and GND power rails to smooth out voltage ripple.