An obstacle-avoiding robot is one of the best beginner-friendly projects in robotics. It uses ultrasonic sensors to detect objects and automatically changes direction to avoid collisions. This type of robot is widely used in autonomous vehicles, warehouse robots, and smart home assistants.
- 1️⃣ How Does an Obstacle-Avoiding Robot Work? 🤔
- 2️⃣ Required Components 🛠️
- 3️⃣ Circuit Connections 🔧
- 🔹 Connecting the Ultrasonic Sensor (HC-SR04) to Arduino
- 🔹 Connecting the Motor Driver (L298N) to Motors
- 4️⃣ Writing the Arduino Code 💻
- 5️⃣ Testing Your Obstacle-Avoiding Robot 🏎️
- 6️⃣ How to Improve Your Robot 🚀
- Final Thoughts 💡
In this guide, you’ll learn:
✅ How an obstacle-avoiding robot works
✅ Required components
✅ Circuit connections
✅ Writing the Arduino code
✅ Testing & improving your robot
Let’s get started! 🚀
1️⃣ How Does an Obstacle-Avoiding Robot Work? 🤔
An ultrasonic sensor (HC-SR04) measures the distance to obstacles using sound waves:
1️⃣ The sensor emits sound pulses (inaudible to humans).
2️⃣ If an object is detected, the sound waves bounce back.
3️⃣ The sensor calculates the distance based on the time taken for the pulse to return.
4️⃣ If the distance is too small, the robot stops and changes direction.
📌 Example Behavior:
- If the path is clear: Move forward 🚗
- If an obstacle is detected (distance < 10 cm): Stop and turn ⏪
2️⃣ Required Components 🛠️
To build an Arduino-based obstacle-avoiding robot, you’ll need:
🔹 Microcontroller – Arduino Uno (best for beginners)
🔹 Motor Driver (L298N) – Controls DC motors
🔹 DC Motors (2x) – Moves the robot forward/backward
🔹 Ultrasonic Sensor (HC-SR04) – Detects obstacles
🔹 Chassis – The body of the robot
🔹 Wheels (2x) & Caster Wheel (1x) – For movement
🔹 Battery Pack (9V or Li-ion) – Power source
🔹 Jumper Wires – To make connections
💡 Pro Tip: You can add a servo motor to rotate the sensor for better obstacle detection!
3️⃣ Circuit Connections 🔧
🔹 Connecting the Ultrasonic Sensor (HC-SR04) to Arduino
HC-SR04 PinArduino PinVCC5VGNDGNDTrig6Echo7
📌 Trig sends sound pulses, and Echo receives them.
🔹 Connecting the Motor Driver (L298N) to Motors
L298N PinArduino PinIN18IN29IN310IN411ENA (PWM)5ENB (PWM)6
📌 PWM pins (5,6) control speed; IN1-IN4 control direction.
4️⃣ Writing the Arduino Code 💻
🔹 Basic Obstacle-Avoidance Code
cpp
-----
#define trigPin 6
#define echoPin 7
#define leftMotor1 8
#define leftMotor2 9
#define rightMotor1 10
#define rightMotor2 11
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(leftMotor1, OUTPUT);
pinMode(leftMotor2, OUTPUT);
pinMode(rightMotor1, OUTPUT);
pinMode(rightMotor2, OUTPUT);
Serial.begin(9600);
}
long getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = duration * 0.034 / 2;
return distance;
}
void loop() {
long distance = getDistance();
Serial.print("Distance: ");
Serial.println(distance);
if (distance > 10) { // If path is clear
moveForward();
} else { // If obstacle detected
stopRobot();
delay(500);
turnRight();
delay(1000);
}
}
void moveForward() {
digitalWrite(leftMotor1, HIGH);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, HIGH);
digitalWrite(rightMotor2, LOW);
}
void turnRight() {
digitalWrite(leftMotor1, HIGH);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, HIGH);
}
void stopRobot() {
digitalWrite(leftMotor1, LOW);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, LOW);
}
📌 How it works:
✔️ The ultrasonic sensor detects objects in front of the robot.
✔️ If the distance is safe (>10 cm), the robot moves forward.
✔️ If an obstacle is too close (<10 cm), the robot stops and turns right.
5️⃣ Testing Your Obstacle-Avoiding Robot 🏎️
🔹 Step 1: Upload Code
- Connect Arduino to your computer via USB.
- Open Arduino IDE, select the correct board & port, and upload the code.
🔹 Step 2: Power On the Robot
- Disconnect USB and power the robot with batteries.
🔹 Step 3: Test Movement
✅ Moves forward when no obstacles are detected.
✅ Stops and turns when an object is near.
✅ Resumes movement after turning away.
📌 If it doesn’t work correctly:
- Adjust the sensor height and motor connections.
- Use
Serial Monitorto debug distance values.
6️⃣ How to Improve Your Robot 🚀
🔹 Use a Servo Motor for Better Detection – Rotate the ultrasonic sensor left/right for a wider view.
🔹 Adjust Speed Using PWM – Modify analogWrite() for smooth movement.
🔹 Add More Sensors – Install side-facing IR sensors for better navigation.
🔹 Use AI for Smart Navigation – Implement Raspberry Pi & OpenCV for object recognition.
🔹 Control It via Bluetooth – Add an HC-05 module to control it with a smartphone app.
💡 Advanced Upgrade: Convert it into a self-driving car using AI & machine learning! 🤖
Final Thoughts 💡
Building an obstacle-avoiding robot is an amazing way to learn robotics! With Arduino, sensors, and motors, you develop real-world automation skills used in AI, autonomous vehicles, and industrial robotics.


