Distance measurement arduino

Distance Measure Using Ultrasonic Sensor

Share this

Distance measuring instrument design and development using Arduino UNO and Ultrasonic Sensor SR04

Distance measurement using ultrasonic sensor

Ultrasonic Sensor interfacing with Arduino

Ultrasonic sensors not only offer distance measuring utility without any physical contact but enable us to measure with no noise and light, unlike laser-based distance measuring instruments. Furthermore, these instruments are cheap and more reliable even in daylight where laser-based instruments often decrease their efficiency. Distance measuring instruments have been in use for centuries and improvements have been made in their designs with the passage of time. Today, Distance measuring instruments such as foot ruler and inches tape are obsolete and digital instruments are used for such purposes at a larger scale. High precision and more convenience of measuring any distance from one point have made the process easy. Such instruments are widely used at construction sites and fluid level monitoring. In containers and sites where fluid level monitoring needs to be precise and remotely controlled, ultrasonic sensors based distance measuring instruments provide ease. Since the design is based on embedded systems and the whole process is controlled by a microcontroller, many features can be added to it. For example, remotely transmission of fluid level and accordingly ON/OFF feature for a digital fluid switch.


Arduino UNO microcontroller has gained popularity for its vast library and easy programming. The digital and I/O pins with multiple timers has enabled us in designing and development of complex projects with ease. Arduino UNO board is compatible with various sensors and offers exceptional efficiency at low cost. The microcontroller is a free source design and one of the most demanding board all of the World.

Ultrasonic sensor working

Ultrasonic sensors also known as Sonar sensors have been in use for decades. They were once used for mapping of ships in sea and detection of broken/faulty parts in mechanical systems. The ultrasonic sensor works by transmitting ultrasonic waves and these waves strike with obstacles placed in front of the transmitter. The waves are reflected and hit on the receiver. The time taken by the waves from transmission to receiving with respect to the velocity of ultrasonic waves is used to measure the distance of obstacle present ahead.

The speed of sound is approximately 341 meters (1100 feet) per second in air. The ultrasonic distance sensor uses this information along with the time difference between sending and receiving the sound signal to determine the distance between object. It uses the following mathematical formula.

Distance = Time x Speed of Sound divided by 2

Time = the time between when an ultrasonic wave is transmitted and when it is received
this number is divided by 2 because the sound wave has to travel to the object and back.

Since the distance can’t be changed in physically in simulation, the simulation model of SR04 is connected with a potentiometer, and the distance for the sensor is varied using this potentiometer.

Ultrasonic Sensor Range

HC SR-04 Ultrasonic Distance Sensor Provides The Range between 2cm to 400 cm approx (1 inch to 13 foot). With Fully non contact .


ultrasonic distance sensor working

hc-sr04 ultrasonic sensor

Measurement of distance needs to be expressed in some way, and for embedded systems, the best way is to use an LCD. 16×2 HDD44780 driver based LCD offers efficient and smooth functioning at a cheap price. The 16 columns and 2 rows offer enough space to display results. The LCD is compatible with all models of Arduino boards which makes it suitable for this project too.

Components required

  • The components required for this project are simple and low cost.
  • Arduino UNO MCU
  • Ultrasonic sensor SR04
  • 16×2 LCD (HDD44780 driver based)
  • 6v battery or 5v USB power source (Mobile adapter/laptop)
  • Potentiometer 10k ohm
  • Jumper wires
  • Breadboard

Connection Diagram

arduino ultrasonic distance measurement


Proteus 8 based simulation model has been demonstrated here. This simulation enables designers and developers in verifying circuit’s functioning before proceeding towards hardware design. Directly developing hardware on PCB or veroboard can be stressful if the end product fails to run due to misplaced connections. Simulation helps in tracking mistakes and improving designs, you can always add more creativity to your design and test it on simulation. The simulation models used for Arduino UNO and Ultrasonic sensor in proteus are easily available on the internet. The only effort needed is to connect all components as described in the circuit diagram.

The working mechanism of this project is simple and easy to understand. The ultrasonic sensor has 4 pins out of which 2 pins are for power, and the remaining 2 pins are for the trigger and echo. The MCU transmits ultrasonic waves through the trigger pin and then reads the receiving time of that ultrasonic wave on echo pin. The velocity of the ultrasonic wave traveling is defined and accordingly the distance is measured.

It is essential to add a hex file of Arduino code in its simulation model. The hex file is in the form of binary instructions given to the MCU for processing.

LCD serves as a graphical interface for displaying results. The display brightness can be adjusted by supplying 0-5v on pin 3 which is unnecessary in case of simulation but works well for hardware design. Make sure the pins are carefully connected with Arduino as mentioned in the circuit diagram.


The use of 10k Preset is only in simulation for making a Object between sensor . Here the preset is works as object.

The use of a potentiometer with the Ultrasonic sensor enables the user to control the input distance for sensor and observe changes in results accordingly. The value of this potentiometer must be above 1K ohm.

Since the simulation model ensures proper functioning of the design, if your simulation works correctly, you can proceed towards the development of hardware design. However in case if your simulation is not working correctly, you need to troubleshoot your error.

If your LCD is displaying distance and it varies but the distance is incorrect, you might be directing your Ultrasonic sensor in the wrong manner, make sure the path is clear between target obstacle and Ultrasonic sensor and the sensor is facing an obstacle. Alternatively, the connections of the ultrasonic sensor with Arduino might be wrong. If there is no display on the LCD, then you need to check code and connections of LCD with Arduino.  

Failing to connect all components as instructed in the circuit diagram, the simulation might not work or show unexpected behavior..


The coding has been compiled on Arduino IDE. The code starts with the LCD library. The pin connection for LCD and Arduino UNO are defined for proper communication. Then all components and pins used are defined.

The setup function is where the baud rate is defined, and the selection between Input/Output functioning of pins is made. The LCD size 16×2 is defined here, and initially few commands are used for clearing LCD..

The loop function contains the main body of instructions. It keeps iterating for as long as MCU runs. The whole mechanism of Ultrasonic based measurement is processed here using multiple commands. The distance is calculated after computations is displayed on the LCD using commands.

Ultrasonic sensor arduino code

#include <LiquidCrystal.h>;

LiquidCrystal lcd(10,9,5,4,3,2);

const int trigPin=13;  // defines the pin numbers
const int echoPin=11;
long duration;                // defines variables
int distanceCm, distanceInch;

void setup() {
  Serial.begin (9600);
  lcd.begin (16,2);     // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
  pinMode (trigPin, OUTPUT);
  pinMode (echoPin, INPUT);

void loop() {
digitalWrite(13,LOW);  // Clears the trigPin
digitalWrite(13,HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10); // Sets the trigPin on HIGH state for 10 micro seconds

duration = pulseIn(11, HIGH);   //To receive the reflected signal.

distanceCm= duration*0.0340/2;  // Calculating the distance
distanceInch = duration*0.0133/2;

lcd.setCursor(0,0);      // Sets the location where text written to the LCD will be displayed
lcd.print("Distance= "); // Prints string "Distance" on the LCD
lcd.print(distanceCm);   // Prints the distance value from the sensor
lcd.print(" cm    ");
lcd.print("Distance= ");
lcd.print(" inch    ");

Share this

1 thought on “Distance Measure Using Ultrasonic Sensor”

Leave a Comment

Your email address will not be published. Required fields are marked *