Detect Distance with the Ping)))(TM) Ultrasonic Detector
![Andy Lindsay (Parallax)](https://forums.parallax.com/uploads/userpics/601/n66C2UR7NIHRG.jpg)
What is the Ping))) Sensor?
The Ping))) sensor is a device you can use with the BASIC Stamp to measure how far away an object is.· It's measurement range is from 3 cm to 3.3 m, and it's remarkably accurate, easily detecting an object's distance down to the half centimeter.
·
![attachment.php?attachmentid=73997](http://forums.parallax.com/attachment.php?attachmentid=73997)
How Does Ping))) Work?
Figure 2 shows how the Ping))) sensor sends a brief chirp with its ultrasonic speaker and makes it possible for the BASIC Stamp to measure how long the echo takes to return to the Ping))) sensor's ultrasonic microphone.· The BASIC Stamp has to send the Ping))) sensor a pulse to begin the measurement.· The Ping))) sensor then waits long enough for the BASIC Stamp program to start a PULSIN command.· Then, at about the same time the Ping))) sensor chirps its 40 kHz tone, it sends a high signal to the BASIC Stamp.· When the Ping))) sensor detects the echo with its ultrasonic microphone, it changes that high signal back to low.· The BASIC Stamp's PULSIN command measures the time between the high and low changes, and stores it measurement in a variable.· You can use this value and modify your program to use the speed of sound in air to calculate the object's distance in whatever measurement units you choose, centimeters, inches, feet, etc...
·
![attachment.php?attachmentid=73998](http://forums.parallax.com/attachment.php?attachmentid=73998)
·
![attachment.php?attachmentid=73999](http://forums.parallax.com/attachment.php?attachmentid=73999)
_____________________________________________________________________________
·
The draft material in this Topic·is part of a forthcoming Stamps in Class text by Andy Lindsay.
·
(c) 2005·by Parallax Inc - all rights reserved.··
Post Edited By Moderator (Jessica Uelmen (Parallax)) : 8/25/2010 5:46:05 PM GMT
The Ping))) sensor is a device you can use with the BASIC Stamp to measure how far away an object is.· It's measurement range is from 3 cm to 3.3 m, and it's remarkably accurate, easily detecting an object's distance down to the half centimeter.
·
How Does Ping))) Work?
Figure 2 shows how the Ping))) sensor sends a brief chirp with its ultrasonic speaker and makes it possible for the BASIC Stamp to measure how long the echo takes to return to the Ping))) sensor's ultrasonic microphone.· The BASIC Stamp has to send the Ping))) sensor a pulse to begin the measurement.· The Ping))) sensor then waits long enough for the BASIC Stamp program to start a PULSIN command.· Then, at about the same time the Ping))) sensor chirps its 40 kHz tone, it sends a high signal to the BASIC Stamp.· When the Ping))) sensor detects the echo with its ultrasonic microphone, it changes that high signal back to low.· The BASIC Stamp's PULSIN command measures the time between the high and low changes, and stores it measurement in a variable.· You can use this value and modify your program to use the speed of sound in air to calculate the object's distance in whatever measurement units you choose, centimeters, inches, feet, etc...
·
·
_____________________________________________________________________________
·
The draft material in this Topic·is part of a forthcoming Stamps in Class text by Andy Lindsay.
·
(c) 2005·by Parallax Inc - all rights reserved.··
Post Edited By Moderator (Jessica Uelmen (Parallax)) : 8/25/2010 5:46:05 PM GMT
Comments
In this activity, you will test the Ping))) sensor and verify that it gives you time measurements that correspond to the object's distance.· You will also modify the example program so that it converts the echo times to cm measurements.
Parts Required
All you'll need is a Ping))) sensor and three jumper wires to make it work.· The Ping))) sensor has protection against programming mistakes (and wiring mistakes) built-in, so there's no need to use a 220 Ω resistor between P15 and the Ping))) sensor's SIG terminal.
·
(1) Ping))) Ultrasonic Distance Sensor
(3) Jumper Wires ·
Ping))) Circuit
Figure 3 shows the schematic and wiring diagram we will use to test the Ping))) sensor.
·
Ö······· Build the circuit shown in Figure 3.·
·
Testing Ping)))
As mentioned earlier, the Ping))) sensor needs a start pulse from the BASIC Stamp to begin the measurement.· A pulse to P15 that lasts 10 μs (PULOUT 15, 5) is easily detected by the Ping))) sensor and only takes a small amount of time for the BASIC Stamp to send.· A PULSIN command that stores the duration of a positive pulse has to come immediately after the PULSOUT command.· The result that the PULSIN command stores in a variable is the round trip time for the Ping))) sensor's chirp to go to the object and back.· You can then multiply this value by .03434 to convert from 2 μs PULSIN Duration units to the distance in centimeters.· Activity #2 will explain how to derive this value.
Example Program - PingTest.bs2·
In this activity, you will measure the distances of a few close-up objects, so the·Ping))) sensor only needs to be roughly Boe-Bot height above your working surface (8 to 10 cm).· If you are measuring objects that are more than 30 cm away, keep your Ping))) sensor about half a meter or more above the floor.
·
Ö······· Place your Board of Education with the Ping))) sensor circuit on something to keep it at least 8 cm above the table surface.
Ö······· Place an object (like a water bottle, box, or paper target) 15 cm from the front of Ping.
Ö······· Enter, save, and run PingTest.bs2.
Ö······· The Debug Terminal should start reporting a value in the neighborhood of 450.· Values of 438 to 466 means the distance is between 15 and 16 cm.
Ö······· Move the target to a distance of 30 cm from the Ping))) sensor·and verify that the value of the time variable doubled.
Ö······· Point your Ping))) sensor·at a variety of near and far objects and see if the time values work.· To convert to centimeters, multiply your time measurement by .03434.· The next activity will explain how to derive this value using the speed of sound in air.·
·
Your Turn - Displaying Centimeter Measurements
The next activity will cover how to derive values that you can use with the ** operator to convert from the PULSOUT command's Duration value to centimeters, inches, feet, etc.· Here are a couple of lines you can add between PingTest.bs2's DEBUG and PAUSE commands to display the measurement in centimeters.
·
· time = time ** 2250
· DEBUG CR, "Distance = ", DEC4 time, " cm"
·
Ö······· Save PingTest.bs2 as PingCentimeters.bs2.
Ö······· Add the two lines of code to the program's DO...LOOP between the DEBUG and PAUSE commands.· When you're done, it should look like this:
·
DO
·
· PULSOUT 15, 5
· PULSIN 15, 1, time
· DEBUG HOME, "time = ", DEC5 time
· time = time ** 2250
· DEBUG CR, "Distance = ", DEC4 time, " cm"
· PAUSE 100
·
LOOP
·
Ö······· Run your modified program and verify that the program correctly displays both the echo time and centimeter measurements.
·
Post Edited By Moderator (Jessica Uelmen (Parallax)) : 8/25/2010 5:46:35 PM GMT
·
I made toy car which goes to 8 mph.. I need to have sensors which detect obstacles and move a round them...since this speed is fast for Infrared sensors which has short range detector·to detect and send signal to servo to react...I decided to use ultra-sonic sensors but my question is I have trouble coding the ultra-sonic sensors to·let the servo react..??
·
If it turns out that the sampling rate is not sufficient for a servo sweeping a Ping))) sensor, another thing you could look into is our Ping))) 5-pack <http://www.parallax.com/detail.asp?product_id=28015-5pack>. There is a coding trick you can use to check all 5 Ping))) sensors between each servo pulse. The trick is that you will be checking to find out whether there are any objects within the threshold distance. Here is an example that checks 4 Ping))) sensors connected to P4, P5, P6, and P7 for objects a little over 1.5 M out:
pingArray VAR Nib
.
.
.
OUTB = %1111
DIRB = %1111
PAUSE 1
OUTB = %0000
DIRB = %0000
PAUSE 10
pingArray = INB
The contents of the pingArray variable will contain four bits.· When the bit for a corresponding Ping))) is 0, then an object is closer than 1.5 m.
Post Edited (Andy Lindsay (Parallax)) : 4/11/2006 7:07:56 PM GMT
Preferably a BS2.
Thank you
Post Edited (computer guy) : 4/3/2007 7:36:47 AM GMT
http://www.parallax.com/detail.asp?product_id=28029
There is no example program for this specifically,but if you work with a known distance I think you could calculate it.·The Ping))) sensor·outputs a time measurement, which we know to be the·time it takes for an echo to emit and return.· The typical distance measurement program uses the·time as a·variable and·the assumed speed of sound at room temperature as a constant·to calculate distance. Now, if you make the distance·a constant by setting up the sensor and a target and measuring the distance with a ruler or tape, you could use this constant, and the time measurement·as a "stopwatch"·and measure the sound's speed.
humanoido
PULSOUT 15, 13 'BS2p, BS2px & BS2sx
The BS2 can't do the timing to get an accuracy of 1/8". The PING))) is specified for an accuracy of 0.5cm at best which is a bit more than 1/8". You may do better using a faster Stamp like a BS2px, but it's not clear that the PING))) is capable of the stability to get that kind of accuracy.
The solution might be to try to 'focus' either the emitter or the 'ear' sensor, perhaps with a length of heat-shrink tubing the right size.
Thank you for your time.
If, on the other hand, your "motion sensor lights" are in fact ultrasonic, it might be possible for them to 'confuse' the 'ping'.
It might help if you explained what you mean by "not working properly". Are the distances too short? Too long? Inconsistent?
i'm looking for a code for speaker when the ultrasonic distancce sensor detects object at close range, it gives a fast and high tone, and otherwise.
Thank you for your time.