Ultrasonic Sensor Distance Measurement With HC-SR04 & LCD

Previous Tutorial Previous Tutorial Tutorial 26 Next Tutorial Next Tutorial
Ultrasonic Distance Meter With HC-SR04 & LCD
Introductory Level ★☆☆☆☆


In this tutorial, we’ll create a distance meter using the ultrasonic sensor HC-SR04 with an Alphanumeric 16×2 LCD module. In a previous tutorial, we’ve discussed how HC-SR04 Ultrasonic sensor works and how to interface it with a microcontroller. That’s why it’s recommended to give it a read, in case you haven’t yet. For this tutorial, we’ll combine both labs of the HC-SR04 with the LCD. And introduce a slightly more accurate way to measure the echo pulse from the ultrasonic sensor. Without any further introduction, let’s get started!


  Components Needed For This Tutorial  

Qty. Component Name Buy On Amazon.com
1 PIC16F877A

or PIC18F2550



1 BreadBoard Add
1 Alphanumeric LCD 16×2 Add
1 Ultrasonic Sensor HC-SR04 Add
1 Jumper Wires Pack Add    Add
1 LM7805 Voltage Regulator (5v) Add
1 8MHz Crystal Oscillator Add
1 PICkit2 or 3 Programmer Add


 Reading HC-SR04 Ultrasonic Sensor’s Output 


To trigger the ultrasonic sensor, you’ll have to send a trigger pulse from your microcontroller using any io pin. The trigger pulse is a High-Pulse with a width of 10μs. Then, the ultrasonic sensor finds the distance between it and any object/obstacle confronting it. This distance information is sent back by the ultrasonic sensor to your main microcontroller as a short-pulse echo signal. 

Ultrasonic Distance Meter Measurement1

Our target is to measure the pulse width of the echo signal to know how long did it take the ultrasonic waves to travel between the sensor and any confronting object. We’ll use the CCPx module in capture mode to capture the time t1 when the echo pin goes high and reverse the capture edge triggering to capture the time when the echo pin goes low t2. Therefore, the echo pulse width is Tp = t2 – t1

Required Configurations

  • Configure CCP1 Module To Capture On Every Rising Edge
  • Enable CCP1 Interrupt
  • Configure TMR1 To Handle Max. & Min. (Tp)
  • Enable TMR1 Overflow Interrupt


  1. Configure CCP1 To Capture Every Rising Edge
  2. Enable CCP1 Interrupt
  3. Configure TMR1 & Enable Interrupt
  4. When CCP1 Capture Interrupt Occurs -> Save t1 & Re-configure CCP1 To Capture On Falling Edge
  5. When CCP1 Capture Interrupt Occurs -> Save t2 & Get Tp = t2 – t1
  6. Re-configure CCP1 To Capture Every Rising Edge Once Again .. (Keep Repeating Steps 4 to 6)

Code Listing


 Converting Echo Pulse Width To Distance 


Ultrasonic Distance Meter Measurement HC-SR04

After measuring the pulse width time of the echo signal being sent by the ultrasonic sensor, we need to extract the distance information from it. In fact, the pulse width (Tp) of the echo signal represents the total trip time it took the sound waves to travel from the sensor to the confronting object back & forth.

Which means, the time it takes the ultrasound waves to travel from the sensor to the object is only half of Tp. Therefore, td = Tp/2. And provided that the sound speed in air is a constant of 340[m/s], we can now get the distance between the sensor and the confronting object from the formula shown below

Distance = td x 340[m/s]

Code Listing


   Printing Distance On LCD   


We’ve developed a simple device driver for the Alphanumeric 16×2 LCD in a previous tutorial. So, we’ll just include our library in this project and make changes to the io pin maps if you wish.

The float Distance variable should be converted to a string (array of characters) before being passed to the LCD printing routine. For this job, we’ll use the sprintf function from stdio.h library. And we’ll settle for 3 digits after the decimal point, and you can surely set it to whatever you want. Below is a code snipped indicating the LCD printing functionality.

Code Listing

Ultrasonic Sensor Distance Meter And LCD

   Concluding Remarks   


LCD Connection Diagram


Results In Simulation

Ultrasonic Sensor Distance Meter Measurement

Results In Practice

Full Code Listing

Download All Project Files

Download Project Files



Did You Find This Helpful? Why Not Share it With Your Network Or On FaceBook Related Groups! Maybe Some Others Would Like To Read Such A Content .. Good Luck, everybody!


Khaled M.



Previous Tutorial Previous Tutorial Tutorial 26 Next Tutorial Next Tutorial
Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

4 thoughts on “Ultrasonic Sensor Distance Measurement With HC-SR04 & LCD”

  1. Hello, Mr.Khaled Magdy: My name is sath, I want to thank you for every tutorial that you helped all of us with. I learned a lot from you in PIC programming. so thank you, thank you…

    • Greetings Sath ^^
      I’m really soo happy to read your comment. Thanks so much for your kind words and this great feedback!

  2. You are more than welcome. I watched every vedio you have and all so good and helpfull. I also, want to ask you about ASM, or assemply language program espicialy for the ultrasonic, why didn’t you do these vedioes, it will be very uniqe. No one ever ha e it on Youtube. Can you please look into it. Thanks again

  3. I could not understand why we have defined a variable called TMR1_C and why we have used it in the

    formula that calculates Echo Pulse Width (tp).Can you please explain.Thanks!


Leave a Comment