Robotics • FREE PROJECT

Obstacle Avoiding Robot

Obstacle Avoiding Robot circuit diagram
Intermediate Robotics ⏱ 90 Minutes

Combine an ultrasonic sensor with motor control so your robot senses obstacles and steers around them on its own.

What You Will Learn

  • Combining sensing and motor control in one program
  • Simple decision-making logic for autonomous movement
  • Using delay-based turning to change direction
  • Debugging robot behaviour with the Serial Monitor

Components Required

Arduino Uno HC-SR04 Sensor L298N Motor Driver 2x DC Gear Motors Robot Chassis Battery Pack
BUILD IT STEP BY STEP

Follow along and build with confidence.

Every step includes a visual reference and a plain-language explanation, so you always know what to do next.

STEP 01

Mount the Sensor & Motors

🔧

Fix the HC-SR04 facing forward at the front of the chassis, and mount both gear motors and wheels so the robot can drive and turn freely.

STEP 02

Wire Everything Together

🔌

Connect the ultrasonic sensor's TRIG/ECHO to pins 9 and 10, and the L298N motor control pins to 5, 6, 9... (use free pins, e.g. 3 and 11 if 9/10 are taken by the sensor).

STEP 03

Upload the Code

💻

The robot drives forward continuously, checking distance each loop. If an obstacle is closer than 15cm, it stops, reverses briefly, then turns before continuing.

cpp
#define TRIG 9
#define ECHO 10
#define LEFT_MOTOR 5
#define RIGHT_MOTOR 6

long getDistanceCm() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  long duration = pulseIn(ECHO, HIGH);
  return duration * 0.034 / 2;
}

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(LEFT_MOTOR, OUTPUT);
  pinMode(RIGHT_MOTOR, OUTPUT);
}

void loop() {
  long distance = getDistanceCm();

  if (distance > 15) {
    // Path clear: go forward
    analogWrite(LEFT_MOTOR, 150);
    analogWrite(RIGHT_MOTOR, 150);
  } else {
    // Obstacle ahead: stop, back up, turn
    analogWrite(LEFT_MOTOR, 0);
    analogWrite(RIGHT_MOTOR, 0);
    delay(200);

    analogWrite(LEFT_MOTOR, 100);
    analogWrite(RIGHT_MOTOR, 30);
    delay(400);
  }
}
STEP 04

Test Your Project

✅

Power the robot on an open floor. It should drive forward and automatically turn away whenever it gets close to a wall or obstacle.

STUCK ON SOMETHING?

Troubleshooting & common fixes.

Robot turns constantly, even in open space.

The 15cm threshold may be too generous for your room — increase it slightly, or check the sensor isn't picking up reflections from the chassis itself.

Robot hits obstacles before reacting.

Increase the distance threshold (e.g. to 20–25cm) to give the robot more time to stop and turn before contact.

Robot turns the same direction every time and gets stuck.

Alternate the turn direction each time an obstacle is detected, or add a second sensor to compare left/right space.

WANT GUIDED, HANDS-ON HELP?

Bring this project to life with a mentor.

Loved building this for free? Our instructor-led workshops go deeper with kits, live guidance and more advanced builds.

Plan a Workshop →
KEEP BUILDING

More free workshops.

💡

LED Blinking Using Arduino UNO

Learn how to control an LED using Arduino UNO by making it turn ON and OFF automatically. This is the basic concept behind indicator lights, warning lights, and signal systems used in real-world electronic devices.

View Free Project →
📏

Motion Detection Project

Use a PIR sensor to detect human movement and automatically switch ON an LED. This demonstrates the technology used in security systems, automatic lights, smart homes, and occupancy detection.

View Free Project →
🛤️

Automatic Street Light

Build a smart street light that automatically turns ON when it gets dark and OFF during daylight using an LDR sensor. This concept is commonly used in street lights, garden lights, parking areas, and energy-saving lighting systems.

View Free Project →