Obstacle Avoiding Robot Using Arduino, L298N, Ultrasonic Sensor, and Servo Motor

by faaathimaa053 in Circuits > Arduino

18 Views, 0 Favorites, 0 Comments

Obstacle Avoiding Robot Using Arduino, L298N, Ultrasonic Sensor, and Servo Motor

6c481158-fe8f-4cd0-bcd1-3a5ccd7261fe.jpeg

In this project, I built an autonomous obstacle-avoiding robot using an Arduino Uno, an HC-SR04 ultrasonic sensor, a servo motor, an L298N motor driver, and two DC motors. The robot can move forward on its own, detect obstacles in its path, scan the surroundings using a rotating ultrasonic sensor, and choose the clearest direction to continue moving.

Supplies

SUPPLIES

Arduino Uno

L298N Motor Driver

HC-SR04 Ultrasonic Sensor

SG90 Servo Motor

2 DC Geared Motors

Robot Chassis

Wheels

Castor Wheel

12V Battery

Jumper Wires

Gather the Components


Before starting, collect all the required components:

  1. Arduino Uno
  2. L298N Motor Driver
  3. HC-SR04 Ultrasonic Sensor
  4. SG90 Servo Motor
  5. 2 DC Geared Motors
  6. Robot Chassis
  7. Wheels
  8. Castor Wheel
  9. 12V Battery
  10. Jumper Wires

Assemble the Chassis

Attach the DC motors to the robot chassis.

Fix the wheels to the motor shafts and install the castor wheel at the front or rear of the chassis to provide balance.

Mount the Arduino and L298N

Place the Arduino Uno and L298N motor driver on the chassis.

Use screws, zip ties, or double-sided tape to secure them.

Leave enough space for wiring and battery placement.

ULTRASONIC SENSOR

Fix the HC-SR04 ultrasonic sensor to the servo horn.

This allows the sensor to rotate and scan the environment in multiple directions.

Make sure the sensor is facing forward when the servo is at 90°.

Connect the Motors to the L298N

Connect:

  1. Left Motor → OUT1 and OUT2
  2. Right Motor → OUT3 and OUT4


Wire the L298N to the Arduino

Connect:

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

These connections allow the Arduino to control the speed and direction of both motors

Connect the Ultrasonic Sensor AND Servo Motor

Wire the HC-SR04 as follows:

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

This sensor is responsible for detecting obstacles.

Wire the servo:

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

The servo will rotate the ultrasonic sensor to scan left and right.



Connect the Power Supply

Wire the HC-SR04 as follows:

  1. VCC → 5V
  2. GND → GND
  3. TRIG → D9
  4. ECHO → D10
  5. This sensor is responsible for detecting obstacles.

Ensure all grounds are connected together:

  1. Arduino GND
  2. L298N GND
  3. Servo GND
  4. Ultrasonic Sensor GND

A common ground is essential for proper operation.

Upload Code to Aurduino

Open the Arduino IDE.

Select the correct board and COM port.

Upload the obstacle-avoidance code to the Arduino Uno.

After uploading, disconnect the USB cable and power the robot using the battery.

Test the Robot

Place the robot on a flat surface.

Observe the following sequence:

  1. Robot moves forward.
  2. Obstacle is detected.
  3. Servo scans right.
  4. Servo scans left.
  5. Robot compares both distances.
  6. Robot turns toward the clearer path.
  7. Robot continues moving.

Adjust motor speed and obstacle distance thresholds if required.