Measuring Distance Using Ultrasonic Sensor And Arduino

In this project we are going to measure the distance using an Ultrasonic sensor and then print it on LCD.


  1. Arduino
  2. 1602 LCD Screen (16 x 2 LCD)
  3. Jumper wires
  4. Arduino program in computer
  5. 220ῼ resistor
  6. 10k potentiometer
  7. Breadboard
  8. Ultrasonic sensor HC04



The circuit:

  1. LCD RS pin to digital pin 12
  2. LCD Enable pin to digital pin 11
  3. LCD D4 pin to digital pin 5
  4. LCD D5 pin to digital pin 4
  5. LCD D6 pin to digital pin 3
  6. LCD D7 pin to digital pin 2
  7. LCD R/W pin to ground
  8. 220 ῼ resistor: lcd pin 15 and ground
  9. Potentiometer wiper to LCD VO pin (pin 3) and ends to +5v and ground of arduino kit
  10. LCD pin 16 to ground
  11. Trig pin of the sensor to 9
  12. Echo pin of the sensor to 10
  13. Gnd of the sensor to ground pin of arduino.
  14. VCC of the sensor to 5V pin



#include <LiquidCrystal.h>

  #define trigPin 9

#define echoPin 8

float duration;

float Inches;

float Cm;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

pinMode(trigPin,OUTPUT);     //sets trigpin as output to send ultrasound

pinMode(echoPin,INPUT);      //sets echopin as input to receive ultrasound

 Serial.begin(9600);                  //sets 9600 as baud rate

  lcd.begin(16, 2);                      //enable 16 columns and 2 rows in the lcd


void loop()


  lcd.setCursor(0,1);                      //sets cursor at 1st row and 2nd coulumn

  digitalWrite(trigPin, LOW);   //sets trigpin to LOW so that it do not emits ultrasound

                                                          //  initially


  digitalWrite(trigPin, HIGH);                  //sets trigpin to HIGH state to emit ultrasound


  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);                        //reads the travel time of the pulse

  Inches = microsecondsToInches(duration);         //function

  Cm = microsecondsToCentimeters(duration);     //function

  lcd.setCursor(0,0);                                                        //sets cursor to initial position










long microsecondsToInches(long microseconds)


return microseconds / 74 / 2;   /* sounds travels at the speed of 1130 feet per second. There are 74.16 microseconds per inch. The time that will  be  received is of full journey of ultrasound i.e. forward and backward. So to calculate the distance we have to divide total time by number of microseconds per inch i.e. 74. We have to further divide it by 2 in order to calculate the distance of   only forward path and not the total distance*/


long microsecondsToCentimeters(long microseconds)


 return microseconds / 29/ 2;  /*Speed of sound is 340m/s. So it takes 29 microseconds to  travel 1 centimeter length of the path.All the working  is same as explained in calculation of inches*/
















When the trigpin of Ultrasonic sensor is HIGH for 10 microseconds, it emits an ultrasound which travels through air at the speed of 340m/s. If an object is placed in its path than it will be reflected back to the sensor. Echopin receive the ultrasound that is reflected by the object and thus calculate the time of return using code.

NOTE: code is explained line by line by using extensive comments.

Leave a Comment