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.