ultrasonic sensor HC-SR04 return 0cm once a second

Hi All,

I'm trying to use ultrasonic sensor HC-SR04 with arduino uno.
I have been using a sensor I bought from dx.com about two years I ago without any problem.

Now, I'm trying to reproduce my project with new sensors. Then I found that every single one of them I ordered from different vendors are different. They don't work properly.

I ordered from dx.com again and got a sensor that looks good nice soldering and .... But it doesn't work properly. Once a second it won't detect a signal and return 0 cm. (I'm using New Ping library.) Whereas, with my two years old problem are working fine with the same code and board. Please have a look at the result from the attached file. I have almost exactly the same problem with new sensors I bought recently.

Is there any work around with the problem? Could you please suggest the vendors that always ship good quality sensor?

Here is my code.

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

float start;
float time;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  start = millis();
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(30);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  time = (millis() - start)/1000.0;
  Serial.print(time);
  Serial.print(", ");
  Serial.println(uS*10 / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
//  Serial.println("cm");
}

Thank you :smiley:

Depending on your application you could simply discard the 0 readings.. Doesn't really solve the mystery of why the new ones dont work like the old ones though. It might be that the new ones use different hardware than the old ones.

Does it make any difference if you change the delay between pings or the MAX_DISTANCE?

tawinan:
Hi All,

I'm trying to use ultrasonic sensor HC-SR04 with arduino uno.
I have been using a sensor I bought from dx.com about two years I ago without any problem.

Now, I'm trying to reproduce my project with new sensors. Then I found that every single one of them I ordered from different vendors are different. They don't work properly.

I ordered from dx.com again and got a sensor that looks good nice soldering and .... But it doesn't work properly. Once a second it won't detect a signal and return 0 cm. (I'm using New Ping library.) Whereas, with my two years old problem are working fine with the same code and board. Please have a look at the result from the attached file. I have almost exactly the same problem with new sensors I bought recently.

Is there any work around with the problem? Could you please suggest the vendors that always ship good quality sensor?

Here is my code.

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN    11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

float start;
float time;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  start = millis();
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(30);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  time = (millis() - start)/1000.0;
  Serial.print(time);
  Serial.print(", ");
  Serial.println(uS*10 / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
//  Serial.println("cm");
}




Thank you :D

The market was flooded with clones of these sensors and there's been all kinds of quality problems. Maybe try getting a SRF05 sensor instead. It seems they're not as popular so there hasn't been so many defective clones made.

Or, you could use the NewPing ping_median() method. That will ignore 0cm pings automatically.

Tim

Thank you Tim and rw950431 for answering my question.

I really like the SR04, too bad even the vendors don't understand the problem we are facing.
I will try to switch to SR05.

Thank you again.