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 means a robot moving around in an unknown environment while not colliding 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)


To successfully implement your project, we have done 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:

Trig = Trigger input of Sensor (pin no 12 for our project)
Echo = Echo output of Sensor (pin no 11 for our project)

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 designing 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 any 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 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 way 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

2/Post a Comment/Comments

  1. Can i use arduino UNO replace mega?

    1. Of course, we can use Arduino UNO in this project. You can also use Arduino Pro Mini or Arduino MINI. Because only a few IO pins we need for obstacle avoiding robot. So feel free to play with different Arduino boards. Thanks for your comment.


Post a Comment