HC-SR04 Very Imprecise

I have 2 ultrasonic sensors that I am using with the New Ping Library, but they return very imprecise measurements using any form of measurement. Also, when you try to detect anything between 8 and 1cm, it returns 1149cm. Is it something with the sensor or the library? I am using the NewPing example code.

Those sensors generally don't work at all for distances shorter than about 20 cm.

Define "imprecise". The measurements I make with them are accurate to about +/- 1 cm, in agreement with the specifications.

HC-SR04 can actually do 2 cm distance minimum. I'm using them for those short distances just fine. I sometimes get signals at about 1 cm but it's not reliable. It may give no signal at all, or weird numbers.
Accuracy is within about 1 cm, as is reproducibility of the signal.

The HC-SR04 sensors I have don’t work below about 20 cm, but I see now that the linked version is “2.0” and claims much shorter range.

wvmarle:
HC-SR04 can actually do 2 cm distance minimum. I'm using them for those short distances just fine. I sometimes get signals at about 1 cm but it's not reliable. It may give no signal at all, or weird numbers.
Accuracy is within about 1 cm, as is reproducibility of the signal.

You probably need to add a 10uf capacitor between ground en 5v at the module. That should solve the strange readings.

The poor readings that I get are when the device is outside of its range, so power doesn't seem to be an issue. When in normal range I get very good accuracy and reproducibility (my distances are typically in the 3-30 cm range, but sometimes it's a bit less).
By the way, I'm operating at 3.3V.

jremington:
Those sensors generally don't work at all for distances shorter than about 20 cm.

Define "imprecise". The measurements I make with them are accurate to about +/- 1 cm, in agreement with the specifications.

I mean that it is accurate to +/- 10cm.

Most people report observing +/- 1 cm variation in distance readings with HC-SR04 sensors.

That is precisely the problem.

You mean you need higher accuracy than 1 cm? If so you'll have to look for different methods of measurement.

Those sensors work with 40kHz ultrasound.
40kHz has a wavelength of about 8.5mm.
The transmitter sends afaik a train of 8 pulses (waves) before the receiver parts starts to listen.
If so, nothing under 8*8.5= 67mm is possible.
That ~8.5mm should also be the resolution/granularity/steps.
Leo..

Half of that as you're measuring double the distance you want (it's a return trip).
Every half wavelength of difference in distance between the HC-SR04 and the surface you're trying to measure would result in a full wavelength difference in path travelled, bringing your theoretical resolution to just over 4 mm.
Doing a number of samples (like 10 or 20 measurements) could improve on that, as you can average out the measurements. Or that's the theory, at least.
Based on these sensors being as cheap as they are, a 1 cm accuracy sounds very reasonable.

wvmarle:
You mean you need higher accuracy than 1 cm? If so you'll have to look for different methods of measurement.

No I mean that these sensors should be +/-1cm but mine are more like +/-10cm which is a whole lot worse.

The other day I did some analyses of my data, which is now about a week worth of measuring water levels. Accuracy and reproducibility is mostly within 1 cm but when the reservoirs got empty it got a whole lot worse, and the signal started to jump around.
It's a rather small container with the sides not much further away than the water surface in that situation, so I suspect I'm getting echoes. I've also found before that the surface you're pointing at matters a lot, walls or table tops that are well away from other surfaces work best.

No I mean that these sensors should be +/-1cm but mine are more like +/-10cm which is a whole lot worse.

Post your code, using code tags. I'll try it with my older version sensors.

#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(115200);
}
 
void loop() {
  delay(50);
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm());
  Serial.println("cm");
}

stormfalcon32:
I have 2 ultrasonic sensors that I am using with the New Ping Library, but they return very imprecise measurements using any form of measurement. Also, when you try to detect anything between 8 and 1cm, it returns 1149cm. Is it something with the sensor or the library? I am using the NewPing example code.

I noticed that I quoted the wrong person, but you should try adding a 10uf capacitor between ground en 5v at the module. That solved the strange readings for me.

Ok. Do you have any ideas for why it would be so imprecise?