Obstacle Avoider Bot using Ultrasonic Sensor & Arduino

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

Components Required –

Ultrasonic Sensor

Ultrasonic sensor | Alpha Electronz
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

L298N Motor Driver

L298N Motor Driver | Alpha Electronz
L298N Motor Driver

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

Circuit Diagram

Obstacle Avoiding Bot Circuit Diagram | Alpha Electronz

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.

Obstacle Avoiding Bot using Ultrasonic Sensor and Arduino | Alpha Electronz

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

Code


/* Obstacle Avoiding Bot using Ultrasonic Sensor and Arduino Uno.

Date - 6/12/2019Website - https://alphaelectronz.com*/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); 
}
}

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

Output Video

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

0

Leave a Reply

Your e-mail address will not be published. Required fields are marked *