Obstacle Avoiding Robot with arduino

Written by: roboguru


An OBSTACLE AVOIDING ROBOT is one which can avoid an obstacle by using ultrasound sensor and navigate in its own path. With a breadboard attached to the robot you can play fun within a short period of time. One such is what we are going to discuss here. This project can teach you how a sensor can be used to process some data.



ü Ultrasonic sensor (SR 04)

ü 2 low current motors

ü 12v battery

What is an ARDUINO?

Arduino Uno is a single board microcontroller intended to make the application of interactive objects or environmentsmore accessible. The hardware consists of an open source hardware board designed around an 8-bit atmel AVR microcontroller or a 32 bit atmel ARM. The Arduino board used for this purpose is Arduino UNO. You can find more about this board in Arduino UNO.

What is an Ultra sonic sensor?

An Ultrasonic sensor generate high frequency sound waves and evaluate the echo which is received back by the sensor, measuring the time interval between sending the signal and receiving the echo to determine the distance to an object.A Sr-04 ultrasonic module has been used here for the demo purpose . It has a power, ground and two data pins(Trigger, Echo).The usage of this module is explained in Usage of US 04

Where to start with?

This is a simple weekend project that could be completed in less than a day following the steps given below:


Take an ARDUINO UNO and connect it with your PC using a data cable. ARDUINO IDE is the platform that is used to write the codes and burn it in the microcontroller of ARDUINO. Hence, it is necessary to have this software installed in the PC before connecting the ARDUINO UNO. You can ping through the options available in this software before it is used. You can get started in an Arduino IDE environment very easily by seeing, Getting started with Arduino IDE


An ARDUINO code which is written in ARDUINO IDE (Software) is shown below. Here ultrasonic sensor reading is used to calculate the distance from the sensor to any obstacle before it and these readings are used to control the motor.

If you are a novice you could try this simple ARDUINO IDE code which is given below.


//ARDUINO IDE code for Obstacle detecting Robot

const int trig = 12;

const int echo = 11;

const int motorPin1 = 2;//left motor of the robot

const int motorPin1 = 3;//right motor of the robot

long duration, inches, cm;

void setup()





pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);


void loop()


digitalWrite(trig, LOW);


digitalWrite(trig, HIGH);


digitalWrite(trig, LOW);

duration = pulseIn(echo, HIGH);

//this returns the time duration taken

//for the ultrasonics to hit an obstacle and return

inches = duration / 74 / 2;//converts the time duration into inches

cm = duration / 29 / 2;//converts the time duration to cm

if(cm>4)//checks for the distance is greater than 4cm

//the bot forward if the condition is true

digitalWrite(motorPin1,HIGH);//Both the motors are in ON state


Serial.print("No obstacle detected");


//the robot turns right when an obstacle is detected

digitalWrite(motorPin1,HIGH);//the left motor is in ON state

digitalWrite(motorPin2,LOW);//the right motor is in OFF state

Serial.print("Obstacle detected");

delay(100);//this delays the process by 100millisec


Serial.print(“in, “);




delay(100);//this delays the code by 0.1 second and repeats the loop again



You have to burn this code into the ARDUINO UNO board using the UPLOAD option.

In this code we first initialize the ultrasonic and the 2 motors with the pins in which they are connected. Then we get an input from the ultrasonic sensor and convert the duration(time period) into cm and inches in order to measure distance. The distance recorded is compared with 4cm (in this case) and the condition is checked. If the condition is TRUE the two MOTORS are in high state and they roll forward. But when the condition fails, the right motor stops rotating and the left motor continues to rotate for a specified time period in the delay. This makes the robot to turn towards the right direction. The loop is executed once again and the process continues and the process goes on…


Make the circuit connections as shown in the picture below.

This circuit is exclusively for low current motors used in toy cars. In case you use a high power DC motor, you may need to add a Motor driver circuit. You can find more about it in blog.iqubean.in


Make a trial test before you build up a chassis for the robot.


Now it is ready. All you need is to build a chassis and fit these modules in the chase and experiment yourself….

This project can be extended where the user can have three ultrasonic sensors and develop a chase where the robot can track its own path and navigate.

There you Go!.. Please don't forget to drop your valuable comments!....

For further updates stay tuned with http://myelectrokart.blogspot.in/...

detailed explanation