Distance measurement using HC-SR04

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Distance measurement using HC-SR04

Post by TomasS »

Hi

I would like to do a project using a HC-SR04 for distance measurement. Anybody who has done a similar project who would like to share a program example for this? :D

BR

Tomas

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Distance measurement using HC-SR04

Post by Benj »

Hi Tomas,

That looks like a great little module. May have to get one myself and have a play :)

Not done anything with these but looking at the sample PICaxe code it looks quite straightforward.

Here is some simple psudocode I came across for driving the device.

symbol trig = 3 ‘ Define output pin for Trigger pulse
symbol echo = 6 ‘ 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
debug range ‘ display range via debug command
goto main ‘ and around forever


You could output the start pulse and then use say a timer interrupt to time the period for the pulse to return.

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Distance measurement using HC-SR04

Post by TomasS »

Hi Ben
Benj wrote:
That looks like a great little module. May have to get one myself and have a play :)

Not done anything with these but looking at the sample PICaxe code it looks quite straightforward.
That's a nice site, indeed ;)
Benj wrote:Here is some simple psudocode I came across for driving the device.

symbol trig = 3 ‘ Define output pin for Trigger pulse
symbol echo = 6 ‘ 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
debug range ‘ display range via debug command
goto main ‘ and around forever


You could output the start pulse and then use say a timer interrupt to time the period for the pulse to return.
Looks nice - I must try to "translate" this into Flowcode :roll:

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Distance measurement using HC-SR04

Post by Benj »

Give it a go and post up your Flowcode program if you get stuck and we should be able to help you get this device up and running.

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Distance measurement using HC-SR04

Post by TomasS »

Benj wrote:Give it a go and post up your Flowcode program if you get stuck and we should be able to help you get this device up and running.
Thanks. I'll do that ;)
The module has now been bought - $3.41 including shipping, so I await it's arrival and will return after this!

BR.

Tomas

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Distance measurement using HC-SR04

Post by medelec35 »

TomasS wrote: The module has now been bought - $3.41 including shipping
Wow that sounds like a bargain!
hope it's all going to work for you.
With all the experts on here, (and me of course :P ) I'm sure we would be able to get a working project for you.
I have already started working on ultrasonic range finder. Not sure on the distance it will work up to yet.
If successful I can post the flowchart.

Martin
Martin

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Distance measurement using HC-SR04

Post by JohnCrow »

Ive played with the SRF05 (which is the same pin out as the SRF04) ranger using the projects in Bert van Dams Flowcode book which work well.
These were done in FC3 but certainly worked fine in FC4 (Not tried them in FC5 but should work).

Im currently trying an SRF10 which is a lot smaller than the SRF05 and uses I2C bus, but so far ive not been able to get it to work properly. :(Need to have another look at this project soon)
1 in 10 people understand binary, the other one doesn't !

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Distance measurement using HC-SR04

Post by TomasS »

medelec35 wrote:
TomasS wrote: The module has now been bought - $3.41 including shipping
Wow that sounds like a bargain!
hope it's all going to work for you.
With all the experts on here, (and me of course :P ) I'm sure we would be able to get a working project for you.
I have already started working on ultrasonic range finder. Not sure on the distance it will work up to yet.
If successful I can post the flowchart.

Martin
Hi Martin

Yep! Ebay is the place to find the stuff :wink:
I'm pleased ´to find all you experts on here - for sure!
Sounds great - maybe we can compare solutions, when I receive my version?

BR.
Tomas

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Distance measurement using HC-SR04

Post by medelec35 »

Benj wrote:
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)
Hi Ben. I have started working on my own ultrasonic detector, but was a bit puzzled about range of 10us and the distance.

Since sound travels at approx 343.2 metres per second in dry air at 20 deg C
then 1meter it takes 1/343.2 = 2.01ms
so if divide by 100 then sound takes 2.01ms/100 = 29.14uS to travel 1cm
Divide by 2 then it takes 14.57 to travle in 0.5cm

But if going by lots of 10us then after say 100 lots of 10us distance will calculate at 100/5.8 = 17cm
But at a distance of 17cm, i believe sound will take 17*29.14 = 495us and not 1ms?

Therefore would be divide by 2.9 and not 5.8?

I could be wrong of course?


Edit :Ahh logically worked it out.

Ben that is correct. because its an echo, it will take twice as long since the distance is double. to the overall calculation has to 1/2 the distance hence 5.8 and not 2.9
At least I have worked out the reason. lol


Martin
Last edited by medelec35 on Sat May 12, 2012 1:26 pm, edited 1 time in total.
Reason: Worked out why 5.8 is used instead of 2.9.
Martin

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Distance measurement using HC-SR04

Post by TomasS »

JohnCrow wrote:Ive played with the SRF05 (which is the same pin out as the SRF04) ranger using the projects in Bert van Dams Flowcode book which work well.
These were done in FC3 but certainly worked fine in FC4 (Not tried them in FC5 but should work).

Im currently trying an SRF10 which is a lot smaller than the SRF05 and uses I2C bus, but so far ive not been able to get it to work properly. :(Need to have another look at this project soon)
Hi John

I'm actually feeling a little stupid at the moment since I actually have this book on my shelf, but I did not see that it had a project with the ultrasound sensor - I will look into that! :oops:

BR.

Tomas

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Distance measurement using HC-SR04

Post by medelec35 »

TomasS wrote:
I'm pleased ´to find all you experts on here - for sure!
I wasn't including myself in the expert list :lol:
TomasS wrote:
Sounds great - maybe we can compare solutions, when I receive my version?
Sure, as you can see from my post above, I have already started, and querying the calculations.
Has to be right :)

If I can get it working OK,will post flowchart in the tips section.
I will also post on here a link to it.

I have a busy weekend, but I will try to complete as much as I can.
TomasS wrote:I actually have this book on my shelf, but I did not see that it had a project with the ultrasound sensor - I will look into that! :oops:
If you go to this website:
http://www.elektor.com/extra/microcontr ... 1368.lynkx
You can download the flowchart (by Download the complete package).
Flowchart can be found in Chapter5, section 5-3

Martin
Martin

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Distance measurement using HC-SR04

Post by TomasS »

medelec35 wrote:
TomasS wrote:
I'm pleased ´to find all you experts on here - for sure!
I wasn't including myself in the expert list :lol:
:wink:
medelec35 wrote:
TomasS wrote:
Sounds great - maybe we can compare solutions, when I receive my version?
Sure, as you can see from my post above, I have already started, and querying the calculations.
Has to be right :)

If I can get it working OK,will post flowchart in the tips section.
I will also post on here a link to it.

I have a busy weekend, but I will try to complete as much as I can.
Sounds great! I'll look forward to it :)
medelec35 wrote:
TomasS wrote:I actually have this book on my shelf, but I did not see that it had a project with the ultrasound sensor - I will look into that! :oops:
If you go to this website:
http://www.elektor.com/extra/microcontr ... 1368.lynkx
You can download the flowchart (by Download the complete package).
Flowchart can be found in Chapter5, section 5-3

Martin
Done!

Post Reply