Ultrasonic Distance Meter Using Arduino

In this tutorial, I will be able to show you the way to create an ultrasonic distance meter using an Arduino. This project uses an HC-SR04 distance sensor and atmega328P or the other ATMEGA chip which is programmed using an Arduino programming kit, that the project is predicated on Arduino. But our final Circuit is a standalone circuit without the attached Arduino programmer.

Circuit Diagram

ultrasonic distance meter

Components Required

  • HC-SR04 Ultrasonic Sensor
  • Arduino
  • 16*2 LCD Display
  • Jumper Wires
  • Breadboard
  • 5V Power Supply

Circuit Connection

LCD display to Arduino Nano setup connection

  • Connect LCD pins 1, 3, 5,16 to the GND pin of Arduino Connect LCD pin 4 to the
  • pin D7 of ArduinoConnect LCD pin 6 to pin D6 of ArduinoConnect LCD pin 11 to
  • pin D5 of ArduinoConnect LCD pin 12 to pin D4 of ArduinoConnect LCD pin 13 to
  • pin D3 of ArduinoConnect LCD pin 14 to pin D2 of Arduino
  • Connect LCD pins 2, 15 to the VCC(+5V) pin of Arduino
  • Connect HC-SR04 VCC pin to +5V pin of Arduino
  • Connect HC-SR04 GND pin to GND pin of Arduino
  • Connect Trig pin to pin D9 of Arduino
  • Connect Echo pin to pin D8 of Arduino

Working of Ultrasonic Distance Meter

In this ultrasonic distance meter working principle, we can send a brief ultrasonic pulse at the trigger, and if any obstacles are there, the reflected received ultrasonic signal is converted to an electrical signal as an echo. If no obstacle is detected, the output pin will provide a 38ms high-level signal. 

Here, we calculate the gap of the ultrasonic distance meter and any object before it. At t1 we send the trigger signal, and at t2 we get the echo signal 

dt = (t2-t1)

The exact time taken by the sound to succeed in the item is: dt/2

Now, Normal speed of sound is: 340 m/s 

And after converting it to centimeters per microseconds it gives: 0.034049 cm/microsecond 

The actual distance travelled by the sound or the gap between the sensor and also the object is: 

Object = (dt/2) * 0.034049

By using this formula we can get the output of the actual distance of an object from ultrasonic distance meter.

Arduino Code

/*Electro Gadget - circuitdiagrams.in*/
#define trigPin 7
#define echoPin 8
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.begin(16, 2);
lcd.print("Electro");
lcd.setCursor(0, 1);
lcd.print("Gadget");
delay(2000);
lcd.clear();
lcd.print("Ultrasonic");
lcd.setCursor(0, 1);
lcd.print("Distnance Meter");
delay(2000);
lcd.clear();
lcd.print("Designed by:");
lcd.setCursor(0, 1);
lcd.print("Electro Gadget");
}
void loop() {
int timetaken, dist;
lcd.clear();
lcd.print("Distance in cm:");
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
timetaken = pulseIn(echoPin, HIGH);
dist = (timetaken/2) * 0.034049 ;
if (dist >= 300 || dist <= 0)
{
lcd.setCursor(0, 1);
lcd.print("Out Of Range");
}
else
{
lcd.setCursor(0, 1);
lcd.print(dist);
}
delay(500);
}

Video Output

Visit our site for more awsome tutorial Electro Gadget

Leave a comment