HC-SR04 Problems

Hi all,

A while ago I bought a HC-SR04 Ultrasonic Sensor, and it worked fine, but now I want to use it on a robot and I am having some problems.

 

When I output the data from the sensor to the serial monitor this is the result:

 

MS: 204551, CM: 3702.51, IN: 1457.68

MS: 1526, CM: 27.62, IN: 10.87

MS: 204613, CM: 3703.63, IN: 1458.12

MS: 847, CM: 15.33, IN: 6.04

MS: 204630, CM: 3703.94, IN: 1458.24

MS: 613, CM: 11.10, IN: 4.37

MS: 204615, CM: 3703.67, IN: 1458.13

MS: 419, CM: 7.58, IN: 2.99

MS: 204636, CM: 3704.05, IN: 1458.28

MS: 785, CM: 14.21, IN: 5.59

MS: 204656, CM: 3704.41, IN: 1458.43

MS: 1598, CM: 28.92, IN: 11.39

MS: 204688, CM: 3704.99, IN: 1458.65

MS: 1553, CM: 28.11, IN: 11.07

MS: 204715, CM: 3705.48, IN: 1458.85

MS: 1526, CM: 27.62, IN: 10.87

MS: 204726, CM: 3705.68, IN: 1458.93

MS: 1544, CM: 27.95, IN: 11.00

MS: 204715, CM: 3705.48, IN: 1458.85

MS: 1519, CM: 27.49, IN: 10.82

MS: 204745, CM: 3706.02, IN: 1459.06

MS: 1523, CM: 27.57, IN: 10.85

MS: 204733, CM: 3705.80, IN: 1458.97

As you can see only every second reading is accurate.
I have tried using two codes, one a modified version of the ping example, which worked before, and another example from a library found here: http://freecode.com/projects/hc-sr04-ultrasonic-arduino-library .
I could just ignore every second reading, but I would prefer not to, so if any one has any ideas, please suggest.
Thanks,
Philip

 

Are you resting in between

Are you resting in between samples? The unit needs cool-down time of a few microseconds (like 10) between readings otherwise it will give you garbage or accedentaly range it’s own echo. Try adding a pause() in between measurements. You seem to be sampling continually which the sensor can’t do. 

Thanks

Thanks, but during this test there was a 1 second pause between readings.

For the moment though I am just ignoring any readings over 3000.