Ultrasonic sensor not responding

Hello, i recently purchased an hc-sr04 sensor an dam trying to get it to work with my arduino... I am using this sketch

#include "Ultrasonic.h"

Ultrasonic ultrasonic(12,13);

void setup() {
Serial.begin(9600);
Serial.println("testing...");
}

void loop()
{
Serial.println(ultrasonic.Ranging(CM));
Serial.println("cm");

delay(100);
}

And trig is connected to 12 and echo to 13. When i run the sketch and bring up the serial moniter all i get is this...

0 cm
0 cm
0 cm
0 cm
0 cm
0 cm
0 cm
0 cm
0 cm

...etc

No matter where i move my hand in front of the sensor. Any ideas? Also i am 99.9% sure they are hooked up right. Cant say 100 because you guys always figure something out...:wink: thanks!!

TheWave:
Hello, i recently purchased an hc-sr04 sensor an dam trying to get it to work with my arduino... I am using this sketch

No matter where i move my hand in front of the sensor. Any ideas? Also i am 99.9% sure they are hooked up right. Cant say 100 because you guys always figure something out...:wink: thanks!!

First, that library may not work with your sensor. Use the NewPing library which is designed for the HC-SR04 and has a multitude of other advantages. Also, be sure not to use pin 13 (see the NewPing example sketch).

Tim

teckel:

TheWave:
Hello, i recently purchased an hc-sr04 sensor an dam trying to get it to work with my arduino... I am using this sketch

No matter where i move my hand in front of the sensor. Any ideas? Also i am 99.9% sure they are hooked up right. Cant say 100 because you guys always figure something out...:wink: thanks!!

First, that library may not work with your sensor. Use the NewPing library which is designed for the HC-SR04 and has a multitude of other advantages. Also, be sure not to use pin 13 (see the NewPing example sketch).

Tim

Ok, just downloaded and checked it out. Now it seems to be doing something ( i can here little clickish sounds coming form the sensor) but now i get

Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm

etc...hmmm...Any other ideas? Or have you seen this before?

Hi,

The ultrasonic sensor will make an ultrasonic sound when you put the trigger pin HIGH for a certain amount of time.

The duration of the signal that the sensor gives back on the Echo pin will be your distance.

Try the following code:

#define trigPin 12
#define echoPin 13

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

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

TheWave:

teckel:

TheWave:
Hello, i recently purchased an hc-sr04 sensor an dam trying to get it to work with my arduino... I am using this sketch

No matter where i move my hand in front of the sensor. Any ideas? Also i am 99.9% sure they are hooked up right. Cant say 100 because you guys always figure something out...:wink: thanks!!

First, that library may not work with your sensor. Use the NewPing library which is designed for the HC-SR04 and has a multitude of other advantages. Also, be sure not to use pin 13 (see the NewPing example sketch).

Tim

Ok, just downloaded and checked it out. Now it seems to be doing something ( i can here little clickish sounds coming form the sensor) but now i get

Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm

etc...hmmm...Any other ideas? Or have you seen this before?

Are you using the sample NewPing sketch? What pins are you using? Be sure to NOT use pin 13. What kind of delay do you have between pings? Maybe post your sketch, just to be sure.

Tim

Automative:
Hi,

The ultrasonic sensor will make an ultrasonic sound when you put the trigger pin HIGH for a certain amount of time.

The duration of the signal that the sensor gives back on the Echo pin will be your distance.

Try the following code:

#define trigPin 12

#define echoPin 13

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

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

It just says out of range repeatedly. Do you think the sensor is broken? I was hoping not based on the sounds. Also, is it ok to try it with different pins, like 8 and 9 or something?

teckel:

TheWave:

teckel:

TheWave:
Hello, i recently purchased an hc-sr04 sensor an dam trying to get it to work with my arduino... I am using this sketch

No matter where i move my hand in front of the sensor. Any ideas? Also i am 99.9% sure they are hooked up right. Cant say 100 because you guys always figure something out...:wink: thanks!!

First, that library may not work with your sensor. Use the NewPing library which is designed for the HC-SR04 and has a multitude of other advantages. Also, be sure not to use pin 13 (see the NewPing example sketch).

Tim

Ok, just downloaded and checked it out. Now it seems to be doing something ( i can here little clickish sounds coming form the sensor) but now i get

Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm

etc...hmmm...Any other ideas? Or have you seen this before?

Are you using the sample NewPing sketch? What pins are you using? Be sure to NOT use pin 13. What kind of delay do you have between pings? Maybe post your sketch, just to be sure.

Tim

Heres the sketch

 // ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#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.

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

