Netduino home hardware projects downloads community

Jump to content


Photo

Library for HC_SR04 Ultrasonic Rangefinder


  • Please log in to reply
27 replies to this topic

#1 J Wilson

J Wilson

    Member

  • Members
  • PipPip
  • 28 posts

Posted 06 January 2012 - 03:23 AM

Someone might find this code useful. I found these HC SR04 ultrasonic rangefinders on amazon for $15 but they had no code. I found some Arduino code, but was pretty useless for Netduino because it used blocking for the timing. So I wrote my own that uses interrupts for the timing. We used this successfully on a mobile robot for detecting obstacles. Accurate to sub-inch against hard surfaces. It is calibrated for my altitude and climate, you may need to adjust the InchConversionFactor and LatencyTicks for your environment. If you want it to read centimeters, just add your own TicksToCentimeters function. Enjoy and feel free to improve it. 2012/04/03 - Corrected pin documentation on constructor

Attached Files



#2 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 01 February 2012 - 10:33 PM

Someone might find this code useful. I found these HC SR04 ultrasonic rangefinders on amazon for $15 but they had no code. I found some Arduino code, but was pretty useless for Netduino because it used blocking for the timing. So I wrote my own that uses interrupts for the timing.

We used this successfully on a mobile robot for detecting obstacles. Accurate to sub-inch against hard surfaces. It is calibrated for my altitude and climate, you may need to adjust the InchConversionFactor and LatencyTicks for your environment. If you want it to read centimeters, just add your own TicksToCentimeters function.

Enjoy and feel free to improve it.



I did!

Thanks for sharing!

#3 dlettiere

dlettiere

    New Member

  • Members
  • Pip
  • 1 posts

Posted 17 February 2012 - 03:14 PM

Someone might find this code useful. I found these HC SR04 ultrasonic rangefinders on amazon for $15 but they had no code. I found some Arduino code, but was pretty useless for Netduino because it used blocking for the timing. So I wrote my own that uses interrupts for the timing.

We used this successfully on a mobile robot for detecting obstacles. Accurate to sub-inch against hard surfaces. It is calibrated for my altitude and climate, you may need to adjust the InchConversionFactor and LatencyTicks for your environment. If you want it to read centimeters, just add your own TicksToCentimeters function.

Enjoy and feel free to improve it.


This is awesome!! thanks so much, out of curiosity, what was the altitude and climate you calibrated to,I'm located in Massachusets.

#4 dimitrivdw

dimitrivdw

    New Member

  • Members
  • Pip
  • 4 posts

Posted 02 April 2012 - 08:22 PM

Hi, I received a HC-SR04 sensor today and tried it with this code, but I don't seem to be getting any interrupts from the echo pin on the sensor. I connected 5V to the vcc pin and ground to ground. pin D6 is connected to echo and pin D7 is connected to trig, just as the demo code does. Do I need to plug the netduino into a standalone power supply instead of usb for this to work or is my sensor dead on arrival? Hopefully someone can help me? Thanks in advance!

#5 J Wilson

J Wilson

    Member

  • Members
  • PipPip
  • 28 posts

Posted 03 April 2012 - 12:55 AM

This is awesome!! thanks so much, out of curiosity, what was the altitude and climate you calibrated to,I'm located in Massachusets.

Western Colorado. Altitude about 4600' Climate: very dry.

Would be interested in seeing how different your calibration numbers are.

#6 J Wilson

J Wilson

    Member

  • Members
  • PipPip
  • 28 posts

Posted 03 April 2012 - 12:58 AM

Hi,

I received a HC-SR04 sensor today and tried it with this code, but I don't seem to be getting any interrupts from the echo pin on the sensor. I connected 5V to the vcc pin and ground to ground.

pin D6 is connected to echo and pin D7 is connected to trig, just as the demo code does. Do I need to plug the netduino into a standalone power supply instead of usb for this to work or is my sensor dead on arrival?

Hopefully someone can help me?

Thanks in advance!


Hmmm... I had it working either way. What are you using to reflect the sound?

#7 dimitrivdw

dimitrivdw

    New Member

  • Members
  • Pip
  • 4 posts

Posted 03 April 2012 - 05:32 AM

Hmmm... I had it working either way. What are you using to reflect the sound?


I tried my hand, a cardboard box, a piece of wood, nothing seems to put the echo pin high...

I tried to use pin D0 and D1 (+change the code of course), but that didn't help either.

