Introduction: Obstacle Avoider Robot

About: An Electronics engineer and a hobbyist. I love to keep experimenting with microcontrollers.

Hello guys and welcome to another instructable. Today we will make an obstacle avoiding robot. This a beginner level project every person should try once they get their Arduino. I will share all the details about this project further so don't forget to give this instructable a "Heart".

Step 1: Working of the Project

The obstacle avoiding robot uses a ultrasonic sensor to detect obstacles. The acquired data (distance) is the sent to the Arduino. This data is then compared with a predefined distance and then the signals to the motors are sent via the motor driver module.The ultrasonic sensor is a sensor which senses the transmitted ultrasonic wave. The sensor first transmits a wave and then waits for it to get back. Meanwhile the wave bounces back after hitting a object. When the wave reaches back to the sensor, the distance from which the wave bounced back is calculated. Now in our project there happen to be two cases once we get the distance from which the wave returned.If the new distance is greater than the safe distance it means the obstacle is not present. If the distance is less than the safe distance it means the obstacle is close to the robot. If the distance is greater the Robot keeps on moving in forward direction. If an obstacle is present then the robot takes a right turn and then keeps on repeating the same process back from the start.

Step 2: Getting the Supplies

I assume you are a beginner to robotics. Or even if you are not, you always need a reliable website where you can buy your electronics components or modules. So I recommend UTSource.net for that. They provide great quality components at affordable rates and ship right to your door step. They also provide PCB Services for up to 16 layers. In short they are a All in One store for us electronics hobbyists. Do check them out !!!

Things you need -

1. Arduino Uno Board

2. Sr04 Ultrasonic sensor

3. L293D Motor Driver Module

4. Metal Chassis with Wheels & Motors

5. Connecting Wires

6. Batteries for powering the robot (I used a 9 V battery for demonstration)

Step 3: Circuit Diagram

The circuit diagram for this project is given above.

The connections are quite simple. First connect the inputs of the Motor Driver module to digital pin 10, 9, 5 & 6 respectively.

Now connect the output of the motor driver to the motors.

Connect the Vcc and Gnd of the Motor Driver Module.

Now its time to connect the ultrasonic sensor to arduino.

Connect the Trigger pin to digital pin 12 and Echo pin to digital pin 2.

The Vcc and ground of the sensor are connected to the 5v and gnd pin of the arduino.

Step 4: Coding

The code for this project is given below. You can copy it or download the .ino file directly.

const int trigPin=11;  //DECLARE TRIG PIN AT D12<br>int echoPin=10;         //DECLARE ECHO PIN AT D2
int safezone=10;       //SAFE DISTANCE FROM THE OBJECT
//Motor A
const int motorPin1  = 5;  // LEFT SIDE WHEEL
const int motorPin2  = 6;  // 5-BLACK 6- GREY
//Motor B
const int motorPin3  = 10; // 9-ORANGE 10-VOILET
const int motorPin4  = 9;  // RIGHT SIDE WHEEL
void setup()
{
pinMode(motorPin1, OUTPUT); 
pinMode(motorPin2, OUTPUT);  //DECLARE PIN MODES
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
Serial.begin(9600);          //BEGIN SERIAL COMMUNICATION WITH BAUD RATE OF 9600
}
void loop()
{
long duration,cm;           //DECLARE VARIABLES TO STORE SENSOR O/P
digitalWrite(trigPin,LOW);  //MAKE THE TRIG PIN LOW
delayMicroseconds(2);       //WAIT FOR FEW MICROSECONDS
digitalWrite(trigPin,HIGH); //NOW SET THE TRIG PIN
delayMicroseconds(5);       //WAIT FOR FEW MICROSECONDS UNTIL THE TRIG PULSE IS SENT
digitalWrite(trigPin,LOW);  //MAKE THE TRIG PIN LOW AGAIN
duration=pulseIn(echoPin,HIGH); //MAKE ECHO PIN HIGH AND STORE THE BOUNCED PULSE IN VARIABLE DURATION
cm=microsecondsToCentimeters(duration); //CONVERT DURATION INTO CM
long inch= cm/2.54;//PRINT THE CM"s IN SERIAL MONITOR
Serial.println(inch);
if(cm>safezone)   
{
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);//IF CM IS GREATER THAN SAFEZONE MOVE FORWARD
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW);
}
else
{
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);//IF CM IS LESS THAN SAFEZONE TAKE A LEFT TURN AND MOVE FORWARD
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, HIGH);
    delay(2000);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW);
}
delay(100);
}
long microsecondsToCentimeters(long microseconds) //SPEED OF SOUND IS 29 uSEC PER CM
{
return microseconds/29/2;  //THE PULSE TRAVELS FROM THE SENSOR AND AGAIN COMES BACK SO WE DIVIDE IT BY 2 TO TAKE ONLY HALF OF THE TOTAL DISTANCE
}

I have explained the code using comments above. But still if you need any help, feel free to drop a comment below.

Step 5: Video of the Project

The working of this project is shown above in the video. Watch it to get some idea about the project.

I also included a .gif file for those who can't download the video.

If this project helped you in any way then please do share it with your friends. Also follow me here to see more of my projects.

That's it for today. See you soon with another project.