HC-SR04 always returns zero

Thanks for the response John! I am really wondering if the sensor is bad.. This is my first time using one

Susan

Ok, another question about this... How do I know for sure it's the sensor?

Which Arduino are you using? Have you checked the voltage of the supply to the sensor? The HC-SR04 requires a solid 5V to work reliably, in my experience.

Not sure how to test the voltage... now I have a second sensor with the same results ... any other ideas?

I have the UNO 3,

Ok .. I finally figured it out ... I followed this post http://samwedge.co.uk/2012/08/29/ultrasonic-sensor-takes-too-long-when-no-objects-are-within-range/

and changed my code like he suggests .. then suddenly the sensor works

Hello Susan,

I found your post from notifications that I get when people click links to my website. I am glad that my ramblings helped!

Since it was the timeout that was the issue, can I suggest that you use the NewPing library that is available from the Arduino website? This takes a third parameter as an input, which is the maximum distance in centimetres. This is easier than modifying the library that you are using to change the timeout to PulseIn, which is given in microseconds. Unfortunately, I didn't know this when I wrote that page. I need to update it.

Just a suggestion, but since you already have it working then you probably don't want/need to change it again!!

Best of luck,

Sam

the solution to the sensor being stuck at zero is in this link. its the 2. post, by docdoc. You will need the NewPing library which is far better.

A working code:

#include <NewPing.h>

#define TRIGGER_PIN 12

#define ECHO_PIN 11

#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {

Serial.begin(9600);

}

void loop() {

delay(50);

unsigned int uS = sonar.ping();

pinMode(ECHO_PIN,OUTPUT);

digitalWrite(ECHO_PIN,LOW);

pinMode(ECHO_PIN,INPUT);

Serial.print("Ping: ");

Serial.print(uS / US_ROUNDTRIP_CM);

Serial.println("cm");

}

link: Ultrasonic distance sensor HC-SR04 lack of timeout problem - Sensors - Arduino Forum

NewPing link: Arduino Playground - NewPing Library

Try connecting your VCC of the sensor to 3V3 instead of 5V. This might sound odd (or not), but I tried it and it worked! I think 3V is enough,actually. And if it works, let me know.Also, please make sure your code's right :wink:

1 Like

Hello, I followed the code in post #10. What do I need to change in order to display the distance in higher accuracy? As far as I know, this sensor has higher resolution than 1cm. Changing

unsigned int uS = sonar.ping();

to

double uS = sonar.ping();

does not work.

Same here, I'm using the NewPing library, and the sensors are not working(getting constant 0s).
Tried connecting it to 3V3, but that makes the sensor give off garbage values(1149cm).

What I observed is that I have to snap my fingers once, to get the sensor started; and when there is a sudden change in distance from the sensor, the readings go back to 0.

I've checked all my connections and all the wires, those don't seem to be the problem.

Does anyone else have a similar problem?

I'm having this problem as well.

I have Vcc from HCSR04 to Arduino GND, then I have trigPin set to digital pin 12, and echoPin to digital pin 11.

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.

int ledPins[] = {5,6,7,10};

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

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

void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}

Note I have changed the delay on mine to (5000) but stillzero either way. Sorry I can't figure out how to snip my code.

WIN_20160429_21_23_50_Pro.jpg

Hi Ardu-weenie.

If really "I have Vcc from HCSR04 to Arduino GND" you won't get anything for sure!

The ultrasonic sensor HC-SR04 gave me a headache aswell.
I found on other fora many similar postings. Finally I created the code below that does not display a zero but the text "resetting the sensor" and then pushes the signal of the echo into LOW. This text will show in the monitor when the sensor is in this kind of timeout. It can occur a few times repeatedly but it never gets stuck in zero.
My best solution so far:

#include <NewPing.h>

#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 300

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
Serial.begin(9600);
}

void loop() {

delay(150);
int uS = sonar.ping();
if (uS==0)
{
Serial.println("MAX: resetting sensor");
pinMode(ECHO_PIN, OUTPUT);
delay(150);
digitalWrite(ECHO_PIN, LOW);
delay(150);
pinMode(ECHO_PIN, INPUT);
delay(150);
}
else
{
Serial.print(" ");
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.println("cm");
}
}

Using the NewPing library and @Dukeofboz sample code, our HC-SR04 work great - Thank you @Dukeofboz !

(We're using a Arduino Uno R3. We used 5V, not 3.3V .. that came up earlier in this thread.)

I had this same issue where I was constantly getting zeros. I tried all of the solutions in this forum but they did not resolve the problem. Thank you for helping. I found about 2 hours later, after reading a dozen forums, that the main issue that was causing my ultrasonic sensor to return zero while using the NewPing library is that pin 1 and other pins cause the ultrasonic sensor to not function properly. I should have check if the pins that I was using were general purpose but I accidently didn't. So if you are a newbie like me, make sure that the pins that you are designating as the trigger and the echo are the right pins to connect to. I would have liked to know this about 2 hours ago. :cold_sweat:

Hi, I also have this problem when doing project with HCSR04 sensor.
I tried different codes and different connections but all those did not work.
So I finally figure out it should be the problem with welding on the sensor itself. I tried to press my finger onto certain point of the sensor, then the sensor suddenly begins to produce result, but in a very fast way. ignoring all the delay command I put in the code.
Maybe you can tries to but more HCSR04 sensor and see the result.
Hope this can help you.

The problem is with New Ping doesn't work at all well for AJ-SR04M.
Try this its working fine

/*
AJ-SR04M distance sensor
with Arduino Nano
Echo to Arduino Echo pin 11
Arduino pin 12 Trig
*/
#define trigPin 12
#define echoPin 11
int cnt =0 ;
long rds[10];
void setup() {
Serial.begin (115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration, distance,distance1;
digitalWrite(trigPin, LOW); // Put trigger pin low
delayMicroseconds(2); // for two microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // for ten microseconds
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //Default time out I second
distance = (duration/2) / 29.1;
if (distance <= 100 ) // slow rate of change
{
if (cnt >9)
{
cnt =0;
}
rds[cnt] =distance/10; //
distance1= rds[1]+rds[2]+rds[3]+rds[4]+rds[5]+rds[6]+rds[7]+rds[8]+rds[9]+rds[0];
Serial.print(distance1);
Serial.println(" cm");
cnt++;
}
delay(50);
}

Best,

I also faced the same problem a few hours ago after reading all the posts in the forum and found an alternate way they maybe help someone. instead of jumper cables and breadboard I directly soldered the wires to my Arduino mega and Molex connector to ultrasonic sensor now it is completely working fine.