The Arduino distance meter that I made doesn't display the correct distance. It just displays "0.0 inches".

Here's the code I used:

#include <LiquidCrystal.h>

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

int pingPin = 7;
int inPin = 6;
long duration, inches, cm;
int indec, cmdec;
int inchconv = 147;
int cmconv = 59;
String s1, s2;

void setup() {
  lcd.begin(16, 2);
  pinMode(pingPin, OUTPUT);
  pinMode(inPin, INPUT);

void loop()
  digitalWrite(pingPin, LOW);
  digitalWrite(pingPin, HIGH);
  digitalWrite(pingPin, LOW);

  duration = pulseIn(inPin, HIGH);

  inches = microsecondsToInches(duration);
  indec = (duration - inches * inchconv) * 10 / inchconv;
  cm = microsecondsToCentimeters(duration);
  cmdec = (duration - cm * cmconv) * 10 / cmconv;
  s1 = String(inches) + "." + String(indec) + "in" + "     ";
  s2 = String(cm) + "." + String(cmdec) + "cm" + "     ";
  lcd.setCursor(0, 0);


long microsecondsToInches(long microseconds)
  return microseconds / inchconv;

long microsecondsToCentimeters(long microseconds)
  return microseconds / cmconv;

Here's the wiring diagram:

Fritzing diagram of Arduino distance meter

Might there be a problem in my code, or in my wiring?

  • Please provide more information. Like e.g. code and way you hooked it up.
    – Gerben
    Jan 1, 2016 at 13:50
  • I provided more informations Jan 1, 2016 at 14:14
  • 3
    Tip for debugging this kind of thing: Print the raw values first to see if you are getting a valid ping, then if you are print each stage of the calculation until you find the failure.
    – Majenko
    Jan 1, 2016 at 14:22
  • please explain me it properly Jan 1, 2016 at 14:27
  • Question: Does the variable duration contain a meaningful value?
    – Majenko
    Jan 1, 2016 at 15:45

3 Answers 3


To solve a problem like this you need to break it down into smaller pieces so that you can build up some confidence about what is working and what is not. When you share a problem like this as a question it helps if you can give us some idea of your experience level and of what you've already done to debug the problem – that helps with avoiding telling you what you already know and makes it easier to focus in on where the problem might be.

From what you've said it appears that you have things working well enough that you get a display on the LCD but it does not represent a meaningful distance.

Looking at your schematic the first thing that comes to my mind is that you have the ultrasonic sensor at the opposite end of the breadboard from the LCD. Many breadboards split their power rails at the middle. So start by confirming that you have power to the sensor.

After that, double-check the rest of your wiring.

The next step for me would be to confirm that I understood the sensor and to try and prove that it was working as I expected. Look for some sample code that is known to work (often the site you bought the sensor from will have links to beginning tutorials or code samples). Try that to see if the sensor works with the sample code.

If that doesn't work then check to see what you can do to prove the sensor is good (or bad). You might also contact the vendor to see if they can give you advice on proving that the sensor works.

Basically, break the problem down into "bite sized" chunks and work through it one step at a time, doing the simple stuff first. Try not to assume anything…


Maybe you should use the New Ping library and check if the sensor works without LCD:



I think you should first check it in simulation and for that install Proteus software and get this Ultrasonic Sensor Library for Proteus and then test your code in simulation.

Once you confirmed that your code is working fine then you can come to the hardware debugging but I think its more probably because of programming issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.