HC-SR04 not measuring correctly past 1 meter

Thread Starter

fasm

Joined Nov 12, 2015
8
I'm using a HC-SR04 ultrasonic distance sensor for a school project. I'm using an ATMEGA328 with Arduino bootloader, on a breadboard, to process the data from the sensor. It works perfectly, until I try to measure distances at or beyond one meter. The data sheet says the sensor works up to 4 meters, and I have 3 of them that all aren't working properly. This is the code that triggers the sensor and works out the distance:

long duration, cm;

digitalWrite(trigPin, LOW);
delayMicroseconds(5);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
delay(10);
cm = (duration/2) / 29.1;

Any help would be appreciated.
 

GopherT

Joined Nov 23, 2012
8,009
These sensors need about 250 mA of 5 volt power when they make their pulse. If you are using a weak battery or if you have thin PCb traces or other issues, there may not be a good loud tone signal. Without a good, lout tone, the echo will be weak.

I would start by checking that I have good connections. If that doesn't solve everything, I would add a 470 to 1000 uF capacitor across the Vcc and ground pins on the ultrasonic sensor. Make sure capacitor negative is connected to ground. That cap should insure that you get a good strong tone each time it blasts.

Also, 2 meters is about the limit on these sensors - you need a 1 sq ft flat area as a target. Any angles or small targets screw up the echo. A better model (not perfect) is the Ping))) from Parallax. Costs about 10x to 15x more than the one you have.
 

Thread Starter

fasm

Joined Nov 12, 2015
8
I only have a 330uF cap but that seems to have helped a lot, still struggles at 150cm but much better than it was before.

Do you know if the ping))) would make a big difference?
 

GopherT

Joined Nov 23, 2012
8,009
If you have good power supplies for both, The only difference between Ping and HC-SR04 is the size of the target that the ping can detect. The ping can "see" smaller objects. The HC-SR04 needs a reseda ably big (1 foot x 1 foot) to achieve about 100 inches (3 meters). The Ping also has a practical range of about 3 meters. To go more, you need a quiet room with no reflections possible (that means 1 or two meters off the floor or away from walls so you don't get interferences or losses from the floor or walls.
 

Thread Starter

fasm

Joined Nov 12, 2015
8
Thanks a lot for the advice, my school project is a proof of concept really so I think I should be able to stick with the SR04.
 

GopherT

Joined Nov 23, 2012
8,009
Thanks a lot for the advice, my school project is a proof of concept really so I think I should be able to stick with the SR04.
I stopped using the PING after they raised their prices. Also, the ping is more annoying to use since the trigger and output are the same pin (3pins total). So, if you have a device that the pint outputs to, you need to add circuitry to prevent the downstream device from triggering when you are triggering the PING. The 4 pins on the SR-04 is much better.
 
Top