Skip to content

deep-chatterjee/Obstacle-Avoidance-Robot-using-Arduino

Repository files navigation

🤖 Obstacle Avoidance Robot

An Arduino-based autonomous robot that uses an HC-SR04 ultrasonic sensor mounted on a servo to scan its surroundings and navigate around obstacles in real time using an L293D motor driver.

Arduino C++ Servo Sensor Motor Driver


📖 Overview

The Obstacle Avoidance Robot is a fully autonomous wheeled robot built on an Arduino UNO. An HC-SR04 ultrasonic sensor continuously measures the distance to objects ahead. When an obstacle is detected within 20 cm, the robot stops, reverses slightly, then uses a servo to scan left and right — choosing the direction with more clearance to navigate around the obstacle. When the path is clear, it drives forward again.


✨ Features

  • 📡 Ultrasonic sensing — HC-SR04 measures front distance in real time using time-of-flight
  • 🔄 Servo-mounted scanning — sensor sweeps left (160°) and right (20°) to compare clearance on both sides
  • 🧠 Autonomous decision-making — robot selects the better direction based on distance comparison
  • 🚗 Full motor control — forward, backward, turn left, turn right, and stop via L293D motor driver
  • 🖥️ Serial monitoring — front, left, and right distances streamed via Serial Monitor (9600 baud)
  • 🔋 External battery powered — 9V battery drives the motors independently of the Arduino

🛠️ Hardware Components

Component Quantity
Arduino UNO 1
HC-SR04 Ultrasonic Sensor 1
SG90 Servo Motor 1
L293D Motor Driver IC 1
DC Gear Motors 2
9V Battery 1
Breadboard 1
Jumper Wires As needed

🔌 Pin Connections

Component Pin Arduino Pin
HC-SR04 VCC 5V
HC-SR04 GND GND
HC-SR04 TRIG Digital 9
HC-SR04 ECHO Digital 10
Servo Motor Signal Digital 3
Servo Motor VCC 5V
Servo Motor GND GND
L293D IN1 Digital 6
L293D IN2 Digital 7
L293D IN3 Digital 4
L293D IN4 Digital 5
L293D VCC1 (Logic) 5V
L293D VCC2 (Motor) 9V Battery (+)
L293D GND GND

🔌 Circuit Diagram

Built and simulated in Tinkercad

Breadboard View:

Breadboard Diagram

Schematic View:

Schematic Diagram


🚀 Getting Started

Prerequisites

  • Arduino IDE (1.8.x or 2.x)
  • Servo.h library — included with the Arduino IDE by default

Installation

  1. Clone the repository
git clone https://github.com/deep-chatterjee/Obstacle-Avoidance-Robot.git
cd Obstacle-Avoidance-Robot
  1. Open the sketch

    • Open Obstacle_Avoidance_Robot.ino in the Arduino IDE
    • Select your board: Arduino UNO
    • Select the correct COM port
  2. Upload to your board

    • Click Upload
    • Open Serial Monitor (baud rate: 9600) to observe live distance readings

💻 How It Works

HC-SR04 measures front distance
            ↓
     frontDistance > 20 cm?
        ↙              ↘
      YES               NO
       ↓                 ↓
  Move Forward        Stop Robot
                          ↓
                    Move Backward (400ms)
                          ↓
                    Servo scans LEFT → measure leftDistance
                          ↓
                    Servo scans RIGHT → measure rightDistance
                          ↓
               leftDistance > rightDistance?
                    ↙              ↘
                  YES               NO
                   ↓                 ↓
               Turn Left         Turn Right
                    ↓
               Resume Forward

📁 Project Structure

Obstacle-Avoidance-Robot/
├── Obstacle_Avoidance_Robot.ino           # Arduino sketch
├── Obstacle_Avoidance_Robot_Circuit.png   # Tinkercad breadboard view
├── Obstacle_Avoidance_Robot_Schematic.png # Tinkercad schematic view
└── README.md

🧠 What I Learned

  • Using pulseIn() with the HC-SR04 for accurate distance measurement
  • Controlling a servo motor to physically sweep a sensor for environmental scanning
  • Driving dual DC motors in both directions using an L293D H-bridge motor driver
  • Implementing autonomous decision-making logic based on real-time sensor comparisons
  • Managing multiple delay()-based timing sequences for coordinated robot movement

🔮 Future Improvements

  • Replace delay()-based timing with a non-blocking approach using millis() for smoother movement
  • Add PWM speed control via the L293D enable pins for variable motor speed
  • Implement a continuous 180° servo sweep instead of fixed left/right snapshots
  • Add IR sensors on the sides for faster edge and wall detection
  • Enable Bluetooth control (HC-05) for a manual override mode
  • Mount on a proper robot chassis for real-world testing beyond simulation

👤 Author

Deep Chatterjee
GitHub


📄 License

This project is licensed under the MIT License — see LICENSE for details.

About

Autonomous robot that detects and navigates around obstacles in real time using ultrasonic sensors and motor driver control.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages