LESSON 20: Arduino LCD Project for Measuring Distance with Ultrasonic Sensor

In LESSON 18 you learned how to use an ultrasonic sensor to measure distance, and in LESSON 19 you learned how to connect an LCD to the arduino. In this lesson we will combine what you have learned to create a circuit for measuring distance, and displaying results on an LCD display.

Arduino LCD
This circuit displays the distance you measure on a cool LCD.

You can use the schematic below to connect the circuit. If you did LESSON 19,  you should already have the LCD hooked up. For more info on connecting to the LCD, and how it works, review LESSON 19. This schematic is for the LCD in the Sparkfun Inventor Kit, or similar LCD. If you have a different LCD, you will have to determine the proper connections. There are some helps in LESSON 19. If you need the ultrasonic sensor, you can pick one up HERE.

LCD Arduino
This diagram shows how to connect my LCD to the Arduino.

Be very careful connecting the circuit. Check your work, and it helps to work with a Buddy. Have one person looking at the schematic, and one looking at the circuit. Sometimes it is easier to get it right working in pairs.

Now the objective of this project is to measure distance using the ultrasonic sensor, and then display that value on the LCD display. You should have the skills you need from the earlier lessons. Try and do this project on your own, but if you get stuck, you can look at my code below. As always, don’t copy and paste my code, but it should be used as a guide to help you write yours if you get stuck.

Your assignment is to get this project going, and show that you can measure distance and then display it on the LCD. After showing me your work, then you need to take the project in your own unique direction. What can you make based on what you have learned in the last few lessons. You should use some combination of Ultrasonic Sensor, LCD, and Servo to make a project or product or your own invention. You have the technical skills, you have the equipment, now go be creative!

60 thoughts on “LESSON 20: Arduino LCD Project for Measuring Distance with Ultrasonic Sensor”

  1. Hi,

    this is one of the best arduino series tutorials I have seen on the internet. Thanks for that.

    So based on this I am creating some project, actually I created it but I am trying to resolve some problems, but I am not familiar with some characteristics. For example, I need to test distance for object with just 2 cm height from the bottom (floor), but if it is two meters from sensor it does not show anything, like there is no any item. I need to test for example when someone steps in front of the sensor where his feet are, but not higher than that (because feet and knees, or back of the knees are not int the same line ). As I tested longer range from sensor item needs to be higher, but I need distance just 2 cm above the floor in range less then 3 m.

    And second problem is when item is on longer range from sensor it is not precise, for example, when I check distance in range of 20 cm, precision varies from 0.5 cm to 1 cm, but on longer range it is from 2 cm to 6 cm, so is there a way to create more precise measure.

    Tnx again for this tutorials.

    Best regards,
    Nenad

  2. I am likely to get detention for saying this …. but I just built one of these units with copy and paste! just correcting for UK “mm” not old English feet and inches! Its just amazing performance, I purchased a cheap sensor off ebay for £ 0.99p approx. $1.50. Having tested it out in the house, its best results are off surfaces like tile and glass, where it is giving me +/- 30mm at 3000mm. At the other end of the scale up close, it is much better performance. The error is the range of 1/2 mm measuring under 300mm lengths. The sensor however cannot work under about 20mm as the ping from the sender needs to reflect back into the receiver. Why was’nt school this much fun when I was there? we only had batteries and bulbs…

  3. The way you teach the lessons is fantastic. I really enjoy how can I learn the content as easy as possible :), but also have a question, how is writing (uploading) programs procedure in Arduino micro controller? I mean when I upload for example this program into Audrino and then decide to upload another program, does it keep my previous one or overwrite new program on previous one?
    Thanks a lot

  4. Hi,
    I baught a HC-SR04 ultrasonic sensor and connected to arduino to measure distance. It works perfect but with a problem. If it measures out of range distance, the echo pin remains high forever. If I switch off/on the power and put a obstacle, it works again but again if distance exceeds the limits, echo pin freezes to high state and does not goes low. It should go low after 1 second as time out but it is not going low.

    1. I’m try to code to enable arduino to find the thickness of an object. So i need to store the first value measured and minus with the new value measured but i have problems to store it (is it possible?)

    2. This is exactly the same sensor that I am using and my sensor worked perfectly. I expect there is something in your coding. Go back to lesson 18 and try out the sensor there.

  5. thank you one more time for great teaching! I’ve just built the circuit but have a problem with my lcd display as It shows absolutely nothing after uploading the program, the only thing it shows is first row high contrast which changes with potentiometer. what can I do in this situation?

    1. Check your wiring very carefully. It sounds like their might be an error on connection. Make sure you have the same pinout as the lcd I am using.
      Check spec sheet on your lcd

  6. Absolutely great tutorial site. Starting one month ago, reading some Arduino books and watching your videos I have built a device to determine the scale speed of model trains and a device to prevent model trains over running a buffer stop. There is a lot of electronics available commercially for model trains although it is much more fun to code, design and built your purpose specific devices with Arduino. Many, many thanks again.

  7. Thank you very much Mr.Paul believe me that your tutoriels don’t let me sleep the night ,i’m so impressed by the method that you use to simplify the information .
    Thank you again.
    Mohamed.

  8. Hello, sir
    I want to ask, I want to make the project a parking crossbar otormatis use arduino and ultrasonic sensors, please help me program the required code, which prinsif the appliance is working within a maximum of 100 cm. if a car passes from the front of the sensor bar will be open.
    tanks

  9. i think its better if you don’t write
    LCD.print(“Target Distance:”); in setup function.

    but you write
    LCD.clear();
    LCD.setCursor(0,0);
    LCD.print(“Target Distance:”);
    LCD.setCursor(0,1);
    LCD.print(targetDistance);
    LCD.print(” inches”);
    in loop function..

    1. @godit, you suggestion makes it even ‘poor’ coding. It’s because the top line of LCD is NOT changing at all in the loop. If your suggestion is followed than the same info get printed on the top line of LCD each time of the loop activity…hence wasting so many unnecessary clock cycles of resouces. So it is better ‘as is’….
      The only thing in the code I can see is that Serial is initialized : serial.begin(9600); but never used anywhere after that but it doesn’t affect very much in resources.

    1. Oops . . . that is left over code from another line. It should not be there. It was when we were controlling the servo position, which is not used in this project.

    1. @Silver line, just use your common sense …since on the top section of the code it is given as: float speedOfSound=775.6 miles per hour. You can make your own for km/hour etc as is used in your country. Down-under they use KMeters so you can get help from Ozie sites.
      In USA we only use miles and inches for distance so in our mind it is that way.

  10. hello,
    I want this to make as my own project, there is a question in my mind that can I add additional feature like indicator i.e. sound of “beep” in this project just for small range like 5cm?,
    if yes then how will I connect this and what additional code will be written?
    And if you have any link please share.
    Thanks 🙂

  11. Questions
    1.) servo is being int?
    int servoControlPin=6; //Servo control line is connected to pin 6 –
    2.) there is a LOW voltage delay after the ultrasonic has been sent a pulse (comment says “high state” but is actually LOW):
    digitalWrite(trigPin, LOW); //ping has now been sent
    delayMicroseconds(10); //Delay in high state
    Does this delay subtract from the ping time? in mean to say wont this cause an error of -10mS in the measurement? or does the ECHO pin go HIGH when ping is sent automatically?
    3.)I am trying to combine all the unit conversions into one or two constants but when i devide by such a large number i end up with zero. Is there a limit to the floating point positions?

    Thanx for everything, i love the videos, and am building right along with you, but making the code my own

    1. I think i figured out my problem on question 3:
      i was missing (and so are you in your example above) the decimal point at the end of the conversion constants.

  12. can you help me to communicated between arduino uno and URM06-rs485 ultrasonic sensor. i dont know how to use it

  13. Hi
    I want to Mix 2 Programs
    The First Program Solenoid valve
    The Second Program MQ4 Sensor
    i want when the MQ4 sensor Intuit Methane gas then the Solenoid valve open

    what should i do ??

  14. kindly help me.I have followed your instructions and have done what u have said in your lessons.i am also using 16*2 lcd and POT. But i am not getting any display on my lcd. i can adjust the brightness but still no display

  15. Is it possible to measure the speed of a rolling ball toward and from the sensor using the millis() function. Please let me know sir if this is a good implementation and practice idea

  16. I would like just to say how good your lessons are and how great is your way to teach people. I’m 58 and so happy that I met you in this website.
    My congratulation for your great effort.

  17. Sir, I highly appreciate your work. This project helped me so much and it worked perfectly. As you said to do something more interesting to modify it, I want to use this sensor as a visitor counter and I am not getting the coding part for this. Please help me in this.

  18. Sir,
    By using this project.
    Q-1) How to detect a motion of object of speed 60km/hr -80km/hr?
    Q-2)which ARDUINO board(Single) is best for to use 7 sensor?

    1. The ultrasonic sensor would not be suitable for measuring speed. Its range and sensitivity would not be adequat for that.

  19. I am a high school teacher with minimal arduino experience and none with the distance measuring sensor. I had an idea to put different balls in a pvc pipe or graduated cylinder and use the distance sensor to collect the distance measurements as the ball bounces inside the tube. Is this something that could work?

    1. Maybe. You should try it and see. Good chance it would work. Only potential issue would be if you got some sort of reflections off the inside of the tube. A good idea, worth trying in my mind.

      1. Thank you so much for your prompt reply! That is great news that you think it could work. I will dive in and see what I can do with it.

  20. Hello Sir,
    Very great lesson about ultrasonic sensor and LCD.
    I am currently doing my school work about measuring angle (respect to a reference line through ultrasonic sensor) and distance with Arduino UNO, LCD, ultrasonic sensor (HC-SR04), resistor and rotatory potentiometer onto of which the ultrasonic module will be mounted. I can rotates the sensor. I want to ask does the potentiometer measure the angle when it is rotated or the ultrasonic sensor measure the angle by some equation. Also, what is the code about measure angles, please?
    Thanks a lot.

      1. Hello admin
        It is not the text that flickers it is the backlight that turns on and off. I can make it flicker so fast that it is barely seen by changing the last delay to 5, but can not get totally rid off the flickering backlight. Any other suggestions?
        Best regards
        Stig!

Leave a Reply

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