void setup() {
  Serial.begin(115200); // 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(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

First of, wich version of the IDE are you using?

I quote form an old forum post:
which version of the IDE are you using?

There was a bug that caused the pulsIn() code to premature timeout, see - http://arduino.cc/forum/index.php/topic,74813.0.html -
it should be fixed in the 1.0.1. version.

Also I read that pulseIn is not good for use with an ultrasonic sensor but it should give you some result.

In my opinion, you can use pulseIn with every PIN, correct me if I'm wrong someone...

TheWave:

Automative:
Hi,

The ultrasonic sensor will make an ultrasonic sound when you put the trigger pin HIGH for a certain amount of time.

The duration of the signal that the sensor gives back on the Echo pin will be your distance.

Try the following code:

#define trigPin 12

#define echoPin 13

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

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

It just says out of range repeatedly. Do you think the sensor is broken? I was hoping not based on the sounds. Also, is it ok to try it with different pins, like 8 and 9 or something?

Automative:
First of, wich version of the IDE are you using?

I quote form an old forum post:
which version of the IDE are you using?

There was a bug that caused the pulsIn() code to premature timeout, see - http://arduino.cc/forum/index.php/topic,74813.0.html -
it should be fixed in the 1.0.1. version.

Also I read that pulseIn is not good for use with an ultrasonic sensor but it should give you some result.

In my opinion, you can use pulseIn with every PIN, correct me if I'm wrong someone...

My NewPing library doesn't use pulseIn (it uses all low-level port register calls). This not only works a lot better, it's MUCH faster, more accurate and generates smaller compiled program size. NewPing works with older versions of the IDE as well as v1.0/1.01.

Pin 13 should be avoided due to the LED attached to the pin. I have no problems using pin 13. But, certain models of Arduino and certain revisions have noted problems when using pin 13. So, whenever there's a problem I always suggest not using pin 13, just in case. Also, it's a good idea to try connecting to different pins just in case one of the pins on the ATmega is fried.

Tim

TheWave:
Heres the sketch

 // ---------------------------------------------------------------------------

// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#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.

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

void setup() {
 Serial.begin(115200); // 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(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
 Serial.println("cm");
}

Sounds like the sensor is fried if that sketch doesn't work (and you're using those pins/timings).

Make sure that you've connected all the pins correctly and try different jumper wires. I've seen situations like this and come to find out the problem was the trigger and echo pins were switched or one of the jumper wires had a bad connection at the pin or a totally broken wire. When I get a pack of breadboard jumper wires I go through and test every one for continuity. In every pack, I've had at least one bad wire. This can make you totally pull your hair out as everything is correct but it's not working.

Anyway, test all your jumper wires and verify your connections are correct. If so, you must have a bad sensor. And if it's a bad sensor, I'd like to have it (I wrote the NewPing library). Having a sensor like this can be helpful for testing. PM me if you'd like to help out and send me the sensor.

Tim

I am using 1.0.1

Automative:
First of, wich version of the IDE are you using?

I quote form an old forum post:
which version of the IDE are you using?

There was a bug that caused the pulsIn() code to premature timeout, see - http://arduino.cc/forum/index.php/topic,74813.0.html -
it should be fixed in the 1.0.1. version.

Also I read that pulseIn is not good for use with an ultrasonic sensor but it should give you some result.

In my opinion, you can use pulseIn with every PIN, correct me if I'm wrong someone...

TheWave:

Automative:
Hi,

The ultrasonic sensor will make an ultrasonic sound when you put the trigger pin HIGH for a certain amount of time.

The duration of the signal that the sensor gives back on the Echo pin will be your distance.

Try the following code:

#define trigPin 12

#define echoPin 13

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

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

It just says out of range repeatedly. Do you think the sensor is broken? I was hoping not based on the sounds. Also, is it ok to try it with different pins, like 8 and 9 or something?

Ok, so now i am using different jumper wires and a different breadboard and it seemed to be working fine for a while. I plugged it in and got readings that seemed correct for roughly five seconds before it returned to the ol' 0cm 1149cm. Any ideas on that? Maybe the delay statement needs to be larger?

TheWave:
Ok, so now i am using different jumper wires and a different breadboard and it seemed to be working fine for a while. I plugged it in and got readings that seemed correct for roughly five seconds before it returned to the ol' 0cm 1149cm. Any ideas on that? Maybe the delay statement needs to be larger?

Ok it just happened again. The sensor was working fine and it said 24,24,24 but as soon as i put my hand in front of it it returned to the randomness.
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 24cm
Ping: 6cm
Ping: 5cm
Ping: 4cm
Ping: 4cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 1149cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm

TheWave:
Ok, so now i am using different jumper wires and a different breadboard and it seemed to be working fine for a while. I plugged it in and got readings that seemed correct for roughly five seconds before it returned to the ol' 0cm 1149cm. Any ideas on that? Maybe the delay statement needs to be larger?

