Day 01

Begineer

Day 25 :- Obstacle Avoiding Robot

Sept 08, 2026

Completed

Wiring Schematic

Arduino Code

ARDUINO / C++

Day 25 Core Training Documentation

Project Overview

The Obstacle Avoiding Robot project teaches you how to build an autonomous robotic system that combines sensing and movement to navigate independently. This project creates a smart robot that detects obstacles and avoids them automatically..

In this project, an HC-SR04 ultrasonic sensor continuously scans for obstacles. Two DC motors drive the robot forward. When the sensor detects an obstacle too close, Arduino stops the motors, rotates the robot, and resumes movement.

When the robot powers up, it moves forward autonomously. As it approaches an obstacle, the sensor detects it within safe distance. The robot automatically stops, backs up slightly, rotates, and continues. This behavior repeats indefinitely.

This project teaches Autonomous Navigation, Obstacle Detection and Response, Motor Coordination, Decision-Making Logic, and Robotics Fundamentals.

By finishing this project, you’ll know how autonomous robots work in warehouse automation, robotic vacuum cleaners, rover vehicles, and industrial inspection systems.

Components Required

To build this circuit, collect these specific items from your kit:

Arduino Uno × 1, Breadboard × 1, Robot Chassis × 1, DC Motors × 2, Motor Driver Module (L298N) × 1, HC-SR04 Ultrasonic Sensor × 1, Servo Motor × 1, Battery Pack (6V-9V) × 1, Caster Wheel × 1, Jumper Wires × 15, USB Cable × 1

Circuit Connections

Step 1: Connect Ground Rail

Connect the Arduino GND pin to the negative rail of the breadboard. This creates a shared ground for all components in the robot system.

Step 2: Connect Battery to Motor Driver

Connect the positive terminal of the battery pack to the +12V input of the L298N motor driver. Connect the negative terminal to the GND of the motor driver.

Step 3: Connect Motor Driver to Arduino GND

Connect the GND pin of the motor driver to Arduino GND to establish a common ground reference point for proper operation.

Step 4: Connect Left Motor

Connect the left motor’s positive terminal to OUT1 of the motor driver. Connect the negative terminal to OUT2 of the motor driver.

Step 5: Connect Right Motor

Connect the right motor’s positive terminal to OUT3 of the motor driver. Connect the negative terminal to OUT4 of the motor driver.

Step 6: Connect Motor Speed Control

Connect the ENA pin of the motor driver to Arduino Pin 5. Connect the ENB pin to Arduino Pin 6 for PWM speed control.

Step 7: Connect Motor Direction Control

Connect the IN1 pin to Arduino Pin 2. Connect IN2 to Pin 3 and IN3 to Pin 4. These pins control motor direction.

Step 8: Connect HC-SR04 Sensor

Connect the HC-SR04 VCC to Arduino 5V. Connect GND to breadboard GND. Connect TRIG to Pin 9 and ECHO to Pin 10.

Step 9: Connect Servo Motor

Connect the servo VCC to Arduino 5V. Connect servo GND to breadboard GND. Connect the servo signal to Arduino Pin 8.

Step 10: Mount All Components

Attach the motors to the robot chassis. Mount the ultrasonic sensor on a servo for scanning capability. Secure Arduino and breadboard to frame.

Step 11: Verify All Connections

Test motor rotation by manually turning the wheel. Verify sensor readings in Serial Monitor. Ensure battery voltage is appropriate for motors.

Important: The robot requires stable power. Battery voltage must be sufficient for motor operation. Test each motor independently before integrating obstacle avoidance system.

How the Arduino Code Works

Initialize Motors and Sensor

In setup(), Arduino configures pins 2 through 6 as outputs for motor control. Pins 9 and 10 are configured for the ultrasonic sensor. Pin 8 controls the servo scanner.

Measure Distance Continuously

Inside the main loop, Arduino measures the distance to objects using the HC-SR04 sensor. If distance is greater than 20 centimeters, the obstacle is far away.

Move Forward Automatically

When no obstacle is detected or the obstacle is far away, Arduino sends forward movement commands to both motors. Both motors receive equal PWM values.

Detect Obstacle Approach

When the distance reading drops below 20 centimeters, Arduino recognizes an obstacle is too close and initiates avoidance behavior immediately.
 

Execute Avoidance Sequence

First, the motors stop completely. Then the robot backs up by reversing motor direction for half a second. Next, the robot rotates for one second. Finally, motors resume forward movement.

Expected Output

After uploading your code, here is what happens.

After uploading your code, the robot moves forward automatically. As it approaches a wall, distance decreases and the robot stops. The robot backs up, rotates left or right, then resumes forward movement. This cycle repeats continuously.

Operational Troubleshooting

Robot Does Not Move :- Verify the battery is fully charged and properly connected. Check the motor connections at the driver output terminals. Test each motor individually to ensure functionality.

Robot Moves Only in Circles :- Check that both motors receive equal speed values. Adjust the PWM values to balance motor power. If motors have different speeds, one will rotate faster.

Obstacle Detection Does Not Work :- Verify the HC-SR04 connections to Pins 9 and 10. Check the distance threshold value in your code. Test the sensor independently with Day 9 code.

Robot Does Not Turn Properly :- Check the rotation time delay value. If too short, the robot won’t rotate enough. If too long, the robot rotates too much.

Servo Scanner Does Not Rotate :- Verify the servo signal wire connects to Pin 8. Check that servo receives proper 5V power. Ensure the servo write() commands include appropriate angle values.

What You Learned

By finishing this project, you now know :-

How to coordinate multiple motors simultaneously for movement control. You understand how to implement autonomous decision-making logic for navigation. You learned to create obstacle detection and response systems effectively. You mastered building practical robotic platforms with independent operation. You developed intelligent navigation behaviors for autonomous systems.

Next Lesson

In Day 26, you’ll learn how to build a Line Follower Robot that autonomously follows a black line on the ground using infrared sensors. You will implement proportional control for precise path following.