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.


