HC-SR04 always returns zero

I’ve followed examples using NewPing and ones that don’t … I’ve rewired many times. No matter what I do, I get zero back.

Wondering if it’s the sensor or the code? can anyone help?

I’ve used the code here

http://playground.arduino.cc/Code/NewPing#Example

and tried this code:

/*
HC-SR04 for Arduino

Original project from http://www.swanrobotics.com

This project demonstrates the HC-SR
The distance presented in the code is in mm, but you can uncomment the line for distance in inches.
The schematics for this project can be found on http://www.swanrobotics.com

This example code is in the public domain.
*/

const int TriggerPin = 8; //Trig pin
const int EchoPin = 9; //Echo pin
long Duration = 0;

void setup(){
pinMode(TriggerPin,OUTPUT); // Trigger is an output pin
pinMode(EchoPin,INPUT); // Echo is an input pin
Serial.begin(9600); // Serial Output
}

void loop(){
digitalWrite(TriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin, HIGH); // Trigger pin to HIGH
delayMicroseconds(10); // 10us high
digitalWrite(TriggerPin, LOW); // Trigger pin to HIGH

Duration = pulseIn(EchoPin,HIGH); // Waits for the echo pin to get high
// returns the Duration in microseconds
long Distance_mm = Distance(Duration); // Use function to calculate the distance
Serial.print("Duration = “);
Serial.print(Duration);
Serial.print(” ");
Serial.print(“Distance = “); // Output to serial
Serial.print(Distance_mm);
Serial.println(” mm”);

delay(1000); // Wait to do next measurement
}

long Distance(long time)
{
// Calculates the Distance in mm
// ((time)*(Speed of sound))/ toward and backward of object) * 10

long DistanceCalc; // Calculation variable
DistanceCalc = ((time /2.9) / 2); // Actual calculation in mm
//DistanceCalc = time / 74 / 2; // Actual calculation in inches
return DistanceCalc; // return calculated value
}

I don't see a constructor for the sensor in your code.

#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

hmm…

tried this also:

#include <NewPing.h>
#define TRIGGER_PIN 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 8 // 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.

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

void setup() {
Serial.begin(9600); // Open serial monitor at 115200 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.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(sonar.convert_in(uS)); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)

still get zero all the time

Are you sure the sensor is ok ? I had a dead one recently, exhibited the same behaviour as you describe.

Also, just double check you have your send / receive pins connected correctly on the sensor, i.e. not swapped.

Cheers, John

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: http://forum.arduino.cc/index.php?topic=55119.15

NewPing link: http://playground.arduino.cc/Code/NewPing

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 ;)

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: