Arduino-Based Obstacle Avoiding Robot Using HC-SR04 Ultrasonic Sensor, Servo Motor, and L298N Motor Driver

by rishiljafer12 in Circuits > Arduino

24 Views, 0 Favorites, 0 Comments

Arduino-Based Obstacle Avoiding Robot Using HC-SR04 Ultrasonic Sensor, Servo Motor, and L298N Motor Driver

Screenshot_27-6-2026_15269_web.whatsapp.com.jpeg

Introduction

In this project, I built an autonomous obstacle-avoiding robot using an Arduino Uno, an HC-SR04 ultrasonic sensor, an SG90 servo motor, and an L298N motor driver. The robot is designed to move independently while detecting and avoiding obstacles in its path.

The ultrasonic sensor is mounted on a servo motor, allowing it to scan the left and right sides whenever an obstacle is detected. The Arduino processes the distance measurements and controls the motors to choose the direction with more free space. This enables the robot to navigate without any manual control.

This project is an excellent introduction to robotics, embedded systems, sensor interfacing, and motor control. It is suitable for students, beginners, and hobbyists who want to learn how autonomous robots work.

Features

  1. Autonomous navigation
  2. Obstacle detection using ultrasonic sensor
  3. Servo-based scanning mechanism
  4. Automatic left/right decision making
  5. Simple Arduino programming
  6. Low-cost components
  7. Beginner-friendly design


Supplies

Supplies

  1. Arduino Uno
  2. L298N Motor Driver Module
  3. HC-SR04 Ultrasonic Sensor
  4. SG90 Servo Motor
  5. 2 × TT DC Gear Motors
  6. Robot Chassis
  7. 2 Robot Wheels
  8. 1 Caster Wheel
  9. 7.4V Battery Pack (2×18650) or 6×AA Battery Pack
  10. Battery Holder
  11. Jumper Wires
  12. Breadboard (optional)
  13. Power Switch (optional)
  14. USB Cable
  15. Arduino IDE (Software)


Understanding the Working Principle

The robot continuously measures the distance in front using the HC-SR04 ultrasonic sensor.

  1. The robot starts by moving forward.
  2. The ultrasonic sensor continuously checks for obstacles.
  3. If no obstacle is detected within the preset distance, the robot continues moving forward.
  4. When an obstacle is detected, the robot stops and moves backward slightly.
  5. The servo rotates the ultrasonic sensor toward the left and measures the distance.
  6. The servo then rotates toward the right and measures the distance.
  7. The Arduino compares both measurements.
  8. The robot turns toward the side with more available space.
  9. After turning, it resumes moving forward.

This cycle repeats continuously, enabling the robot to navigate around obstacles automatically.

Circuit Connections

Connect all the components according to the following wiring:

Arduino to L298N Motor Driver

  1. ENA → D5
  2. IN1 → D2
  3. IN2 → D3
  4. ENB → D6
  5. IN3 → D4
  6. IN4 → D7

Arduino to HC-SR04 Ultrasonic Sensor

  1. VCC → 5V
  2. GND → GND
  3. TRIG → D9
  4. ECHO → D10

Arduino to Servo Motor

  1. Signal → D11
  2. VCC → 5V
  3. GND → GND

Power Connections

  1. Battery (+) → L298N 12V
  2. Battery (−) → L298N GND
  3. Arduino GND → L298N GND

Before powering the robot, verify all wiring and ensure that every component shares a common ground.

Assemble the Robot

Mount the two TT gear motors onto the robot chassis and attach the wheels. Install the caster wheel to provide balance and smooth movement.

Fix the ultrasonic sensor onto the servo motor so it can rotate freely for scanning. Secure the Arduino Uno, L298N motor driver, and battery pack to the chassis using screws, cable ties, or double-sided tape. Arrange the wires neatly to prevent them from interfering with the robot's movement.

Upload the Arduino Code and Test the Robot

Open the Arduino IDE and connect the Arduino Uno to your computer using a USB cable.

  1. Select Arduino Uno from the Tools → Board menu.
  2. Choose the correct COM port.
  3. Upload the obstacle-avoiding robot code.
  4. Disconnect the USB cable if required.
  5. Power the robot using the battery pack.
  6. Place an obstacle in front of the robot.

The robot should move forward, detect the obstacle, stop, scan both sides using the servo-mounted ultrasonic sensor, choose the direction with more free space, and continue moving.

Conclusion

This project demonstrates how an Arduino Uno, an HC-SR04 ultrasonic sensor, a servo motor, and an L298N motor driver can work together to create an autonomous obstacle-avoiding robot. It is an excellent project for beginners to understand robotics, embedded systems, and sensor interfacing.

Future Improvements

  1. Add IR sensors for line-following.
  2. Control the robot using Bluetooth or Wi-Fi.
  3. Add voice control.
  4. Integrate a camera for AI-based object detection.
  5. Improve navigation using advanced path-planning algorithms.

Thank you for reading! I hope this guide helps you build your own obstacle-avoiding robot. Feel free to customize and improve the design based on your own ideas.