How To Measure Distance Using Ultrasonic Distance Meter

Published by Ubaid Khan on

Ultrasonic distance meter

An ultrasonic distance meter an Arduino meter which measure a short duration distance and show distance on the serial monitor.


An ultrasonic sensor is a device that can measure the distance to An object by using sound waves. It measures distance by sending out a  sound wave at a specific frequency and snooping for that sound wave to bounce back. By recording the passed time between the sound wave being produced and the sound wave bouncing back, it is possible to calculate the distance between the sonar sensor & the object Distance = Time x speed of Sound divided by 2.

In sea ships similarly used these type of Sonars used for determining the depth of the ocean.

How to use the ultrasonic sensors to measure the distance in electrical engineering and automation


 Arduino UNO At Mega 328

An Arduino main programmable controlling circuit which controls the outputs and inputs according to user desired technics

This circuit has reduced a large other many circuit connection replace by user programming codes to upload Or simply said that “All in one” circuit.

Following is an Arduino board showing its different parts with its names.

1) Arduino UNO At mega 328. Distance measurement using ultrasonic sensors. Ultrasonic Sensor

An ultrasonic sensor commonly 4 pins used but some sensors have 5th pin also which used for LCD display showing result having the name “OUT”.

Task: Measuring a distance using the HC-SR04 ultrasonic sensor and display the result on the serial monitor. Required equipment: Microcontroller board/cables/ Breadboard / HC-SR04 ultrasonic sensor how does the ultrasonic sensor work?

The sensor has four pins.

  1. 5V (+)             b) GND (-)           c) Echo                       d) Trigger

The contacts 5V and GND are meant for the power supply. Using the “trigger” pin the sensor receives a short signal (5V) from the microcontroller board to create a sound wave. As soon as the sound wave hits a wall or other objects, it will be reflected and comes back to the ultrasonic sensor.

When the sensor detects this returned sound wave, the sensor will send a signal to the Arduino microcontroller through the “echo” pin. The Arduino-board is used here to quantify and measures the time between the transmission and the return of the sound wave and converts this time into a distance.

Ultrasonic sensors. distance measurement

Jumper wires & Brad Board

Jumper wires are used for connection pathway signals and current carrying (materials) lines.

Jumper wires and distance measurement using the ultrasonic distance

A small brad board use for connecting board without soldering plug in and plug out make easier by its one-sided holes. The Row-wise holes are commonly used for power supply  VCC+ and GND. The center narrow hollow path is used for IC installation.

The Row wise holes are commonly used for power supply VCC+ and GND


  • Install the ultra-sonic sensor in brad board tightly
  • The take a wire from “Vcc” to Arduino UNO to +5VPin.
  • Take the 2nd wire from “Trig” to Arduino any digital pin.
  • “Echo” pin must connect to any “PWM” pin of UNO.
  • Provide “GND” ground connection from any pin of UNO have GND naming.
  • The power supply ( +Ive & GND ) to UNO through its USB cable from laptop or PC and the UNO provide supply to the ultrasonic sensor.

How To Measure Distance Using Ultrasonic Distance Meter Distance measurement

Coding Explanation

We want to measure the distance to check between 0 and 200 cm. for this purpose we make a code to explain it.

arduino coding and How To Measure Distance Using Ultrasonic Distance Meter

Global declaration

  1. 1st of all we de define trig pin to pin 11 and echo pin to 12
  2. For output limitation blinking we add a LED connected to pin 13 and second to 10.

Setup of Inputs & Outputs:-

  1. Trigger pin must have for output to Arduino
  2. Echo pin must generate echo inputs to rappel any object.
  3. Both leads we want to output show blink on a specific distance.

arduino coding How To Measure Distance Using Ultrasonic Distance Meter

Looping of Commands;-

  1. Insert the long duration and distance  library
  2. Stop the trig pin for 2u seconds
  3. Make a high trigger pin for 10u seconds.
  4. While low the trig pin oppositely echo pin high 10us & measure the time (duration ) in both  travels
  5. We know that Distance=time/velocity But here by rippling the time is duel elapsed that’s why dived Total time by 2.
  6. Here is the constant velocity of ultrasound waves in small sensors are 29.1m/s.

arduino coding


  1. We use “if ” condition for more than one possibilities
  2. If the distance is less than 4cm then make led ON and led2 must be OFF.
  3. Else if the condition is opposite mean distance larger than 4cm then led make OFF and led2 make high.
  4. If the distance is less or equal to 0cm and greater or equal to 200cm   then  aware me that “Out of range “
  5. I want distance numerical value showing by serial monitor
  6. And print length in “cm”
  7. After every half of the second show me result on the serial monitor
  8. Remember that (1second=1000u seconds).