Distance measurement using HC-SR04
Moderator: Benj
-
- Posts: 75
- Joined: Wed Oct 28, 2009 2:37 pm
- Has thanked: 15 times
- Been thanked: 1 time
- Contact:
Distance measurement using HC-SR04
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?
BR
Tomas
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?
BR
Tomas
- 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
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. or inches (divide by 14.
‘ 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.
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. or inches (divide by 14.
‘ 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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- 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
Hi Ben
That's a nice site, indeedBenj 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.
Looks nice - I must try to "translate" this into FlowcodeBenj 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. or inches (divide by 14.
‘ 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.
- 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
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- 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
Thanks. I'll do thatBenj 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.
The module has now been bought - $3.41 including shipping, so I await it's arrival and will return after this!
BR.
Tomas
-
- 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
Wow that sounds like a bargain!TomasS wrote: The module has now been bought - $3.41 including shipping
hope it's all going to work for you.
With all the experts on here, (and me of course ) 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
- JohnCrow
- 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
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)
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 !
-
- 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
Hi Martinmedelec35 wrote:Wow that sounds like a bargain!TomasS wrote: The module has now been bought - $3.41 including shipping
hope it's all going to work for you.
With all the experts on here, (and me of course ) 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
Yep! Ebay is the place to find the stuff
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
-
- 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
Hi Ben. I have started working on my own ultrasonic detector, but was a bit puzzled about range of 10us and the distance.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)
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.
Reason: Worked out why 5.8 is used instead of 2.9.
Martin
-
- 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
Hi JohnJohnCrow 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)
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!
BR.
Tomas
-
- 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
I wasn't including myself in the expert listTomasS wrote:
I'm pleased ´to find all you experts on here - for sure!
Sure, as you can see from my post above, I have already started, and querying the calculations.TomasS wrote:
Sounds great - maybe we can compare solutions, when I receive my version?
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.
If you go to this website: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!
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
-
- 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
medelec35 wrote:I wasn't including myself in the expert listTomasS wrote:
I'm pleased ´to find all you experts on here - for sure!
Sounds great! I'll look forward to itmedelec35 wrote:Sure, as you can see from my post above, I have already started, and querying the calculations.TomasS wrote:
Sounds great - maybe we can compare solutions, when I receive my version?
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.
Done!medelec35 wrote:If you go to this website: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!
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