Obstacle Avoiding Robot
Introduction
Obstacle avoidance is one of the fundamental capabilities of autonomous robots, enabling them to navigate safely without human intervention. In this project, we built an Arduino-based obstacle avoiding robot that detects objects in its path and automatically changes direction to avoid collisions.
The robot uses an HC-SR04 ultrasonic sensor to continuously measure the distance to obstacles ahead. An Arduino Uno processes the sensor data and controls two DC motors through an L298N motor driver. When an obstacle is detected within a predefined distance, the robot stops, determines a new direction, and continues moving once the path is clear.
This project is ideal for beginners and students who want to learn about Arduino programming, ultrasonic sensing, motor control, and basic robotics. It provides hands-on experience in integrating sensors, actuators, and microcontrollers to create an autonomous navigation system.
By following this guide, you will learn how to assemble the robot, connect the electronic components, upload the Arduino code, and test its obstacle avoidance capability. This project also serves as a strong foundation for more advanced robotics applications, such as maze-solving robots, autonomous vehicles, and smart navigation systems.
Supplies
Components Used
- Arduino Uno
- HC-SR04 Ultrasonic Sensor
- SG90 Servo Motor (to rotate the ultrasonic sensor)
- L298N Motor Driver Module
- 2 × DC Gear Motors
- Robot Chassis
- 2 × Robot Wheels
- 1 × Caster Wheel
- 7.4V Battery Pack (or suitable battery source)
- Battery Holder
- Jumper Wires
- Breadboard (optional, for easy connections)
- USB Cable (for uploading the Arduino code)
- Mounting Hardware (screws, nuts, spacers, or double-sided tape)
All Steps of Project
Step 1: Gather the Components
Before starting, collect all the required components:
- Arduino Uno
- HC-SR04 Ultrasonic Sensor
- SG90 Servo Motor
- L298N Motor Driver
- 2 × DC Gear Motors
- Robot Chassis
- 2 × Wheels
- Caster Wheel
- Battery Pack
- Jumper Wires
- USB Cable
Step 2: Assemble the Robot Chassis
Attach the two DC gear motors to the robot chassis and secure them firmly. Fix the wheels onto the motors and install the caster wheel at the front or rear of the chassis for balance.
Step 3: Mount the Arduino and Motor Driver
Secure the Arduino Uno and L298N motor driver onto the chassis using screws, spacers, or double-sided tape. Ensure that all components are firmly fixed and accessible for wiring.
Step 4: Install the Ultrasonic Sensor
Mount the HC-SR04 ultrasonic sensor on top of the SG90 servo motor. Fix the servo motor to the front of the robot so that the ultrasonic sensor can rotate left and right to scan the surroundings.
Step 5: Connect the Electronics
Connect the Arduino, L298N motor driver, ultrasonic sensor, servo motor, and battery according to the circuit diagram. Double-check all wiring before powering the robot.
Step 6: Upload the Arduino Code
Open the Arduino IDE, select the correct board and COM port, and upload the obstacle avoidance program to the Arduino Uno.
Step 7: Power the Robot
Disconnect the USB cable after uploading the code and connect the battery pack to the robot. Switch on the power supply.
Step 8: Test the Robot
Place the robot on a flat surface with obstacles in front of it. The ultrasonic sensor continuously measures the distance ahead. When an obstacle is detected, the robot stops, scans the surroundings, chooses a clear direction, turns, and continues moving forward.
Step 9: Troubleshooting
- Verify all wiring connections if the robot does not move.
- Ensure the battery is fully charged.
- Adjust the ultrasonic sensor position if obstacles are not detected correctly.
- Confirm the correct Arduino board and COM port are selected before uploading the code.
- Check that the servo motor rotates freely without obstruction.
Step 10: Conclusion
Congratulations! You have successfully built an Arduino-based obstacle avoiding robot capable of detecting obstacles and navigating around them automatically. This project is a great introduction to embedded systems, robotics, sensor interfacing, and autonomous navigation. You can further enhance it by adding Bluetooth control, Wi-Fi connectivity, or advanced navigation algorithms.
Program for the Project
#include <Servo.h>
// Ultrasonic Sensor
#define trigPin 9
#define echoPin 10
// Servo
Servo servoMotor;
// Motor Driver
#define ENA 5
#define ENB 6
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 7
long duration;
int distance;
int leftDistance;
int rightDistance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
servoMotor.attach(11);
servoMotor.write(90);
analogWrite(ENA, 120);
analogWrite(ENB, 120);
Serial.begin(9600);
delay(1000);
}
void loop() {
distance = readDistance();
Serial.print("Distance: ");
Serial.println(distance);
if (distance > 20) {
moveForward();
}
else {
stopRobot();
delay(300);
moveBackward();
delay(400);
stopRobot();
// Look Left
servoMotor.write(150);
delay(500);
leftDistance = readDistance();
// Look Right
servoMotor.write(30);
delay(500);
rightDistance = readDistance();
// Center Servo
servoMotor.write(90);
delay(300);
if (leftDistance > rightDistance) {
turnLeft();
delay(600);
}
else {
turnRight();
delay(600);
}
stopRobot();
delay(200);
}
}
int readDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance == 0)
distance = 400;
return distance;
}
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 stopRobot() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}