# Distance Measurement using Ultrasonic Sensor and Arduino

#### Byinham

Feb 24, 2021

Ultrasonic sensors are used to measure distance or detect objects without any physical contact. The ultrasonic sensor work on the principle of ECHO. There are many articles available on the internet showing the complex method of measuring distance that’s why I have decided to make the simplest project.

## Working and Explanation

Sound waves from the sensor travel in an environment when striking with any object it came back to the origin. As the speed of the sound is known we have to calculate the outgoing and incoming time of the wave. These calculations are done through the Arduino program.

This has many applications like mini radar, in parking to avoid accidents, etc.

### what is an ultrasonic sensor?

• Operating voltage: +5V
• Theoretical  Measuring Distance: 2cm to 450cm
• Practical Measuring Distance: 2cm to 80cm
• Accuracy: 3mm
• Measuring angle covered: <15°
• Operating Current: <15mA
• Operating Frequency: 40Hz

Distance =1/2* (Speed × Time)

### Components Used

• Arduino Uno
• 16×2 LCD
• Ultrasonic sensor Module
• battery source
• Connecting wires
• variable resistor

## Circuit Diagram

• At pin no 1 and 2, the virtual terminal to display data is connected.
• At PINs, 3 to 7 LCD is conned.
• At PINs 11 and 12 echo and trig pin of the ultrasonic sensor are connected.

## Coding (Distance Measurement using Ultrasonic Sensor and Arduino):

#include<LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

const int trigPin = 12;
const int echoPin = 11;
void setup()
{
Serial.begin(9600);

lcd.begin(16,2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.setCursor(0,0);
lcd.print(” Distance “);
Serial.print(” Distance “);
lcd.setCursor(0,1);
lcd.print(” Measurement “);
Serial.print(” Measurement “);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Geeky Engineers: “);
delay(1000);
Serial.print(“:”);
lcd.setCursor(0,0);
Serial.print(” www.geekyengineers.com”);
delay(2000);
lcd.clear();

}

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);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(“Distance:”);
Serial.print(cm);
Serial.print(“cm”);
delay(100);
Serial.println();
lcd.setCursor(0,0);
lcd.print(“”);
delay(10);
lcd.setCursor(0,1);
lcd.print(“Distance:”);
lcd.print(cm);
lcd.print(“cm”);
delay(100);

}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

After reading this article “How To measure distance using ultrasonic sensor” you will be able to make this project. We have tried to cover each aspect of the ultrasonic sensor and will look for more and try to add those in the next update. Do you know anything else about this project? Let us know in the comments section below.

#### By inham

Inhamullah Jadoon is an Electrical Engineer and Co Founder of geekyengineers.com. He has vast amount of experience in the field of Engineering and in assisting Engineers on Software and Hardware issues.