Introduction
An obstacle avoiding robot is an autonomous robotic vehicle that can detect objects in its path and change direction without human control.
In this project, we will build a simple Arduino-based obstacle avoiding robot using an ultrasonic sensor, motor driver and two DC geared motors.
The ultrasonic sensor continuously measures the distance between the robot and objects in front of it. When an obstacle is detected within a predefined distance, Arduino stops the motors and changes the robot's direction.
Block Diagram
┌──────────────────┐
│ HC-SR04 │
│ Ultrasonic Sensor│
└────────┬─────────┘
│
Distance
│
▼
┌──────────────────┐
│ Arduino UNO │
│ Microcontroller │
└────────┬─────────┘
│
Control Signals
│
▼
┌──────────────────┐
│ Motor Driver │
└───────┬───┬──────┘
│ │
Motor Motor
│ │
▼ ▼
Left Right
Wheel Wheel
Hardware Requirements
| Component | Quantity | Purpose |
|---|---|---|
| Arduino UNO | 1 | Main controller |
| HC-SR04 Ultrasonic Sensor | 1 | Obstacle detection |
| L298N Motor Driver | 1 | Motor control |
| DC Geared Motors | 2 | Robot movement |
| Robot Wheels | 2 | Movement |
| Robot Chassis | 1 | Mechanical structure |
| Caster Wheel | 1 | Balance |
| Battery Pack | 1 | Power |
| Jumper Wires | As required | Connections |
| Breadboard | 1 | Optional prototyping |
Circuit Diagram
ARDUINO UNO
┌──────────────────┐
│ │
D9 ───┤ │
D10 ───┤ │
│ │
D2 ───┤ │
D3 ───┤ │
D4 ───┤ │
D5 ───┤ │
│ │
5V ───┤ │
GND ───┤ │
└────────┬─────────┘
│
┌───────────────┴──────────────┐
│ │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ HC-SR04 │ │ L298N │
│ │ │ Motor Driver│
│ TRIG ← D9 │ │ IN1 ← D2 │
│ ECHO → D10 │ │ IN2 ← D3 │
│ VCC → 5V │ │ IN3 ← D4 │
│ GND → GND │ │ IN4 ← D5 │
└─────────────┘ └──────┬──────┘
│
┌────────┴────────┐
▼ ▼
Left Motor Right Motor
Arduino Code
const int trigPin = 9;
const int echoPin = 10;
const int motor1A = 2;
const int motor1B = 3;
const int motor2A = 4;
const int motor2B = 5;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motor1A, OUTPUT);
pinMode(motor1B, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2B, OUTPUT);
Serial.begin(9600);
}
void loop() {
distance = getDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance > 20) {
moveForward();
} else {
stopRobot();
delay(300);
moveBackward();
delay(400);
stopRobot();
delay(200);
turnRight();
delay(500);
stopRobot();
}
delay(100);
}
int getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH, 30000);
if (duration == 0) {
return 999;
}
return duration * 0.034 / 2;
}
void moveForward() {
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, HIGH);
digitalWrite(motor2B, LOW);
}
void moveBackward() {
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}
void stopRobot() {
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
}
void turnRight() {
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, HIGH);
}
Applications
Obstacle avoiding robots are commonly used as educational platforms for:
- Robotics learning
- Autonomous vehicle prototypes
- Arduino projects
- Embedded systems education
- Robotics competitions
- Navigation experiments
- Sensor-based automation
Conclusion
The Arduino obstacle avoiding robot is a practical project for learning how sensors, microcontrollers and motors work together.
The HC-SR04 measures the distance to obstacles, Arduino processes the sensor data, and the L298N motor driver controls the robot's motors.
Once the basic robot works, you can expand it with Bluetooth control, servo scanning, additional sensors, motor speed control and more advanced navigation algorithms.