Obstacle Avoiding Bot Using Ultrasonic Sensor and Arduino

Obstacle Avoiding Bot Using Ultrasonic Sensor and Arduino
Obstacle Avoiding Bot Using Ultrasonic Sensor and Arduino

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

Components Required –

Ultrasonic Sensor HSR04

Ultasonic Sensor HSR04
Ultasonic Sensor HSR04

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

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

Circuit Diagram of Obstacle Avoiding Bot

Circuit Diagram of Obstacle Avoiding Bot
Circuit Diagram of Obstacle Avoiding Bot

The connections of Motor Driver and the ultrasonic sensor is given in the above circuit diagram. All the components are attached to 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.

Working Code

/* 
Obstacle Avoiding Bot using Ultrasonic Sensor and Arduino Uno.
Date - 6/12/2019
Website - <a href="https://alphaelectronz.com">https://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); 
}
}

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

Leave a Reply

Your email address will not be published. Required fields are marked *