The fact that new jumper wires & breadboard yield different results means you must not be testing your circuit very well. And if that's the case, who knows what else is going wrong. We can't diagnose shorts or lose connections over a forum. It sounds like the sensor is working, as it's giving accurate measurements for a time. Set the ping delay to 1000ms, I don't know of any sensor that can't at least do one ping per second. You're going to have to diagnose the problem as well. For example, instead of asking if the delay statements need to be larger, you should simply change the 50 to a 1000 and try it yourself. Should 50ms work, yup! But, these sensors are made by 50 different Chinese companies with different components and logic. So, while all of mine can ping at a very high rate (at least 30 times a second) maybe yours can only don once per second. But, the only way to find out is for you to try it, as we don't have your hardware here to test it for you.

Tim

Yes, i understand what your saying and i have been testing these things out myself. The newest thing ive figured out is that it works unitl the distnace changes. Do you think there is anything wrong with the code?

teckel:

TheWave:
Ok, so now i am using different jumper wires and a different breadboard and it seemed to be working fine for a while. I plugged it in and got readings that seemed correct for roughly five seconds before it returned to the ol' 0cm 1149cm. Any ideas on that? Maybe the delay statement needs to be larger?

The fact that new jumper wires & breadboard yield different results means you must not be testing your circuit very well. And if that's the case, who knows what else is going wrong. We can't diagnose shorts or lose connections over a forum. It sounds like the sensor is working, as it's giving accurate measurements for a time. Set the ping delay to 1000ms, I don't know of any sensor that can't at least do one ping per second. You're going to have to diagnose the problem as well. For example, instead of asking if the delay statements need to be larger, you should simply change the 50 to a 1000 and try it yourself. Should 50ms work, yup! But, these sensors are made by 50 different Chinese companies with different components and logic. So, while all of mine can ping at a very high rate (at least 30 times a second) maybe yours can only don once per second. But, the only way to find out is for you to try it, as we don't have your hardware here to test it for you.

Tim

TheWave:
Yes, i understand what your saying and i have been testing these things out myself. The newest thing ive figured out is that it works unitl the distnace changes. Do you think there is anything wrong with the code?

If you're using my NewPing library and using my test sketch, it's not the code as it's working on many sensors by many people. So, the problem must be with the hardware, not the software. If using a very long delay like 1000ms still doesn't work consistently, and you've verified all your jumper wires and connections, it's probably a bad sensor.

The good thing is that the sensors are cheap, so trying another one is a viable option.

I'm having the exact same problem. Did you get it working, or should I just buy a new one?

For the sake of anyone who has this same problem in the future, I confirmed that this is a bad sensor. I found this thread because i had the exact same problem.

Luckily I ordered 2 of the ultrasonic sensors, after trying everything I could think of, I opened and plugged in the second HC-SR04. It works perfectly... same code, same pins. If you have this problem, my suggestion is that you get a new ultrasonic sensor.

Hi,
Why are you using a Library at all, I find the follwing works fine with many types of U/S sensors.

My favourite code is, and you can use most pins.

---------------------------------------------------------
long scanner(long cm)
{
	const int pingPin=7, EchoPin=8;
	long duration;

	// The SRF005/HC-SR06 is triggered by a HIGH pulse of 2 or more microseconds.
	// Give a short LOW pulse before to ensure a clean HIGH pulse:
	pinMode(pingPin, OUTPUT);
	pinMode(EchoPin, INPUT);  

	digitalWrite(pingPin, LOW);
	delayMicroseconds(2);
	digitalWrite(pingPin, HIGH);
	delayMicroseconds(2);
	digitalWrite(pingPin, LOW); 
	duration = pulseIn(EchoPin, HIGH);
	delay(100);

	// convert the time into a distance
	// inches = microsecondsToInches(duration);
	cm = microsecondsToCentimeters(duration);
	return (cm);
}
//------------------------------------------------------------------
long microsecondsToCentimeters(long microseconds)
{
	// The speed of sound is 340 m/s or 29 microseconds per centimeter.
	// The ping travels out and back, so to find the distance of the
	// object we take half of the distance travelled.
	return microseconds / 29 / 2;
}

Regards

Mel.

I had same problem with a HC-SR04 while constructing a simple robot using a L293d chip to drive 2 small DC MOTORS.

I kept getting

Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 1149cm
Ping: 0cm

in the serial monitor.

The problem solved itself when I pinned the distance sensor directly to the 5 volt UNO pin. For some reason pinning the sensors Vcc pin to a positive hole in the breadboard which I connected to the 5 volt uno pin got the above results.

I am by no means an arduino expert but I suspect the UNO board has a voltage regulator that responds to the demand of the distance sensor.

Hopefully this will help someone.

Larry c