Problems with using the HC-SR04 Sonar Unit

hbl2013

Senior Member
I am buiding an Object Avoidance car using one of those inexpensive HC-HR04 Sonar Units. To see if a unit is working properly, I build a small circuit to test them. I am using an 08M2, with the following code. The idea is to use an 28X2 as a main chip for other functions while the 08M2 is checking the surroundings independently, and signalling the 28X2 if there is an obstruction in its path by pulling an input port up (or down).

Code:
;Here is the code for testing the ultrasonic sensor HC-SR04 Sonar unit
;with the terminal windows displaying the distance result in centimetres.
;The sonar has a 2K2 resistor connected between Echo and Trigger
;terminals with C.2 connected to the Trigger terminal. 
;
;Uses the ULTRA command.

#picaxe 08M2

symbol ping = C.2
pause 100
  
main:	

   ultra ping,b1	;measure the distance      
   sertxd("The range is ",#b1,13,10)	;print it       
   pause 300

 goto main		;loop
Unfortunately, I have encountered several problems with these units. First of all, they seem to be quite irratic in use. In the program used above, I get often random readings of the same object inbetween the correct ones. Also, I bought 10 units, of which 7 would not work and would display "0"s as the range, without changing the figure when the object was moved.
On the ones which worked, this range would vary depending on where the object was located, with a "255" if the object was several feet away.
Has anyone used these units with any better results? Or is it something I am missing?
 

jims

Senior Member
hbl2013...I have had good/consistent results from this unit. Found that I had to use a "pause 10'" between the ultra command and the sertxd. You might want to try it. JimS
 

kfjl

Member
I did this with a 20X2:

Code:
#no_data
#no_table

output b.6
input b.7

pause 500

main:
pulsout b.6,2
pulsin b.7,1,w1
pause 1000
let w1=w1*5/58
debug w1
goto main
I used a hand-held breadboard (a real wooden one) as a reflecting surface and a rigid metal tape measure. I got measurements accurate to something like 1cm.
With a reflecting surface mounted perpendicular and parallel to the HC-SR04 I think it would be better than 1cm.
Try fixing it on the edge of your desk so the emitter-detector part has nothing underneath it.
 

erco

Senior Member
I use those sensors, which work fine with the ULTRA command. They are only about a dollar now, no doubt some are factory rejects. Try a few to make sure you don't have a bad unit.

Try a 1.8K resistor instead of 2.2K. Make sure your batteries & connections are good and the wire is big. There's a big current spike for the outgoing pulses which can reset the sensor micro if the voltage sags and can't keep up.

Tech's original post on the one-wire mod at http://www.picaxeforum.co.uk/showthread.php?22639-HC-SR04&p=260470&viewfull=1#post260470

Here's my post on using a DVM to display distances using the ULTRA command.

http://www.picaxeforum.co.uk/showthread.php?27594-1-Digital-Voltmeter-Becomes-One-Pin-LED-Display&p=284989&viewfull=1#post284989

 
Top