distance measurement

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


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.



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;



void setup()











void loop()



























 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.


  1. Greetings! Very useful advice in this particular post! It is the little changes which will make the most significant changes. Many thanks for sharing!

  2. Hey, I think your blog might be having browser compatibility issues. When I look at your blog site in Ie, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, superb blog!

  3. Awesome blog you have here but I was wanting to know if you knew of any message boards that cover the same topics talked about in this article? I’d really love to be a part of online community where I can get feedback from other experienced individuals that share the same interest. If you have any suggestions, please let me know. Kudos!|

  4. Everything typed was actually very reasonable. But, what about this? suppose you typed a catchier title? I am not saying your content isn’t good, however suppose you added a title to possibly get people’s attention? I mean %BLOG_TITLE% is a little boring. You might glance at Yahoo’s front page and watch how they create article headlines to get people to open the links. You might add a video or a related pic or two to grab people interested about everything’ve written. Just my opinion, it could bring your blog a little livelier.

    • Thank you for your suggestion. Sorry for delayed in reply. I am very thankful that you posted such valuable and informative comment . Thank you , I will work on this.

  5. Nice post. I was checking constantly this weblog and I’m inspired! Very useful information specially the ultimate part 🙂 I care for such info a lot. I was seeking this certain info for a very long time. Thanks and best of luck.

  6. My partner and I stumbled over here different web address and thought I might check things out. I like what I see so now i’m following you. Look forward to looking over your web page again.|

  7. naturally like your web-site but you have to take a look at the spelling on several of your posts. A number of them are rife with spelling issues and I find it very troublesome to inform the truth then again I’ll certainly come again again.

Leave A Reply

Please enter your comment!
Please enter your name here