Go Down

Topic: HC-SR04 always returns zero (Read 1 time) previous topic - next topic


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


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);
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("   ");
Serial.print("Distance = "); // Output to serial
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.

Code: [Select]
#define MAX_DISTANCE 200




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.



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



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


Jan 27, 2014, 05:34 pm Last Edit: Jan 27, 2014, 05:36 pm by samwedge Reason: 1
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,



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


void setup() {



void loop() {


unsigned int uS = sonar.ping();




Serial.print("Ping: ");

Serial.print(uS / US_ROUNDTRIP_CM);



link: http://forum.arduino.cc/index.php?topic=55119.15

NewPing link: http://playground.arduino.cc/Code/NewPing´╗┐

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131