Skip to Content

OBSTACLE DETECTION ROBOT

The Obstacle Detection Robot is an exciting entry-level project for anyone diving into embedded systems and robotics. This autonomous two-wheel-drive vehicle uses an HC-SR04 ultrasonic sensor to detect objects in its path, while an Arduino Uno/Nano processes data and controls movement via an L298N motor driver. Designed for applications like self-driving prototypes, automated navigation, and educational robotics, this project not only brings theory to life but also equips beginners with foundational skills. By building this robot, you’ll learn to integrate sensors with microcontrollers, program Arduino for decision-making, and control motors to achieve obstacle avoidance—all while gaining hands-on experience in assembling circuits, troubleshooting hardware, and refining autonomous behaviors.

Components Required

  • Arduino Uno
  • 2WD Motor Chassis and accessories
  • L298N Motor Driver Module
  • Ultrasonic Sensor
  • Power Supply
  • Breadboard 
  • Jumper Wires

Circuit Diagram

1. Connect the Ultrasonic Sensor to Arduino

  • VCC → 5V on Arduino
  • GND → GND on Arduino
  • Trig (Trigger Pin) → D9 on Arduino
  • Echo (Echo Pin) → D10 on Arduino

2. Connect the L298N Motor Driver to Arduino

  • IN1 → D6 on Arduino
  • IN2 → D7 on Arduino
  • IN3 → D8 on Arduino
  • IN4 → D9 on Arduino
  • OUT1, OUT2 → Left Motor Terminals
  • OUT3, OUT4 → Right Motor Terminals
  • 12V Power (from battery) → L298N 12V Input
  • GND of Battery → L298N GND and Arduino GND

3. Power the System

  • Use a 9V or 12V battery and connect:
    • Battery + → L298N 12V input
    • Battery - → L298N GND and Arduino GND

CODE

#define TRIG 9

#define ECHO 10

#define IN1 6

#define IN2 7

#define IN3 8

#define IN4 11


void setup() {

  pinMode(TRIG, OUTPUT);

  pinMode(ECHO, INPUT);

  pinMode(IN1, OUTPUT);

  pinMode(IN2, OUTPUT);

  pinMode(IN3, OUTPUT);

  pinMode(IN4, OUTPUT);

  Serial.begin(9600);

}


void loop() {

  digitalWrite(TRIG, LOW);

  delayMicroseconds(2);

  digitalWrite(TRIG, HIGH);

  delayMicroseconds(10);

  digitalWrite(TRIG, LOW);


  long duration = pulseIn(ECHO, HIGH);

  float distance = duration * 0.034 / 2; // Convert to cm


  Serial.print("Distance: ");

  Serial.println(distance);


  if (distance > 10 || distance == 0) { // No obstacle detected

    digitalWrite(IN1, HIGH);

    digitalWrite(IN2, LOW);

    digitalWrite(IN3, HIGH);

    digitalWrite(IN4, LOW);

  } else { // Obstacle detected

    digitalWrite(IN1, LOW);

    digitalWrite(IN2, HIGH);

    digitalWrite(IN3, LOW);

    digitalWrite(IN4, HIGH);

    delay(500); // Move backward

    digitalWrite(IN1, HIGH);

    digitalWrite(IN2, LOW);

    digitalWrite(IN3, LOW);

    digitalWrite(IN4, HIGH);

    delay(500); // Turn right

  }

}


CODE EXPLANATION

1. Pin Definitions

#define TRIG 9 
#define ECHO 10
#define IN1 6
#define IN2 7
#define IN3 8
#define IN4 11
  • TRIG and ECHO: These are the pins connected to the ultrasonic sensor’s Trigger and Echo pins, respectively.
  • IN1, IN2, IN3, and IN4: These are the pins connected to the L298N motor driver, which controls the direction of the two DC motors.

2. setup() Function

void setup() {

  pinMode(TRIG, OUTPUT);

  pinMode(ECHO, INPUT);

  pinMode(IN1, OUTPUT);

  pinMode(IN2, OUTPUT);

  pinMode(IN3, OUTPUT);

  pinMode(IN4, OUTPUT);

  Serial.begin(9600);

}

  • pinMode(TRIG, OUTPUT): Sets the TRIG pin as an output to send the ultrasonic pulse.
  • pinMode(ECHO, INPUT): Sets the ECHO pin as an input to receive the reflected pulse.
  • pinMode(IN1, IN2, IN3, IN4, OUTPUT): Sets the motor control pins as outputs to drive the motors.
  • Serial.begin(9600): Initializes serial communication at 9600 baud rate for debugging (e.g., printing distance values to the Serial Monitor).

3. loop() Function

The loop() function runs continuously and performs the following tasks:

Step 1: Trigger the Ultrasonic Sensor

digitalWrite(TRIG, LOW); 
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);  
  • The TRIG pin is set to LOW for 2 microseconds, then HIGH for 10 microseconds, and finally back to LOW. This sends a short ultrasonic pulse.

Step 2: Measure the Echo Pulse Duration

long duration = pulseIn(ECHO, HIGH);
    • The pulseIn() function measures the time it takes for the ultrasonic pulse to bounce back to the sensor. It returns the duration in microseconds.

Step 3: Calculate the Distance

float distance = duration * 0.034 / 2; // Convert to cm 
    • The distance is calculated using the formula:

​Distance (cm) = (Duration × Speed of Sound) / 2

      • The speed of sound is approximately 0.034 cm/µs.
      • The result is divided by 2 because the sound travels to the object and back.

Step 4: Print the Distance to Serial Monitor

Serial.print("Distance: "); 
Serial.println(distance);
    • This prints the calculated distance to the Serial Monitor for debugging and monitoring.

Step 5: Decision-Making for Obstacle Avoidance

if (distance > 10 || distance == 0) { // No obstacle detected 
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
} else { // Obstacle detected
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  delay(500); // Move backward
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  delay(500); // Turn right
}
    • If no obstacle is detected (distance > 10 cm or distance == 0):
      • The robot moves forward by setting:
        • IN1 = HIGH, IN2 = LOW (left motor forward)
        • IN3 = HIGH, IN4 = LOW (right motor forward)
    • If an obstacle is detected (distance ≤ 10 cm):
      • The robot stops, moves backward for 500 ms, and then turns right for 500 ms:
        • Move Backward:
          • IN1 = LOW, IN2 = HIGH (left motor backward)
          • IN3 = LOW, IN4 = HIGH (right motor backward)
        • Turn Right:
          • IN1 = HIGH, IN2 = LOW (left motor forward)
          • IN3 = LOW, IN4 = HIGH (right motor backward)

How It Works

The robot operates in a simple loop:

  1. The HC-SR04 ultrasonic sensor continuously measures the distance to objects in front of the robot.
  2. If an obstacle is detected within a set threshold (e.g., 10 cm), the Arduino stops the motors and decides the next move.
  3. The robot can either stopmove backward, or turn left/right to avoid the obstacle.
  4. If no obstacles are detected, the robot continues moving forward.

OBSERVING FUNCTIONALITY

  1. Turn on the robot and place obstacles in its path.
  2. If no obstacles are detected, the robot moves forward continuously.
  3. When an obstacle is detected within 10 cm, the robot stops, moves backward, and turns right to avoid a collision.
  4. The process repeats as the robot continuously navigates around obstacles.

COMMON PROBLEMS AND SOLUTIONS

  1. Robot Not Moving?
  • Check if the battery is fully charged and properly connected.
  • Verify that the L298N motor driver connections are correct.

   2. Sensor Not Detecting Obstacles?

  • Ensure the ultrasonic sensor is properly wired and facing forward.
  • Increase the delay between distance readings to reduce interference.

    3. Robot Moving Erratically?

  • Adjust the distance threshold (currently 10 cm) in the code.
  • Ensure the motors are wired correctly and spinning in the right direction.

Conclusion

Building an Obstacle Detection Robot is an exciting and educational introduction to robotics and embedded systems, combining hardware assembly, sensor integration, and programming into one hands-on project. By connecting components like the Arduino, ultrasonic sensor, and motor driver, you’ve learned how to process sensor data, make decisions, and control motors to create an autonomous robot that avoids obstacles. This project not only teaches foundational skills like circuit assembly and coding but also opens the door to advanced applications such as self-driving cars, smart home devices, and industrial automation. As you continue your journey, consider expanding your robot with additional sensors, improved navigation algorithms, or wireless control, and remember that every expert was once a beginner—so keep experimenting, learning, and building. Share your experience in the comments, and subscribe for more beginner-friendly projects to fuel your passion for innovation! 


Comments

PULSE RATE MONITOR