# Arduino Based Distance Measurement using Ultrasonic Sensor

Ultrasonic sensors are great tools to measure distance without actual contact and used at several places like water level measurement, distance measurement etc. This is an efficient way to measure small distances precisely. In this project we have used an Ultrasonic Sensor to determine the distance of an obstacle from the sensor. Basic principal of ultrasonic distance measurement is based on ECHO. When sound waves are transmitted in environment then waves are return back to origin as ECHO after striking on the obstacle. So we only need to calculate the travelling time of both sounds means outgoing time and returning time to origin after striking on the obstacle. As speed of the sound is known to us, after some calculation we can calculate the distance.

### Components Used

1. Arduino Pro Mini
2. Ultrasonic sensor Module
3. 16x2 LCD
4. Scale
6. 9 volt battery
7. Connecting wires

### Ultrasonic Sensor Module

Ultrasonic sensor HC-SR04 is used here to measure distance in range of 2cm-400cm with accuracy of 3mm. The sensor module consists of ultrasonic transmitter, receiver and the control circuit. The working principle of ultrasonic sensor is as follows:

1. High level signal is sent for 10us using Trigger.
2. The module sends eight 40 KHz signals automatically, and then detects whether pulse is received or not.
3. If the signal is received, then it is through high level. The time of high duration is the time gap between sending and receiving the signal.

Distance= (Time x Speed of Sound in Air (340 m/s))/2

Timing Diagram

The module works on the natural phenomenon of ECHO of sound. A pulse is sent for about 10us to trigger the module. After which the module automatically sends 8 cycles of 40 KHz ultrasound signal and checks its echo. The signal after striking with an obstacle returns back and is captured by the receiver. Thus the distance of the obstacle from the sensor is simply calculated by the formula given as

Distance= (time x speed)/2.

Here we have divided the product of speed and time by 2 because the time is the total time it took to reach the obstacle and return back. Thus the time to reach obstacle is just half the total time taken.

### Circuit Diagram and Explanation

In circuit connections Ultrasonic sensor module’s “trigger” and “echo” pins are directly connected to pin 18(A4) and 19(A5) of arduino. A 16x2 LCD is connected with arduino in 4-bit mode. Control pin RS, RW and En are directly connected to arduino pin 2, GND and 3. And data pin D4-D7 is connected to 4, 5, 6 and 7 of arduino.

First of all we need to trigger the ultrasonic sensor module to transmit signal by using arduino and then wait for receive ECHO. Arduino reads the time between triggering and Received ECHO. We know that speed of sound is around 340m/s. so we can calculate distance by using given formula:

Distance= (travel time/2) * speed of sound

Where speed of sound around 340m per second.

A 16x2 LCD is used for displaying distance.

Find more about the working of distance measurement project in this tutorial: Distance measurement using ultrasonic sensor.

### Program Description

In code we read time by using pulseIn(pin). And then perform calculations and displayed result on 16x2 LCD by using appropriate functions.

Code:

#include <LiquidCrystal.h>

#define trigger 18
#define echo 19

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

float time=0,distance=0;

void setup()
{
lcd.begin(16,2);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
lcd.print(" Ultra sonic");
lcd.setCursor(0,1);
lcd.print("Distance Meter");
delay(2000);
lcd.clear();
lcd.print(" Circuit Digest");
delay(2000);
}

void loop()
{
lcd.clear();
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*340/20000;
lcd.clear();
lcd.print("Distance:");
lcd.print(distance);
lcd.print("cm");
lcd.setCursor(0,1);
lcd.print("Distance:");
lcd.print(distance/100);
lcd.print("m");
delay(1000);
}

Video:

CircuitDigest highly recommends EasyEDA for Circuit Design and PCB Prototype

Low Cost Prototype PCB: 10 pcs 2 layers only \$10,quick delivery,100% E-test

• Htoo

Can you give me a distance meter flow chart.

Oct 18, 2015
• Dan

nice work.. i just have one query
can you tell me the calculation of (voltage change per distance?)
thanks

Jan 11, 2016
• varun

i need this copy

Feb 03, 2016
• MOHANKUMAR

i want to know the arduino model

Feb 20, 2016

I think you should read the arduino.cc page for more about arduino.

Feb 25, 2016
• Its Arduino Pro Mini, as mentioned in Components section.

Feb 27, 2016
• mark

can anyone put the link to buy arduino pro mini and IR sensor used here??? plzz help

Apr 12, 2016
• umesh

i need complete detailed calculation of distance

Apr 22, 2016
• dennis

can you program the measurement sensor to power a motor at a particular distance for a short period of time

May 09, 2016
• Nitin kumar

Hi,

i am also implement this type of projects but real time problem is how to mobile no is store through sms.
and send the sms from gsm your mobile no is registered. second problem tx /rx pin. what pin are using tx and rx . Nobody can entered the this type of sms because sms is tool long .

command:

Tv On tv off
on off
like another message.
after send the sms through the mobile then sms memory is full how to delete the sms memory after gsm read the message . pl use eeprom header and use the eeprom memory in ardunio. then store the mobile no and you can easily change mobile no .

May 18, 2016
• harish

HOW TO CURRENT SUPPLY TO PRO MINI

May 18, 2016
• harish

i am already programme the arduino pro mini using arduino uno board
uno rest to pro mini rest button
uno vcc to pro mini vcc button
uno gnd to pro mini gnd button
uno tx and rx to pro mini txo and rxi button
it is correct or not sir

another one thing is my pro mini board consists
A6 and A7 pin
but in circuit diagram pro mini board consists a A4, A5 pin
it is correct or not
pls tell that sir

May 19, 2016
• You can connect to A6 and A7 but then define the Trigger and 'echo' pin accordingly.

May 21, 2016
• harish

project is success sir thank you for your response
but in this programme [340/20000]. how its possible
because in explanation is 340/2
but it is not use in the progromme why sir

May 23, 2016
• harish

digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);

pls tell meaning of the lines sir
i don't know sir

Sep 12, 2016
• Cherish

Why does this project use Arduino Pro Mini and all I was seeing from YouTube and other sites are Arduino Mega or Arduino Uno. Is there a significant difference? Does the code above really works?

Sep 30, 2016
• You can use any model, Code will work in all of them. Further Arduino Pro Mini is small in size and generally used on PCBs because of its size.

Oct 06, 2016
• akash

Hey if I use ultrasonic sensor for moving robot then how it will be calculate the distance?????

Oct 17, 2016
• Ko

Hi
im curious about how to measure inclined plane
with inclined plane distance data has an error
do u know method to modify this problem?

Nov 06, 2016
• unknown

we are making this project and our teacher asked if what is scale?

Dec 10, 2016
• Che

Hai
We are planning to make this project and our teacher asked what is the used of Scale because it is in the components thanks.

Dec 11, 2016
• thala mathi(vaalu)

its a mind blowing project....therikka vitutinga...

Jan 05, 2017