HC-SR04 problems

tobyw

Member
Hello

I have been trying to interface this ultrasonic distance finder module with a PICAXE 18x

I found two helpful code examples, and they both seem to do the actual sensing the same way. My code is


symbol trigger = 3
symbol echo = 6
symbol range = w1

main:
pulsout trigger, 2
pulsin echo, 1, range
pause 10
range = range * 10 / 58 ‘ multiply by 10 then divide by 58
debug range
pause 500
goto main


My problem is that, although I am getting values in w1, they don't make sense. Eg... pointing at the celing the value is 7, when i place my hand over the sensor the value is 5, pointing it into the distance the value is 8, and all sorts of other obviously incorrect readings.

Unfortunately, there is not much on Google about interfacing this with the picaxe, and the datasheet isn't that helpful to someone who is a bit of a newbie like me.

Has anyone had any success with this module and a picaxe? If so, is my code wrong, is my module faulty, or is it something else?

Many thanks
 

nick12ab

Senior Member
Code looks fine.

What's your schematic? Have you made sure that trigger is connected to an output pin and echo is connected to an input pin?

Have you got decoupling capacitors fitted?
 

tobyw

Member
Thanks for the replies.

Thanks eclectic. I actually did see that thread when I was trying to google an answer eclectic, but it didn't really help me - other than a suggestion on there that the picaxe should be set to 8Mhz - but it doesn't seem possible with the 18x I am using.

nick12ab - I am pretty sure the pins are connected to output / input because I am definitely getting some readings...just not accurate ones. I don't know about need decoupling capacitors...where would I need these?

Also, I did see something about another sensor saying the grounds need to be set to common ground..but i assume since I am using a PICAXE prototyping board, they would be anyway?...

Thanks
 

tobyw

Member
I should have dropped a bigger hint. :)

Use an M2 PICAXE.

e
I'm obviously really thick - didn't see where that hint was. So is the SR04 only compatible with later chips? Unfortunately I don't have any until I get my christmas money :( I can't see anything on the datasheet about clock speed....could you explain to this dummy? Do you mean it has to run at a higher speed? Why?

Thanks
 

eclectic

Moderator
I'm obviously really thick - didn't see where that hint was. So is the SR04 only compatible with later chips? Unfortunately I don't have any until I get my christmas money :( I can't see anything on the datasheet about clock speed....could you explain to this dummy? Do you mean it has to run at a higher speed? Why?

Thanks
Have you got an "old" 08M?
Might work using the sane pin as input/output.

e
 

tobyw

Member
Hi yes I have. But I'm obviously still missing the point about why it needs to be m or m2. Is it for the faster clock speed? And if so

1. Why does the sensor need a faster chip? The x can pulsout and in at the frequencies specced..?

2. What frequency is needed? Can I get around the problem by connecting an external resonator or using setfreq?

Thanks
Toby
 

eclectic

Moderator
Try Technical's post #12 suggestion, from the thread referred to in post#2 above.

You'll need to modify the pin numbers.

The 08M and M2 have "bidirectional pins" (1,2,4)

e

The "old" 18X has In or Out.

e
 

russbow

Senior Member
This code works for both my sr04 and srf05 - the srf04 is stamped HC-SR04

Code:
#picaxe18x


symbol trig = 0 ‘ Define output pin for Trigger pulse
symbol echo = 1 ‘ Define input pin for Echo pulse
symbol range = w1 ‘ 16 bit word variable for range

main:

pulsout trig,2 '‘ produce 20uS trigger pulse (must be minimum of 10uS)

pulsin echo,1,range '‘ measures the range in 10uS steps

pause 10 '‘ recharge period after ranging completes

'‘ now convert range to cm (divide by 5.8) or inches (divide by 14.8)
'‘ as picaxe cannot use 5.8, multiply by 10 then divide by 58 instead

let range = range * 10 / 58 ‘ multiply by 10 then divide by 58

