go_away

### Author Topic: Distance measurement: p18f4520 and PING))) Ultrasonic Sensor  (Read 2734 times)

0 Members and 1 Guest are viewing this topic.

#### Yr0gErG

• Jr. Member
• Posts: 18
##### Distance measurement: p18f4520 and PING))) Ultrasonic Sensor
« on: September 30, 2010, 03:18:18 AM »
Hi, I would like to make a C code for distance measurement using the C18 Compiler for MPLab. what would be the steps needed? would the following pseudocode do?

Make the I/O pin an Output
Bring LOW the  pin that the PING rangefinder is connected
Bring the  pin HIGH for 5 microseconds
Bring the  pin LOW
Make the I/O pin an Input
Wait until the pin goes HIGH
Use a timer to see how much longer it takes for the pin to become LOW
The time it took to become low is now our raw distance ( in microseconds)
Divide Raw Distance by two since it includes the time for a return trip of the sonar
Raw distance * 2257 is our distance in cm

Raw distance * 889 is our distance in inches

#### kidwidget

• Jr. Member
• Posts: 7
##### Re: Distance measurement: p18f4520 and PING))) Ultrasonic Sensor
« Reply #1 on: October 01, 2010, 05:15:39 PM »
That's the basic algorithm. I didn't check your math for the conversion of cm and inches though.

RockON!
World's crappiest website, www.robowidgets.net.
RockON!

#### Yr0gErG

• Jr. Member
• Posts: 18
##### Re: Distance measurement: p18f4520 and PING))) Ultrasonic Sensor
« Reply #2 on: October 01, 2010, 11:02:19 PM »
I'm trying to make it print the distance values on the debugger, any idea how to do that? im using a C18 compiler, i'm trying to stick with compiler for consistency purposes

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,166
##### Re: Distance measurement: p18f4520 and PING))) Ultrasonic Sensor
« Reply #3 on: October 02, 2010, 05:44:32 AM »
Cant comment on the compiler but the maths is wrong....

Sound travels at 1130ft/sec or 13,560in/sec or 73.7463uS per inch

Since we are detecting the echo then the sound needs to travel out and back so it will take twice  as long ie 147.4926254uS per inch or 58.06796274uS per cm.

If you have measured the duration in microseconds (us) then:
cm = us / 58.06796274
inches = us / 147.4926254
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk