Build and Test the Ping))) Sensor Circuit

In this activity, you will build the PING))) sensor circuit and use it to measure distances of a variety of objects to get familiar with what it does and does not detect.  The PING))) sensor just needs power, ground, and one signal connection for the Propeller to get distance measurements.

Parts List

  • (1) PING))) Sensor (shown below)
  • (1) 2.2 k-ohm resistor (red-red-red)
  • (1) jumper wire

 

Build the PING))) Sensor Circuit

  • If you haven't done so already, remove the touch-whisker circuits but leave the piezo speaker circuit in place.
  • Build the PING))) sensor circuit on your Propeller Activity Board WX as shown here.
  • Make sure to use a 2.2 kΩ resistor between the PING))) sensor’s SIG pin and the Propeller I/O pin (P8 in this case).

              

Ping))) Sensor and Servo Ports: When you are ready to design your own projects, you might want to mount the Ping))) sensor somewhere else on the robot. To make this easier, you can set the P16–P17 servo port power jumper to 5V, and then connect the sensor to one of those ports with a 3-pin extension cable.   Each servo port includes a 3.9 k-ohm resistor in series with its Propeller I/O pin, so you would not need to include the 2.2 k-ohm resistor that is needed for the breadboard circuit.

 

Ping))) Tests

The PING))) sensor's echo-location works on objects that can effectively reflect sound back at the sensor, from up to 3.3 meters away.  Small objects don't reflect back enough sound.  Hard-surfaced cylinders, ball-shaped obstacles, and walls if faced directly, work well.  Walls that are approached at glancing angle will simply reflect the sound further away instead of back at the PING))) sensor. Soft items such as curtains and stuffed animals tend to muffle the sound and not bounce the echo.

 

For obstacles that do reflect sound back, the PING))) sensor and ping library provide centimeter distance measurements in the 3 cm to 3 m range.  Air temperature can affect the accuracy of these distance measurements.  Check out this reference article about the speed of sound vs. air temperature to learn more about why this happens.

The test program will display the PING))) distance measurements in the SimpleIDE terminal.  You can use it to test how well the PING))) sensor can detect various objects, and walls at various angles, to get familiar with what your ActivityBot can and cannot do with this sensor. 

    

  • Click SimpleIDE’s Open Project button.
  • Open Test Ping from ...Documents\SimpleIDE\Learn\Examples\Robots\ActivityBot360. 
  • Click the Run with Terminal button.
  • Try measuring the distance to your hand, a cup, and a ball. All should be easy to detect.
  • Try measuring the distance to something soft, like a stuffed animal or a crumpled piece of fabric. Can the PING))) sensor detect an echo from it?
  • Try measuring a wall’s distance head-on.
  • Try measuring the distance to a wall, approaching at various angles.  How far from 90◦ can the PING))) sensor still see the object?

 

How it Works

The PING))) sensor requires a brief high/low signal called a start pulse from the Propeller chip to start the measurement.  The sensor then makes its ultrasonic click and sets its SIG pin high.  When the echo comes back, it sets its SIG pin low. The Propeller chip then can measure the duration of the PING))) sensor SIG pin's high signal, which corresponds to the echo return time. 

    

The ping library takes care of the math for converting the echo return time measurement to distance in centimeters. All your code has to do is request the distance with the ping_cm function, and pass to it the number of the Propeller I/O pin connected to the sensor.  In the test code, the function's return value gets stored in a variable named distance.  Then, it gets displayed in the SimpleIDE terminal with print(“%c distance = %d cm %c”, HOME, distance, CLREOL). This particular print statment puts the cursor in the top-left home position with HOME, displays the distance, and then CLREOL clears any characters to the end of the line.  CLREOL is useful if the previous measurement has more digits than the current measurement.  (If the program skipped that step, it would display cmm instead of cm after the measurement if it happens to be one less digit than the previous measurement. Try it if you want.)      

/*
  Test Ping.c

  Test the PING))) sensor before using it to navigate with the ActivityBot.

*/

#include "simpletools.h"                      // Include simpletools header
#include "ping.h"                             // Include ping header

int distance;                                 // Declare distance variable

int main()                                    // main function
{
  while(1)                                    // Repeat indefinitely
  {
    distance = ping_cm(8);                    // Get cm distance from Ping)))

    print("%c distance = %d%c cm",            // Display distance
           HOME, distance, CLREOL);           

    pause(200);                               // Wait 1/5 second
  }
}


Did You Know?

The ping library measures the echo pulse in terms of microseconds and then uses the fact that sound travels at 0.03448 cm/µs at room temperature.  That’s 3.448 hundredths of a centimeter per millionth of a second at a temperature of (22.2 ºC).  Just as a car travels distance = speed x time, so does sound, with an equation of s = ct, where s is the distance, c is the speed of sound and t is the time. 

    

To calculate the distance of an echo, you have to remember that the time measurement is for twice the distance (there and back), so the equation would be:

    

Divide both sides by 2, and you’ve got an equation for distance from a time measurement.


    

Since 0.03448/2 ≈ 1/58, the equation for cm distance from microsecond echo time becomes:

    

Check out our Reference page on the "Speed of Sound in Air v. Temperature" to learn how air temperature affects ultrasonic sensor readings.


Try This

You can display the raw microsecond time measurements with a call to the ping function.

    

  • Use the Save Project As button to save a copy of your project in …Documents\SimpleIDE\My Projects.
  • Modify the main function as shown below.
  • Run the program and verify the output.

    

 

Your Turn

  • Use your calculator to verify that the centimeter distance is the microsecond distance ÷ 58.  Keep in mind that C language int calculations always round down.