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.


