Obstacle Avoiding Robotic Car Using Arduino – Circuit Diagram, Components, Code & Working

Introduction

An Obstacle Avoiding Robotic Car is an autonomous robot that can detect obstacles in its path and automatically change direction to avoid collisions.

This project is useful for students and beginners who want to learn about Arduino, robotics, ultrasonic sensors, motor drivers and autonomous navigation.

The robot continuously measures the distance between itself and nearby objects. When an obstacle is detected within a predefined distance, the Arduino stops the motors, checks the available directions and moves toward a safer path.

Project Objective

The main objectives of this project are:

  • Detect obstacles automatically.
  • Measure the distance using an ultrasonic sensor.
  • Control DC motors using a motor driver.
  • Automatically change direction when an obstacle is detected.
  • Build a simple autonomous robotic vehicle.
  • Learn the basics of robotics and embedded programming.

How It Works

        ┌─────────────────────┐
        │   Ultrasonic Sensor                        │
        │      HC-SR04                                   │
        └──────────┬──────────┘
                                     │ Distance
                                     ▼
        ┌─────────────────────┐
        │       Arduino                                   │
        │     UNO/Nano                                │
        └──────────┬──────────┘
                                     │ Control Signals
                                     ▼
        ┌─────────────────────┐
        │    Motor Driver                              │
        │       L298N                                     │
        └──────────┬──────────┘
                                     │
              ┌────────┴────────┐
              ▼                                           ▼
    ┌───────────┐     ┌───────────┐
    │ Left Motor           │     │     Right Motor     │
    └───────────┘     └───────────┘

Hardware Requirements

Component Quantity
Arduino UNO 1
HC-SR04 Ultrasonic Sensor 1
L298N Motor Driver Module 1
DC Gear Motors 2
Robot Car Chassis 1
Robot Wheels 2
Caster Wheel 1
Battery Pack 1
Jumper Wires As required
Switch 1


Circuit Diagram

             HC-SR04
          ┌─────────────┐
          │ VCC     GND              │
          │ TRIG   ECHO             │
          └──┬─────┬────┘
                  │             │
                 ▼             ▼
        ┌────────────────┐
        │    ARDUINO                      │
        │      UNO                            │
        └───────┬────────┘
                             │
       ┌────────┴────────┐
       ▼                                           ▼
 ┌───────────┐     ┌───────────┐
 │   L298N                │     │       Battery          │
 │   Driver                 │     │       Pack              │
 └─────┬─────┘     └───────────┘
                 │
      ┌───┴────┐
     ▼                    ▼
Motor L        Motor R

Arduino Code

#define TRIG_PIN 9
#define ECHO_PIN 10

#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5

#define ENA 6
#define ENB 7

long duration;
int distance;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);

  analogWrite(ENA, 180);
  analogWrite(ENB, 180);

  Serial.begin(9600);
}

void loop() {

  distance = getDistance();

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if (distance > 20) {
    moveForward();
  }
  else {
    stopCar();
    delay(300);

    moveBackward();
    delay(400);

    stopCar();
    delay(200);

    turnRight();
    delay(500);

    stopCar();
  }
}

int getDistance() {

  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);

  digitalWrite(TRIG_PIN, LOW);

  duration = pulseIn(ECHO_PIN, HIGH);

  int distance = duration * 0.034 / 2;

  return distance;
}

void moveForward() {

  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);

  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void moveBackward() {

  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 stopCar() {

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);

  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

Advantages

  • Simple to build
  • Low-cost components
  • Easy Arduino programming
  • Autonomous operation
  • Good beginner robotics project
  • Can be upgraded with additional sensors

Conclusion

The Obstacle Avoiding Robotic Car is an excellent beginner-level robotics project that demonstrates how sensors, microcontrollers and motor drivers can work together to create an autonomous vehicle.

By using an Arduino, HC-SR04 ultrasonic sensor and L298N motor driver, the robot can detect obstacles and automatically change its movement to avoid collisions.

This project provides a practical introduction to robotics, embedded systems, sensors, motor control and Arduino programming.