Problem SR04 always returning "0 cm"

Hi All,

Can anyone see why the below code might always return a distance reading of “0 cm” i have buzzed out all the wiring and triple checked connections to the SR04. Bit stumped other than the SR04 is faulty.

*/
#include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(11,10,9,2,3,4,5);  //Create Liquid Crystal Object called LCD

#define trigPin 13 //Sensor Echo pin connected to Arduino pin 13
#define echoPin 12 //Sensor Trip pin connected to Arduino pin 12

//Simple program just for testing the HC-SR04 Ultrasonic Sensor with LCD dispaly 
//URL:

void setup() 
{  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
  LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
  LCD.print("Target Distance:");  //Print Message on First Row
  Serial.begin(9600);
  
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  LCD.setCursor(0,1);
  LCD.print("sonar-low");
  Serial.println("");
  Serial.println("sonar-low");
  delayMicroseconds(2);
  LCD.setCursor(0,1);  
  LCD.print("sonar-high");
  Serial.println("sonar-high");
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  LCD.setCursor(0,1);  
  LCD.print("sonar-low");
  Serial.println("sonar-low");
  //delayMicroseconds(10000000);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  LCD.setCursor(0,1);  //Set cursor to first column of second row
  Serial.print("");
  LCD.println("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  LCD.print(distance); //Print measured distance
  Serial.print(distance);
  LCD.print(" cm");  //Print your units.
  Serial.print(" cm");
  delay(250); //pause to let things settle
}

Not sure if i should this or not, i put an analogue multimeter on GND and the Arduino trigger pin (13) set the multimeter to a scale of 10V DC thinking i might see the needle fluctuate, no movement. I also changed the trigger pn to pin (11) but still nothing. I see a good 5v on the SR04.

What happens if you declare distance as a float, rather than a long?

 digitalWrite(trigPin, LOW);
  LCD.setCursor(0,1);  
  LCD.print("sonar-low");
  Serial.println("sonar-low");
  //delayMicroseconds(10000000);
  duration = pulseIn(echoPin, HIGH);

Not sure, but I wouldn't waste time printing things between sending the ping and looking for the echo. You might be missing it while you're off printing.

Spot on Delta_G

I removed the Print command and hey presto

Thanks