# Distance Measurement Using Arduino & Ultrasonic Sensor

0
305

Ultrasonic sensors are excellent tools for measuring distances and detecting objects without making physical contact. It can be used to detect liquid levels, figure out how close you are, and more recently, to help with self-parking and anti-collision systems in cars. This is a quick and accurate way to measure tiny distances. To figure out how far away an object was from the sensor in this project, we used the HC-SR04 Ultrasonic Sensor and an Arduino to read the sound waves.ECHO is the fundamental basis of ultrasonic distance measurement. When sound waves travel through the environment, they return to the source as an echo after colliding with an impediment. So, after striking the obstruction, we only need to calculate the travel time of both noises, i.e., the departing time and the return time to the origin. Because we know the speed of sound, we can determine the distance with a little math. Let’s get started with this Arduino distance measurement project, where we’ll apply the same technique.

## Ultrasonic Sensor Module

#### Time x Sound Speed in Air (340 m/s) / 2 ## Hardware Pin Details

#### 16×2 Lcd Module

• GND  –       ARDUINO GND
• 5V     –       ARDUINO 5V
• RS     –       ARDUINO PIN 13
• EN     –       ARDUINO PIN 12
• D4     –       ARDUINO PIN 11
• D5     –       ARDUINO PIN 10
• D6     –       ARDUINO PIN 9
• D7     –       ARDUINO PIN 8

#### ULTRASONIC SENSOR

• VCC     –      ARDUINO 5V
• TRIG   –      ARDUINO PIN 2
• ECHO  –      ARDUINO PIN 4
• GND   –       ARDUINO GND

## ARDUINO UNO PROGRAM

```#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
const int trigPin = 2;
const int echoPin = 4;
int ledA0 = 7;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop()
{
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
pinMode(ledA0,OUTPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
lcd.setCursor(0,0);
lcd.print(inches);
lcd.setCursor(6,0);
lcd.print("inch");
lcd.setCursor(0,1);
lcd.print(cm);
lcd.setCursor(6,1);
lcd.print("cm");
delay(1000);
lcd.clear();
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}```

## Project Video 