Skip to content
The Savvy Engineer

The Savvy Engineer

Invest in Your Knowledge

  • Home
  • ENGINEERING
    • CAD
    • CAM
    • Business
    • Robotics
  • YOUTUBE
  • Privacy Policy
  • Contact Us
  • Toggle search form
How to Build an Arduino Obstacle Avoiding Robot for beginners

How to Build an Arduino Obstacle Avoiding Robot for beginners

Posted on September 4, 2025September 4, 2025 By TSE

Robotics is one of the most exciting fields where electronics and programming merge. One of the best beginner projects to understand the basics of robotics, sensors, and motor control is an Arduino obstacle-avoiding robot. This robot uses sensors to detect objects in its path and automatically changes its direction to avoid collisions. Such a project will introduce you to microcontrollers, sensors, motor drivers, and coding in a hands-on way.

Below, we will go over the detailed explanation, components required, wiring, and Arduino code.

1. How the Robot Works

The obstacle-avoiding robot uses ultrasonic distance sensing to determine if an obstacle is ahead. When the ultrasonic sensor detects an object within a set distance (e.g., 20 cm), the Arduino decides whether the robot should stop, turn left, or turn right. If the path is clear, the robot moves forward.

Here’s a simplified logic:

  1. Move forward if there’s no obstacle.
  2. If an obstacle is detected at a certain distance:
    • Stop.
    • Turn right or left to find a clear path.
    • Resume moving forward.

This cycle repeats continuously, allowing the robot to navigate autonomously.

2. Components Needed

Here’s the list of all components required to build this robot:

Electronics and Microcontroller

Arduino UNO (or compatible board) – The “brain” of the robot.

Ultrasonic Sensor (HC-SR04) – For distance measurement to detect obstacles.

L298N Motor Driver Module – Controls the motors by interfacing them with Arduino.

Motors and Mechanical Parts

2 × DC Geared Motors + Wheels

1 × Caster Wheel – A free-rotating wheel to balance the robot.

Chassis (Robot Body Frame) – Acrylic, plastic, or metal base to mount all components.

Power Supply

Battery Pack (7.4V Li-ion or 9V Battery Pack with Holder) – To power the motors and Arduino.

Jumper Wires For connections.

Switch (optional) – To easily power on/off the robot.

Additional Items

Breadboard (optional) – For temporary wiring.

Screws, nuts, standoffs, and double-sided tape – For assembling components.

USB Cable for Arduino to upload the code.

3. Circuit Connections

Now let’s wire everything together.

Ultrasonic Sensor HC-SR04

  • VCC → 5V on Arduino
  • GND → GND on Arduino
  • TRIG → Pin 9 on Arduino
  • ECHO → Pin 10 on Arduino

L298N Motor Driver

  • IN1 → Pin 5 on Arduino
  • IN2 → Pin 4 on Arduino
  • IN3 → Pin 3 on Arduino
  • IN4 → Pin 2 on Arduino
  • ENA → 5V (or connect to Pin 6 for PWM speed control)
  • ENB → 5V (or connect to Pin 11 for PWM speed control)
  • Motor A terminals → Left motor
  • Motor B terminals → Right motor
  • VCC (12V) → Battery positive
  • GND → Arduino GND and Battery negative

Power

  • Use a 7.4V–12V battery pack connected to the L298N VCC input.
  • Arduino can be powered either via USB or through the L298N’s onboard 5V regulator.

4. Step-by-Step Assembly

  1. Prepare the chassis: Attach the two DC motors on opposite sides. Mount the caster wheel in the front or rear for balance.
  2. Mount the Arduino Uno on the chassis using screws or standoffs.
  3. Attach the motor driver (L298N) close to the motors for easy wiring.
  4. Place the ultrasonic sensor on the front of the chassis (you can use a small bracket or glue).
  5. Connect all wiring as explained above.
  6. Insert the battery pack and secure it to the chassis.
  7. Upload the code to Arduino using the USB cable.
  8. Turn on the switch and watch your robot move and avoid obstacles!

5. The Arduino Code

Here’s the complete Arduino sketch that makes the robot work:

// Obstacle Avoiding Robot using Arduino and HC-SR04

// Motor driver pins

#define IN1 5

#define IN2 4

#define IN3 3

#define IN4 2

// Ultrasonic sensor pins

#define TRIG 9

#define ECHO 10

long duration;
int distance;

void setup() {
// Motor pins
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);

// Ultrasonic sensor pins
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);

// Serial monitor (optional)
Serial.begin(9600);
}

void loop() {
distance = getDistance();

Serial.print(“Distance: “);
Serial.println(distance);

if (distance > 20) {
// Move forward
forward();
} else {
// Stop, turn, and continue
stopRobot();
delay(300);
turnRight();
delay(500);
}
}

// Function to measure distance
int getDistance() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);

duration = pulseIn(ECHO, HIGH);
int dist = duration * 0.034 / 2;
return dist;
}

// Motor control functions
void forward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}

void backward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}

void turnRight() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}

void turnLeft() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}

void stopRobot() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

6. How the Code Works

  1. The getDistance() function sends out an ultrasonic pulse and measures how long it takes to bounce back, calculating the distance in centimeters.
  2. The robot keeps moving forward if the distance is greater than 20 cm.
  3. If an obstacle is detected closer than 20 cm:
    • The robot stops.
    • It turns right for a short duration (500 ms).
    • Then it continues moving forward again.
  4. Motor control is handled through simple HIGH/LOW logic on the motor driver pins.

7. Improvements You Can Make

Once you’ve built this basic robot, you can make it smarter and more advanced:

  • Add servo motor to rotate the ultrasonic sensor left and right, allowing the robot to scan for a better path.
  • Add infrared sensors for line following or edge detection.
  • Add Bluetooth or Wi-Fi module (HC-05 or ESP8266) to control the robot via smartphone.
  • Use PWM (pulse width modulation) to control motor speed.
  • Upgrade to rechargeable lithium-ion batteries for longer operation.

8. Learning Outcomes

By building this project, you’ll learn:

  • How ultrasonic sensors measure distance.
  • How to control DC motors with a motor driver.
  • How to structure decision-making logic in Arduino programming.
  • How to design and assemble a simple robotic system.

Thank you guys for visiting the Savvy Engineer Website!

Post Views: 100
Robotics Tags:arduino, avoiding obstacles, dc motors, engineering, robotics, wires

Post navigation

Previous Post: SolidWorks | How to Create an Extrusion Not Normal to the Sketch Plane
Next Post: 5 Reasons to Start Trading in Pocket Option as a Beginner Trader

Copyright © 2025 The Savvy Engineer.

Powered by PressBook Grid Blogs theme