Create Your Own Obstacle-Avoiding Robot with Arduino ๐Ÿค–๐Ÿšง

Prabhu TL
6 Min Read
Disclosure: This website may contain affiliate links, which means I may earn a commission if you click on the link and make a purchase. I only recommend products or services that I personally use and believe will add value to my readers. Your support is appreciated!

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.

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 Monitor to 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.

Share This Article
Prabhu TL is a SenseCentral contributor covering digital products, entrepreneurship, and scalable online business systems. He focuses on turning ideas into repeatable processesโ€”validation, positioning, marketing, and execution. His writing is known for simple frameworks, clear checklists, and real-world examples. When heโ€™s not writing, heโ€™s usually building new digital assets and experimenting with growth channels.