Obstacle Avoiding Robot using Arduino with Code

Obstacle detection and avoiding robots are intelligent robots that can perform desired tasks in an unstructured environment by finding and overcoming obstacles in their way without continuous human guidance.

Obstacle avoidance refers to a robot navigating an unknown environment while avoiding collisions with surrounding objects. Obstacle avoidance can be found on many different machines, big and industrial robots, or even on newer cars. Knowing how to program obstacle avoidance into your robots can be a beneficial skill to have, whether for a bit of a robot project or a more extensive project where obstacle avoidance is just a tiny piece of the larger picture. 

We will need the following components for this project:

1. Microcontroller (Arduino Mega 2560)
2. Motor Shield (L298P)
3. Ultrasonic Sensor (HC08)
4. BreadBoard
5. Robot chassis with 4 DC motors and 4 wheels
6. Connecting Wires
7. Power source (9v battery)
8. Arduino IDE 
9. Servo motor (SG90)

CIRCUIT DESIGN:

To successfully implement your project, we have taken these steps to design our robotic circuit connection. 

STEP 1: Robot chassis DC motors connection to Motor Shield

Each motor has +ve and –ve terminals. Two +ve and –ve terminals of both left-side and right-side DC motors are shorted separately to be connected to the ports of motor shield A+, A-( 2 +ve terminals of left side DC motors are connected to A+ and 2 -ve terminals of left side DC motors are connected to A-) and B+, B-( 2 +ve terminals of right-side DC motors are connected to B+ and 2 -ve terminals of right-side DC motors are connected to B-) respectively. 

L298P Motor Driver Shield
L298P Motor Driver Shield

STEP 2: Servo motor connection

We used the Servo motor, Tower Pro SG90 9G Micro Small Servo Motor, to control movement 0-180 degrees. This controls the ultrasonic head movement for sensing an obstacle. It has three wires:

Red = +5v

Brown = GND

Orange = Signal (pin 10 for our code)

STEP 3:  Ultrasonic Sensor Connection

The ultrasonic sensor we used was HC-SR04. It consists of 4 wires for the following connection:

VCC = +5VDC
Trig = Trigger input of Sensor (pin no 12 for our project)
Echo = Echo output of Sensor (pin no 11 for our project)
GND = GND

STEP 4:  Horn and Light Connection

Horn and Light we used here. It consists of 3 wires for the following connection:

Horn = pin 2 in the motor shield 
Light = 1 in the motor shield 
        GND = GND

We completed our circuit design of the Obstacle Avoidance Robot by following the above procedure.

Working Principle of this Obstacle Avoiding Robot

The obstacle-avoiding Robot we created was mainly based on the working principle of 

Ultrasonic sensor. Our Robot initially moves forward until it gets an kind of Obstacle. During driving, every time, it will check if there is any obstacle nearby by continuously sending and receiving distance-related information. The Trig pin will be used to send the signal, and the Echo pin will be used to listen for the returning signal.

So, we see an object closer than 30 cm to our Robot. The check has two possible outcomes: yes or no. Yes, meaning that there is indeed some object closer than 30 cm. No, meaning that there are no objects detected within 30 cm. If nothing is within 30 cm, the Robot can simply move forward as the path is clear. If there is something closer than 30 cm, the Robot must perform obstacle avoidance maneuvers.

Another check occurs to see what direction is the best direction to go. If left is the way to go, it has to turn back to the left and go forward. If the right is the way to go, the Robot simply moves forward as it is already facing the right direction.

All we have done is convert that diagram into Arduino code, and that's it. So, quickly, the obstacle avoidance code was done.

Flow Diagram of Arduino Obstacle Avoiding Robot
Flow Diagram of Arduino Obstacle-Avoiding Robot

Arduino Code for Obstacle-Avoiding Robot:

Physical Obstacle Avoiding Robot:

Obstacle Avoiding Robot using Arduino with Code
Obstacle Avoiding Robot using Arduino and L298P

In addition to the basic obstacle avoidance mechanisms, the robot design can also be enhanced by incorporating more intelligent behaviours. For example, the ultrasonic sensor mounted on the servo allows the robot to scan its surroundings rather than just checking the front. This scanning approach helps create a more accurate estimation of which direction is safer to move. By integrating more complex logic, such as storing the last known safe direction or prioritising paths based on available space, the robot can behave more naturally and efficiently in a cluttered environment.

Furthermore, the L298P motor driver provides adequate current to run dual motor pairs on both sides, making it suitable for medium-sized robots. By controlling motor speed and direction individually, the robot can perform precise turns, smooth braking, and speed adjustments depending on the situation. Combining this with the servo-mounted ultrasonic sensor offers a dynamic decision-making approach, where both speed and direction can change instantly when an obstacle is detected.

To improve performance further, additional sensors such as infrared proximity sensors, bump switches, or even a second ultrasonic module can be added. These sensors can help the robot detect objects that are out of the primary sensor’s field of vision, ensuring more reliable navigation. Adding LEDs, buzzers, or display modules also helps visualise what the robot is doing at any moment, making debugging and demonstrations much easier.

The overall modularity of the components makes it simple to upgrade or replace any part of the robot. Whether you want to experiment with different algorithms, integrate Bluetooth or Wi-Fi control, or add autonomous mapping features, this basic obstacle-avoidance platform serves as a strong foundation for further robotics projects.