sertxd ("range ",#range," Cm ",cr,lf)

pause 1000

goto main '‘ and around forever
It works as above for the old 18x chip and by changing the symbols from 0 to b.0 and 1 to c.1 it runs on an 18m2 without any freq modification.

It doesn't look any different to your code, so suggest you really check your circuit and connections.

Hope that helps

R.
 
Last edited:

Technical

Technical Support
Staff member
In this thread, and some other recent threads, people should realise that an SRF05, an SRF04 and a HC-SR04 are all completely different boards with different circuits, and hence have different operating requirements. So it is helpful to clarify exactly which board you are using when giving example programs.
 

eclectic

Moderator
Cheers russbow.
I was only answering using the posts of others as references.

Can I ask a favour please?

If you have a 08M,
could you test it using the single pin circuit?

e
 

tobyw

Member
In this thread, and some other recent threads, people should realise that an SRF05, an SRF04 and a HC-SR04 are all completely different boards with different circuits, and hence have different operating requirements. So it is helpful to clarify exactly which board you are using when giving example programs.
So to clarify, when Russbow said his SRF04 was stamped HC-SRO4 does this mean they are not the same, and the stamp was wrong?

If so, it sounds like perhaps the code given will not work with the HC-SR04 - so the HC-SRO4 needs to use the single pin method?..and clock speed is irrelevant?
 

Technical

Technical Support
Staff member
So to clarify, when Russbow said his SRF04 was stamped HC-SRO4 does this mean they are not the same, and the stamp was wrong?

If so, it sounds like perhaps the code given will not work with the HC-SR04 - so the HC-SRO4 needs to use the single pin method?..and clock speed is irrelevant?
If it is stamped HC-SR04 then that is what it is - it is *not* an SRF04! An SRF04 also has 5 pins, a HC-SR04 has 4.
The HC-SR04 returns the echo pulse much quicker than the other two devices, and the start of the echo pulse can be missed in certain circumstances/programs. That is why the single pin method using high-pulsin on a single pin is the only recommended way of using the HC-SR04 with PICAXE.
http://www.picaxeforum.co.uk/showthread.php?22639-HC-SR04&p=223675&viewfull=1#post223675
 

tobyw

Member
hc-sro04.jpgOk

Here is my code and the debug vals...

I have a 1.9k resistor connecting the trig and echo pins on the HC-SR04, and a jumper wire between the trigger pin and pin 5 (input 2) of my 08M

The value of w2(calcd) starts out at 8 when pointing directly up at the ceiling. If I place my palm directly in front of the module sensors, the value will go to 4 or 5. Waving objects around 1-2 foot in front of the module sometimes changes the value, but with nonsensical values like 5,6,7. This is just the same as it was doing when I wired it the other way...

So....what am I doing wrong? Is it my calculation? It feels like I should be getting values between 2 and 500 (cm)...am I off track?

Appreciate the help..
 

russbow

Senior Member
Either I am confused, or I have caused confusion. My unit , HC-SR04, is visually identical to the one linked in post #1
The code in post #10 works with an 18x, ( and an 18m2 ). I did initially forget the pullup on the 18x reset though :mad:
 

Technical

Technical Support
Staff member
It's often a case of you get what you pay for. The HC-SR04 is very cheap but lots of people do have issues with them, we have quite a lot of support requests on this unit. The SRF005 is more expensive, but far more reliable in our experience.
In this case it does actually sound like Toby's HC-SR04 is not working as expected.

There could well also be different timing tolerances on these devices, because some people get them to work fine and some don't (e.g. see the other thread already linked). In most cases the single pin method will work for most people, although the 2 pin method russbow is using *may* also work.

Also make sure you have a good smooth supply @ 5V
 

tobyw

Member
Also make sure you have a good smooth supply @ 5V
Aarrggh...that was it all along. I checked the supply and it was 4.08V. Refreshed batteries and the results are much more sensible looking. Still pretty inaccurate, but I guess you are right about the cost difference.

Thanks to everyone.
 

russbow

Senior Member
@Toby
Add this line to your code and press F8 when it has loaded.

sertxd ("range = ",#range," Cm ",cr,lf)

Much nicer to follow than debug.
 

tobyw

Member
@Toby
Add this line to your code and press F8 when it has loaded.

sertxd ("range = ",#range," Cm ",cr,lf)

Much nicer to follow than debug.
Thanks Russ. By the way - your method works fine as well. It seems like both one-wire and two-wire methods work.
 

Haku

Senior Member
Odd coincedence, I received some HC-SR04's yesterday, bought 5 because they were only £1.30 each but took 42 days to arrive..!

On a 20m2 I found that it didn't work running at the default 4mhz because it couldn't catch the return pulse in time so set it running at 32mhz because this reduces the pulsin timeout and speeds things up when the sensor doesn't get a return ping. I've hooked up a 10 LED bar graph to the 20m2 for that 'ooh, shiny!' factor, but now I have to find an actual use for them beyond breadboard tinkering :D

Nice tip about getting them to operate on a single IO pin, I'll have to try that next.
 

Puuhaaja

Senior Member
I tried several commands and methods and none of them worked with my hc-sr04 and picaxe 18m2. Now I have connected echo cable to trigger cable with 1k8 resistor and using one pin method. With 4.5v battery supply it's working ok, but not as well as using usb powered 5v power supply. At 8mhz my code is:
symbol distance = w0

main:
setfreq m8
low B.0
high B.0
pulsin B.0, 1, w0
let w0 = w0 * 5 / 58
debug distance
goto main
and when using 4 mhz code is:
symbol distance = w0

main:
low B.0
high B.0
pulsin B.0, 1, w0
let w0 = w0 * 10 / 58
debug distance
goto main
Finally I got them working

EDIT:

I got it working also when using normal two pin method. I had to chance 4 mhz to 8 mhz and after that distance is distance * 5 / 58, not distance * 10 / 58 as it is when using 4mhz. It seems that two pin method react much faster than one pin.

symbol distance = w0
symbol trigger = B.0
symbol echo = C.1

main:
setfreq m8
pulsout trigger, 2
pulsin echo,1, distance
pause 10
let distance = distance * 5 / 58
debug distance
pause 50
goto main
 
Last edited:

Technical

Technical Support
Staff member
No, two pin will not work quicker than one pin. You could try this:

Code:
[COLOR=#000000][I]symbol distance = w0[/I][/COLOR]

[COLOR=#000000][I]main:[/I][/COLOR]
[COLOR=#000000][I]setfreq m8[/I][/COLOR]
[COLOR=#000000][I][/I][/COLOR]high B.0
pause 4
[COLOR=#000000][I]pulsin B.0, distance[/I][/COLOR]
[COLOR=#000000][I]let distance = distance * 5 / 58[/I][/COLOR]
[COLOR=#000000][I]debug distance[/I][/COLOR]
[COLOR=#000000][I]pause 50[/I][/COLOR]
[COLOR=#000000][I]goto main
[/I][/COLOR]
 

Puuhaaja

Senior Member
It seems that two pin method react much faster than one pin.
No, two pin will not work quicker than one pin. You could try this:
I should had mentioned that when I'm watchin debug screen and moving different objects in front of sensor and testing different angles and distances it seems that two pin method reacts faster when angle or distance chances. when using one pin method it quite often shows 0 cm for about one second and then it will show the right distance. when using 8mhz and two pin it seems to be little more accurate.

pulsin B.0, distance
I got error from this so I changed it to:
pulsin B.0, 1, distance
That method worked about 5 seconds and after that it shows 0 and nothin more. I added low B.0 before high B.0:

symbol distance = w0

main:
setfreq m8
low B.0
high B.0
pause 4
pulsin B.0, 1, distance
let distance = distance * 5 / 58
debug distance
pause 50
goto main
that seems to work perfectly. Both 1 and 2 pin methods shows exactly same distances when I have tested them.

CONCLUSIONS:

1 PIN METHOD
Use this method if you want to reserve pins to other devices. Connect echo to trigger by using 1800 ohm resistor. You need to connect only one pin(trigger) to microcontroller. This method can be used both 4mhz and 8 mhz. When using this method is important to know that sometimes there can be about 0,5-1 second delays when getting right distances.

example code:
symbol distance = w0

main:
low B.0
high B.0
pulsin B.0, 1, w0
let w0 = w0 * 10 / 58
debug distance
goto main
2 pin method
This method can be used only when using 8mhz, but you can always change freqvencies by using set freq command. This method seemed to react faster than 1 pin method when I changed angles and distancies in front of sensor. So..if you need fast reaction time use this method.

example code:
symbol distance = w0
symbol trigger = B.0
symbol echo = C.1

main:
setfreq m8
pulsout trigger, 2
pulsin echo,1, distance
pause 10
let distance = distance * 5 / 58
debug distance
pause 50
goto main
I hope this will help other users in future.
 
Top