Help with HC-SR04 - Getting weird values

Hello guys, I'm new around here :slight_smile:
I'm trying to do something really simple. Turn on a led if the distance is less than 10cm.
Everything seemed to work at first, but I noticed if the distance exeeds about 3000cm, the NEXT time I do a distance measurement, the value is ALWAYS below 10cm (which turns on my led...). I've been debugging for a while, and even trying to do the same thing with a Raspberry Pi, but it happens there too. I've tried multiple SR04's. I'm at a point where I can do nothing but ask for help. Does anyone know what is going on here?

Here's how the measurement goes if i point the sensor right up in the sky:

8cm
16cm
16cm
15cm
16cm
184cm
3306cm
8cm
3299cm
8cm
3282cm
8cm
3307cm
8cm
3320cm
8cm

I have no idea why every second measurement returns 8cm if the previous one exeeded 3000cm o.O

Here's the code:

const int trigPin = 7;
const int echoPin = 6;
const int led1 = 13;

void setup() {
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  int dist = getDistance("cm");
  if(dist < 10) {
    digitalWrite(led1, HIGH);
  } else {
    digitalWrite(led1, LOW);
  }
  delay(1000);
}

long getDistance(String measurement){
  long duration;
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  if(measurement.equals("cm")) {
    long cm;
    cm = microsecondsToCentimeters(duration);
    Serial.print(cm);
    Serial.println("cm");
    return cm;
  } else {
    long inches;
    inches = microsecondsToInches(duration);
    Serial.print(inches);
    Serial.println("in");
    return inches;
  }
}

long microsecondsToInches(long microseconds){
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds){
  return microseconds / 29 / 2;
}

Any help appreciated!

Others on the comment thread for this article http://www.instructables.com/id/Easy-ultrasonic-4-pin-sensor-monitoring-hc-sr04/ have the same issues.

Those sensors are only rated to 400cm so its unlikely that the 3000cm ranges are real values. Perhaps you can use the 3 argument version of pulseIn() and set the timeout value to a lower value or increase the delay() on the main loop to eliminate the phantom readings.

For a better understanding of what is happening you may wish to search "radar second time around echo".

Thanks for your reply. I actually tried setting a timeout value, but the next reading is still wrong. The only partially working work-around I have so far is setting a timeout, and if a timeout occurs, ignore the next reading. I came across this thread after doing some more research Ultrasonic distance sensor HC-SR04 lack of timeout problem - Sensors - Arduino Forum
Not sure if that's exactly the same issue as I have, but it might be related to the SR04's lack of a timeout function. Oh well, what should you expect for 2$...

If anyone can suggest a better sensor with a timeout function, which is not too expensive, let me know :slight_smile:

Flowlance:
Thanks for your reply. I actually tried setting a timeout value, but the next reading is still wrong. The only partially working work-around I have so far is setting a timeout, and if a timeout occurs, ignore the next reading. I came across this thread after doing some more research Ultrasonic distance sensor HC-SR04 lack of timeout problem - Sensors - Arduino Forum
Not sure if that's exactly the same issue as I have, but it might be related to the SR04's lack of a timeout function. Oh well, what should you expect for 2$...

If anyone can suggest a better sensor with a timeout function, which is not too expensive, let me know :slight_smile:

Its normal actually, some time the wave travel for a long time and then come back to the sensor.
Sound can be effected by material and surface angle too.

You should :
1/ Use NEW PING library which is very good and compatible.
2/ If you dont, try read 5-10 values, find which value super big (3000cm just like your example), ignore it, find average value.
3/ remember to reset Echo pin everytime its value=0.
4/ Try HC-SR05, less error, longer distance, but Im sure even cheap SR04 will be fine.
5/ Google and find the Author of New Ping, he has many posts here on this forum which will help you out.

Minh.