Is there a way to check if this module is working, except for connecting it to the netduino?

#8 J Wilson

J Wilson

    Member

  • Members
  • PipPip
  • 28 posts

Posted 04 April 2012 - 03:08 AM

I tried my hand, a cardboard box, a piece of wood, nothing seems to put the echo pin high...

I tried to use pin D0 and D1 (+change the code of course), but that didn't help either.

Is there a way to check if this module is working, except for connecting it to the netduino?


Please forgive me. The problem is all mine. I modified the documentation and never tested it after the modification (I should know better than that).

Anyway, I had the pin documentation reversed for the constructor. Please download the updated code with the corrections. The first parameter is the pin connected to Trig. The second parameter is the pin connected to Echo. The names of the parameters are renamed to pinTrig and pinEcho (and I hope that will end all confusion).

Anyway, let me know if that does not fix your problem.

#9 dimitrivdw

dimitrivdw

    New Member

  • Members
  • Pip
  • 4 posts

Posted 04 April 2012 - 05:10 AM

Please forgive me. The problem is all mine. I modified the documentation and never tested it after the modification (I should know better than that).

Anyway, I had the pin documentation reversed for the constructor. Please download the updated code with the corrections. The first parameter is the pin connected to Trig. The second parameter is the pin connected to Echo. The names of the parameters are renamed to pinTrig and pinEcho (and I hope that will end all confusion).

Anyway, let me know if that does not fix your problem.


Hi. That would be great news. Will try it this evening... Never thought about trying that, actually...
Thanks! I'll let you know what the result is today or tomorrow!

#10 dimitrivdw

dimitrivdw

    New Member

  • Members
  • Pip
  • 4 posts

Posted 04 April 2012 - 05:26 AM

Couldn't wait until this evening. It works! This is amazing. First really cool thing I did with the Netduino. Thanks a lot!

#11 Steve French

Steve French

    Member

  • Members
  • PipPip
  • 10 posts

Posted 14 January 2013 - 03:02 PM

Could you elaborate a bit on how to calibrate the InchConversionFactor and LatencyTicks?  I'm using using it with a Netduino Go and I'm having no luck at all.

 

thank you.



#12 ccfoo242

ccfoo242

    Member

  • Members
  • PipPip
  • 16 posts

Posted 18 January 2013 - 02:21 AM

Thanks for the code. Had it up and running in just a few minutes. I'm planning to use this to surprise my cats when they get on my countertop!  :D



#13 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 18 January 2013 - 02:40 AM

Thanks for the code. Had it up and running in just a few minutes. I'm planning to use this to surprise my cats when they get on my countertop!  :D

 HAH, now that would be a great Netduino video to post :)



#14 Steve French

Steve French

    Member

  • Members
  • PipPip
  • 10 posts

Posted 21 January 2013 - 02:03 PM

[font="arial, helvetica, sans-serif;font-size:14px;color:rgb(0,0,0);"]@[/font][color=#000000;]ccfoo242[/color] were you using the Netduino Plus or the Netduino Go?  The more I experiment with this the more I think I have a bad 

[color=#000000;][font="arial, helvetica, sans-serif;"]HC_SR04.[/color][/font]

#15 emg

emg

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 21 January 2013 - 05:27 PM

 HAH, now that would be a great Netduino video to post :)

 

Yes, that's exactly what the internet needs, more cat videos.  B)



#16 GenEric

GenEric

    New Member

  • Members
  • Pip
  • 2 posts

Posted 28 February 2013 - 09:55 PM

Sir.. thank you!  :rolleyes:



#17 eplaksienko

eplaksienko

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts

Posted 13 March 2013 - 03:08 AM

I have problem with NGO and SB, it gives me values between 93 and 120



#18 eplaksienko

eplaksienko

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts

Posted 13 March 2013 - 11:42 AM

PS: It works well with arduino



#19 eizner23

eizner23

    Member

  • Members
  • PipPip
  • 13 posts

Posted 22 March 2013 - 02:22 PM

Thank you for the driver, works very well with Netuino 1.  I made myself a rear distance sensor for my auto :)

 



#20 Niyomal

Niyomal

    New Member

  • Members
  • Pip
  • 1 posts

Posted 26 March 2013 - 08:21 AM

sir can I use this code with FEZ Spider what are things need to be change to work this code with FEZ Spider using Extender module 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2010-2014 Secret Labs LLC  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.