HR-SR04 Ultrasonic rangefinder troubles...

Hi all,
First of all, I am sorta new to arduino. I have two HR-SR04 rangefinders that I purchased off Amazon. I quickly pulled them out, wired them up, and downloaded (of course :wink: ) the newping library. Got it all set, loaded up an example program to test one of the sensors. Changed the pin numbers to match the ones my sensor was hooked up to, and uploaded.
At first, i thought it was working fine…Until i tried measuring anything passed 50cm. below 50cm, it was perfectly accurate (i have a meter stick out and was checking the sensor against that). However, after moving the piece of cardboard i was using to test the sensor with out to around 150 cm, i would just return a value between 50 to 60cm.
I traded out the sensors (thinking one might have been damaged), and got the same problem. afterwards, I tried changing the code to fix it, but it’s still not working. Here are the changes and code. Thanks to anyone who can help me.
I have also removed the default comments from the example code to make room for my own…

#include <NewPing.h>

#define TRIGGER_PIN  11  //plugged into the "trig" pin of the sensor
#define ECHO_PIN     9  //plugged into the "echo" pin of the sensor
#define MAX_DISTANCE 200 //changed this value several times, from 100, 200, 400, and 500 cm

int led = 12; //I always have an led in so i know when the program has started

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(115200);  // Open serial monitor at 115200 baud to see ping results.
  pinMode(led, OUTPUT);  //my led code
  digitalWrite(led, HIGH);   /my led code
}

void loop() {
  delay(250);                      // started this value at 50, then changed to 1000, and to 250, no luck 
  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");
}

And that’s it. I have no idea why these will not work past 50cm. and yes, the vcc and ground pins are hooked up to the 5v and gnd pins of the arduino :wink:

TechnoD

If you are powering the Arduino (and HR04) from USB that can be a problem. The HR04 really likes 5V (minimum). Often the 5V output of the Arduino is somewhat less than 5V when powered by USB. Try powering through Vin to get a more solid 5V.

hi, and thanks for your response. actually, I am already powering the board through the voltage input. the power supply is 5v 1 amp.

TechnoD

Was my best guess. I had a similar problem and that was the fix. Can you measure the actual voltage that supplies the HR04 just to be sure?

Just tested it with my voltmeter, 5v output is just that, a perfect 5.0 volts
could it be an amp problem? am i supplying it with too little or too many amps?
TechnoD

Since it’s getting 5.0V that should rule out supply. I’ve got to say that I don’t know why it won’t go beyond 50cm. Your code is right from the example so should work.

Alright, I'll keep doing some testing, and if I figure it out, I'll post the answer here.
TechnoD

How long are the wires between Arduino and the rangers? If they are longer than like 30cm, add a 10uF capacitor between 5V and gnd at the ranger side. I had your problem and adding the cap stopped the randomness. The transient drop of voltage can’t be seen on a voltmeter. Try instead of a cardboard, the door. Move your ranger towards or away from a door.