How to Make an Arduino Obstacle Avoiding Robot Car (L298N + Ultrasonic Sensor)

In this tutorial, we’ll build a smart obstacle avoiding robot car using Arduino Uno, L298N motor driver, and an ultrasonic distance sensor.

This project is beginner-friendly and affordable — costing under $20, yet it introduces the fundamentals of autonomous robotics and sensor-based navigation.

The robot uses an HC-SR04 ultrasonic sensor mounted on a servo motor to detect obstacles in front and to the sides. When it detects an obstacle, it automatically stops and turns toward the clearer direction.

Required Components

ComponentQuantityDescription
Arduino Uno1Main controller board
L298N Motor Driver Module1Drives two DC motors
HC-SR04 Ultrasonic Sensor1Detects obstacles
SG90 Servo Motor1Rotates the ultrasonic sensor
DC Gear Motor2Drives the wheels
Robot Car Chassis Kit1Includes wheels, base plate, and screws
4×AA Battery Holder1Power source for motors
9V Battery + Clip1Power for Arduino
Jumper WiresSeveralFor connections
Mini Screwdriver, Bolts, NutsFor assembly

Assembly Steps

1️⃣ Prepare the Chassis

Attach the DC motors to the chassis using the brackets and screws provided in your kit.
Mount the wheels to the motor shafts and install the caster wheel at the rear for balance.

2️⃣ Mount the Sensor

Attach the HC-SR04 ultrasonic sensor to the servo motor holder, then fix the holder on the front of the chassis.
The servo allows the sensor to rotate left and right, scanning for obstacles.

3️⃣ Install the Electronics

Place the Arduino Uno and L298N motor driver on the chassis top plate using spacers or double-sided tape.
Connect the battery holders — one for Arduino (9V) and one for motors (4×AA).

Circuit Connections

How to Make an Arduino Obstacle Avoiding Robot Car Circuit

🔹 L298N Motor Driver → Motors

L298N PinConnection
OUT1, OUT2Left Motor
OUT3, OUT4Right Motor

🔹 L298N Motor Driver → Arduino

L298N PinArduino Pin
IN15
IN24
IN33
IN42
+12V4×AA Battery Pack (+)
GNDCommon GND with Arduino
5V(Do not connect if using external power)

🔹 HC-SR04 Ultrasonic Sensor → Arduino

HC-SR04 PinArduino Pin
VCC+5V
GNDGND
TRIGA1
ECHOA2

🔹 SG90 Servo Motor → Arduino

Servo PinArduino Pin
SignalD11
VCC+5V
GNDGND

🔹 Power Supply

  • Motors: 4×AA batteries connected to L298N +12V input
  • Arduino: 9V battery to the barrel jack
  • Connect all GNDs together (Arduino, L298N, sensor, and servo)

How It Works

1️⃣ The ultrasonic sensor measures the distance to nearby obstacles.
2️⃣ If an obstacle is detected within 20 cm, the robot stops.
3️⃣ The servo turns left and right, checking for free paths.
4️⃣ The robot compares left and right distances and turns toward the direction with more space.
5️⃣ If no obstacle is detected, it moves forward continuously.

Arduino Code

Before uploading, install the NewPing Library for the ultrasonic sensor:
📦 Download NewPing Library

#include <Servo.h>      // Servo control
#include <NewPing.h>    // Ultrasonic sensor library

// L298N motor driver pins
const int LeftMotorForward = 5;
const int LeftMotorBackward = 4;
const int RightMotorForward = 3;
const int RightMotorBackward = 2;

// Ultrasonic sensor pins
#define trig_pin A1
#define echo_pin A2
#define maximum_distance 200

boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance);
Servo servo_motor;

void setup() {
  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);

  servo_motor.attach(11);
  servo_motor.write(90);  // Center position
  delay(2000);

  distance = readPing();
  delay(100);
}

void loop() {
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20) {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distanceRight >= distanceLeft) {
      turnRight();
      moveStop();
    } else {
      turnLeft();
      moveStop();
    }
  } else {
    moveForward();
  }
  distance = readPing();
}

int lookRight() {
  servo_motor.write(10);
  delay(500);
  int distance = readPing();
  servo_motor.write(90);
  delay(100);
  return distance;
}

int lookLeft() {
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  servo_motor.write(90);
  delay(100);
  return distance;
}

int readPing() {
  delay(70);
  int cm = sonar.ping_cm();
  if (cm == 0) {
    cm = 250; // Default if no reading
  }
  return cm;
}

void moveStop() {
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

void moveForward() {
  if (!goesForward) {
    goesForward = true;
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
  }
}

void moveBackward() {
  goesForward = false;
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
}

void turnRight() {
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
  delay(500);
}

void turnLeft() {
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  delay(500);
}

Code Explanation (Step-by-Step)

1️⃣ Libraries:

  • Servo.h controls the SG90 servo movement.
  • NewPing.h handles precise ultrasonic distance measurement.

2️⃣ Motor Logic:

  • Forward = Left and Right motors rotate forward.
  • Backward = Both motors rotate backward.
  • Left Turn = Left motor stops or reverses, right motor forward.
  • Right Turn = Right motor stops or reverses, left motor forward.

3️⃣ Decision Process:

  • If distance < 20 cm, stop and check both sides.
  • Compare right and left sensor readings.
  • Move toward the direction with more space.

Power Tips

  • Servo jittering? Use a dedicated 5V supply for servo and connect grounds.
  • Motors weak? Use NiMH rechargeable batteries instead of carbon-zinc.
  • Random resets? Add a 100 µF capacitor near the L298N power input.

How to Test

1️⃣ Upload the code to your Arduino Uno.
2️⃣ Power the car with both battery packs.
3️⃣ Place the robot in an open space.
4️⃣ Observe how it moves forward and avoids obstacles by turning autonomously.

Conclusion

You’ve successfully built an Arduino Obstacle Avoiding Robot Car powered by L298N and HC-SR04!
This simple yet powerful project teaches the basics of:

  • Autonomous robotics
  • Ultrasonic distance sensing
  • Servo-based scanning
  • Motor driver control

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir