Introduction: Obstacle Avoiding Bot Using Ultrasonic Sensor and Arduino

About: We provide services in Embedded Projects, Website Development, App Development, Customised Electronic Solutions and Products

In this tutorial we are going to make an obstacle Avoider Bot using Ultrasonic Sensor and Arduino Uno.

Components Required –

Arduino Uno – https://amzn.to/33U9phD

Ultrasonic sensor – https://amzn.to/2OULNoC

L298N Motor Driver – https://amzn.to/2qwAGZW

2x DC Motor – https://amzn.to/34SjU6i

Chassis – https://amzn.to/2s5c3Uh

Jumpers – https://amzn.to/33VssIn

OR

Buy it from Alpha Electronz - Instagram | Whatsapp | Facebook

Step 1: Ultrasonic Sensor

An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves.To know more about Ultrasonic sensor Click here

Step 2: L298N Motor Driver

L298N Motor Driver Module is responsible for providing the necessary drive current to the motors of the robotic car.

Step 3: Circuit Diagram

The connections of Motor Driver and ultrasonic sensor is given in the above circuit diagram.All the components are attached on the chassis.After all the placement your Bot should look something like this.

For further help on Placement and connecting Everything See the full tutorial Video at the end of the post.

Step 4: Code

/* 
Obstacle Avoiding Bot using Ultrasonic Sensor and Arduino Uno.
Date - 6/12/2019
Website - <a href="http://alphaelectronz.com">http://alphaelectronz.com</a>
*/
int trigPin = 3;
int echoPin = 2;
int m1 = 5; 
int m2 = 4; 
int m3 = 7; 
int m4 = 6;
int s1 = 10;
int s2 = 11;
void setup() {
Serial.begin(9600);
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(m4, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
analogWrite(s1,170);
analogWrite(s2,170);
}
void loop() {
long duration, distance;
digitalWrite(trigPin,HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance =(duration/2)/29.1;
Serial.print(distance);
Serial.println("CM");
delay(10);
if((distance<15))
{ 
digitalWrite(m1,LOW); 
digitalWrite(m2,LOW); 
digitalWrite(m3,LOW); //
digitalWrite(m4,LOW); 
delay(1000);
digitalWrite(m1,LOW); 
digitalWrite(m2,HIGH); 
digitalWrite(m3,LOW); //
digitalWrite(m4,HIGH);
delay(500);
digitalWrite(m1,HIGH); 
digitalWrite(m2,LOW); 
digitalWrite(m3,LOW); //
digitalWrite(m4,HIGH);
delay(700);
}
else if(distance>15) 
{
digitalWrite(m1,HIGH);
digitalWrite(m2,LOW);
digitalWrite(m3,HIGH); //HIGH
digitalWrite(m4,LOW); 
}
}

Step 5: Conclusion and Output Video

Note – Possible Errors due to which your bot is not working properly.

  • Check the voltages of both batteries.
  • Use 2 batteries for Motor Driver and 1 for powering Arduino.
  • Check all the connections once again

If you have any doubts or problems regarding the code and circuit. Feel free to ask in the comments section.Or contact us on Instagram | Whatsapp | Facebook