Measuring HC-SR04 always change

Hello there,
I'm new user of arduino project and now i have a trouble with my ultrasonic sensor of HC-SR04.
My arduino duemilanove get the pulse from sensor is always change, it doesn't stable. The sensor and object in a same position, not move but the measuring of distance can't have a same value.
I was connect the trigger at pin 2 and echo at pin 3.
Any suggestion or solution about it?

Check the supply voltage to the HC-SR04. I found that it must be at least 5.00 volts to operate reliably. If you are powering your board from USB the voltage may be low.

Thank's for reply.
I already try to use external power supply such as 9V battery and 12VDC switching power supply, but the result still same.not stable.
I already change the sensor with the new one of HC-SR04. Still no change. Pulse in still not stable.
Sorry for my english. :sweat_smile:

That was my best guess.
Can we see some readings from the sensor? What is your target (object that you are trying to detect)?
Post your code, maybe something there.

I'm using this sensor to measure the human height.
And this is the code i used.
//--------------------------------------------
#define ECHOPIN 3
#define TRIGPIN 2
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8 ) ;
int H2,HT,H1;

void setup(){
lcd.begin(16, 2);
lcd.print(β€œHeight”);
lcd.setCursor(0, 1);
lcd.print(β€œTB=”);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
delay(1000);
HT=200;
}

void loop(){
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
int distance = pulseIn(ECHOPIN, HIGH);
distance= distance/58;
H2=HT-distance;
lcd.setCursor(3, 1);
lcd.print(H2);
lcd.print(” cm β€œ);
delay(1000);
}
//--------------------------------------------

I put the sensor at 2 meters of height, so i get HT = 200. And the Object is a stainless plate. I have to move the plate above the head to get the best rebound area.

is there something false on my code?
Thank you very much before.

This is my next trial of HC-SR04, using a new coding.

//---------------------------------------------------------
#include <Ultrasonic.h>

#define TRIGGER_PIN 3
#define ECHO_PIN 2

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

void setup()
{
Serial.begin(9600);
}

void loop()
{
float cmMsec, inMsec;
long microsec = ultrasonic.timing();

cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
Serial.print("MS: ");
Serial.print(microsec);
Serial.print(", CM: ");
Serial.print(cmMsec);
Serial.print(", IN: ");
Serial.println(inMsec);
delay(1000);
}

//---------------------------------------------------------------
And this is what i get from serial monitor.

MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13
MS: 1414, CM: 25.59, IN: 10.08
MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13
MS: 1415, CM: 25.61, IN: 10.08
MS: 1414, CM: 25.59, IN: 10.08
MS: 1421, CM: 25.72, IN: 10.13
MS: 1422, CM: 25.74, IN: 10.13
MS: 1422, CM: 25.74, IN: 10.13
MS: 1415, CM: 25.61, IN: 10.08
MS: 1415, CM: 25.61, IN: 10.08
MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13
MS: 1415, CM: 25.61, IN: 10.08
MS: 1415, CM: 25.61, IN: 10.08
MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13

The posision of sensor and object doesn't change. Not moving. But, the result of "MS" like we see above. Not stable.
What is wrong ???
I'm in desperate mode now. =(

I think your expectations of accuracy are maybe a bit high. The measurements only vary by 1.5 MM (0.060"). This from the data sheet: the ranging accuracy can reach to 3mm. If you need higher accuracy you should look into something like a Sharp IR rangefinder.

Edit: I thought of one more thing to try. I don't know that it will help. Connect a 10uf cap across the power supply pins (Vcc-GND) as close to the sensor as possible. The sensor can draw quite a bit of current, relatively, while pinging so the cap may help smooth things out. Also, how often are you ranging? Is there enough time between pings to let reflections die out?

for my first coding, every 1 sec it has to ranging and delay for trigpin to high is just 10microsecond.
I think it is enough to let reflection die out. But, i'm not sure. :smiley:
Oke, i'll try to using a capacitor like you suggest.
Thank's for your reply. It's really help me to do my project.

Hi,

You may try using a 2 cm pipe mounted on the barrel of sensor just to narrow down the width of sensing beam. But as someone mentioned above, the level of accuracy is reasonable what your are getting. I am using SR04 with velvet coated pvc pipe mounted on barrel and I am also seeing some variations in readings, but its working okay for me.

Hi there,

Your sensor is working properly ??
How you can made it ??

I need this project to measure 200cm max distance and need precision too.
How long maximal distance you made ??
Is it working good when detecting obstacle more then 80cm ??