Detect Distance with the Ping)))(TM) Ultrasonic Detector

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.

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
328 x 212 - 50K
579 x 221 - 35K
594 x 221 - 40K
603 x 246 - 20K
549 x 200 - 26K


  • 16 Comments sorted by Date Added Votes
  • edited March 2005 Vote Up0Vote Down
    ACTIVITY #1: Measuring Echo Time

    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.·

    [color=#008000]' PingTest.bs2[/color]
    [color=#008000]' {$STAMP BS2}[/color]
    [color=#008000]' {$PBASIC 2.5}[/color]
    time VAR Word
      [color=#0000ff]PULSOUT[/color] 15, 5
      [color=#0000ff]PULSIN[/color] 15, 1, time
      [color=#0000ff]DEBUG[/color] [color=#800080]HOME[/color], [color=#ff0000]"time = "[/color], [color=#000080]DEC5[/color] time
      [color=#0000ff]PAUSE[/color] 100


    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:
    · PULSOUT 15, 5
    · PULSIN 15, 1, time
    · DEBUG HOME, "time = ", DEC5 time
    · time = time ** 2250
    · DEBUG CR, "Distance = ", DEC4 time, " cm"
    · PAUSE 100
    Ö······· 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
    470 x 195 - 29K
    531 x 239 - 29K
  • salemsalem Posts: 3
    edited April 2006 Vote Up0Vote Down
    Hello..Thanks for sharing these valuable ideas ....I got a question..?
    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..??
  • edited April 2006 Vote Up0Vote Down
    This is a project that will take some planning. The first thing you'll want to do is draw a picture of where the beams will be directed in front of your vehicle. Then, write some code and test to see how long it takes a servo to turn to each of the directions you want to check. You will then know how many times per second you can check in front of you. With that bit of information you can then decide whether you can detect the obstacles in time while traveling at 8 MPH. If the answer is yes, then there are various downloads on the Ping))) product page <>; that you can use to get the sweeping sensor to work.

    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 <>. 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
  • computer guycomputer guy Posts: 1,113
    edited April 2007 Vote Up0Vote Down
    How would i use the ping sensor and a bs2/prop to measure the speed of sound, in meters/second?
    Preferably a BS2.

    Thank you smile.gif

    Post Edited (computer guy) : 4/3/2007 7:36:47 AM GMT
  • Steph LindsaySteph Lindsay Posts: 759
    edited April 2007 Vote Up0Vote Down
    You might want to take a look at Smart Sensors and Applications, which has a chapter for the Ping))) sensor - this thread was an early draft of the beginning of that chapter.· The book goes further into the way temperature affects the speed of sound, and gives a formula for the speed of sound in air at a given temperature.· You can download the book here:

    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.
  • HumanoidoHumanoido Posts: 5,770
    edited September 2007 Vote Up0Vote Down
    What PING)) code would you use with a faster Basic Stamp 2px?

  • sdysdy Posts: 40
    edited October 2007 Vote Up0Vote Down
    The faster BS2px requires a longer PULSOUT parameter to trigger the PING. Replace the 5 in PULSOUT with 13:

    PULSOUT 15, 13 'BS2p, BS2px & BS2sx
  • weglobalweglobal Posts: 2
    edited April 2009 Vote Up0Vote Down
    How can I have the ping sensing some from 2 inches to 10 inches with a acuracy of 1/8 of inches
  • Mike GreenMike Green Posts: 22,601
    edited April 2009 Vote Up0Vote Down
    You probably can't.

    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.
  • An NguyenAn Nguyen Posts: 10
    edited March 2010 Vote Up0Vote Down
    I have used the ultrasonic sensor to measure the distance from the sensor to object. It can detect the object up to 10ft far from the sensor at the hall way of LAB room,the distance result are very accurate,·but it not work properly in the LAB room with many computers, lightbulbs, and router. Can you tell me what "noises" affect to the ultrasonic transducer.
  • allanlane5allanlane5 Posts: 3,815
    edited April 2010 Vote Up0Vote Down
    In the LAB room, there's LOTS of ultrasonic reflective surfaces. It's probably not electrical or optical noise affecting the transducer, but echos bouncing around all those hard surfaces.

    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.
  • An NguyenAn Nguyen Posts: 10
    edited April 2010 Vote Up0Vote Down
    please tell me how the motion sensor light affect to the ultrasonic transducer. The ultrasonic transducer dosen't· work properly in the room have motion sensors.

    Thank you for your time.
  • allanlane5allanlane5 Posts: 3,815
    edited April 2010 Vote Up0Vote Down
    The 'ping' is not affected by light -- so, if you have IR based motion sensors, they won't affect the 'ping'.

    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?
  • An NguyenAn Nguyen Posts: 10
    edited May 2010 Vote Up0Vote Down
    Hi everyone,
    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.
  • Mike GreenMike Green Posts: 22,601
    edited May 2010 Vote Up0Vote Down
    You'll need to modify one of the existing sample programs for the PING))). The sample code shows how to read the sensor and produce a value for the distance to the nearest object detected. You'll have to test this for whatever you consider to be "close range" and generate the tone you want. The "What's a Microcontroller?" tutorial shows how to make sounds using a Stamp and a piezo-speaker. You can also read the chapter on the FREQOUT statement in the "Basic Stamp Syntax and Reference Manual". Both are included in the help files of the Stamp Editor.
Sign In or Register to comment.