Last Updated on

Distance Measurement using Arduino:

There is number of applications that require to measure distance between two moving or stationary objects like water level measurement, distance measurement etc. Ultrasonic sensors like HC-SR04 are widely used to measure the distance without actual contact.

Components Used :

  1. Arduino UNO
  2. 16×2 LCD
  3. HC-SR04 Ultrasonic Distance Sensor
  4. Bread board
  5. Connecting wires

Ultrasonic sensor HC-SR04

ultrasonic-sensor

Interfacing HC-SR04 to a microcontroller is very much easy. trig pin is used to trigger an ultrasonic sensor and then “listen” for the echo pulse. The sensor measures the time required for the echo return, and returns this to the microcontroller as a variable pulse width via the echo pin.

Key Features:

  • Precise, non-contact distance measurements
  • Range 2 cm to 4 m range
  • work in any lighting condition
  • Requires just two microcontroller I/O pin
  • Power requirements: +5 VDC;

Timing Diagram

The module works on the natural phenomenon of ECHO of sound. A high pulse is sent for about 10us to trigger the module. After which the module sends 8 cycles of 40 KHz ultrasound signal through transmitter and checks its echo received at receiver. By using this time span triggering pulse and echo you can calculate the distance by using formula as

Distance = (time  x  Speed)/ 2

Where speed= 340m/s (speed of sound wave or ultrasonic signal)

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.

timing-diagram

 

Circuit Diagram :

distance-measurement-circuit-diagram

 

In circuit connection

 

Ultrasonic sensor module’s

  1. “trigger” to A4of arduino
  2. “echo” to A5of arduino
  3. Vcc to 5V and
  4. Gnd to Ground of Arduino

 

16×2 LCD connected in  4-bit mode as

  1. RS – pin 2
  2. En–pin 3
  3. D4-D7 –pin 4- pin7
  4. RW, LED-, Vss– GND and Vdd, LED+ –  5V

 

Code :

#include <LiquidCrystal.h>

#define trigger 18

#define echo 19

float time=0,distance=0;

LiquidCrystallcd(2,3,4,5,6,7);

 

void setup()

{

lcd.begin(16,2);

pinMode(trigger,OUTPUT);

pinMode(echo,INPUT);

lcd.setCursor(0,1);

delay(2000);

lcd.clear();

delay(1000);

}

 

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);

 

}

 

Results:

Result-distance-measurement

Result-Distance-Measurement

 

 See alsoArduino Traffic Light Controller || IoT Project

 

Let us know what you think about ‘How exactly are Distance measurement using Arduino in the comment section below.

If you like this post subscribe to our YouTube Channel for IoT video Tutorials. You can also find us on Twitter, Facebook, and Instagram for more updates.

and If you have smart product or service and looking for perfect audience then submit a guest post at IoTDunia and get to know in IoT world.