Sense Distance with PING)))
The PING))) Ultrasonic Distance Sensor measures the round-trip echo time of ultrasonic sound to determine how far away an object is. It can measure distances anywhere from 3 centimeters to 3 meters. In addition to being a great distance sensor for robots, it’s also useful for detecting people passing through doorways, viewing art exhibits, approaching holiday props, and lots more.
Want to go a bit more in-depth? Then be sure to check out this reference article about the speed of sound vs. air temperature, and learn how the temperature can actually affect distance measurements from your PING))) sensor.
Circuit
The PING))) Ultrasonic Distance Sensor only needs three connections to do its job: I/O pin, 5 V, and GND.
Parts
(1) PING))) Ultrasonic Distance Sensor (#28015)
(1) 2 k-ohm resistor (red-black-red, #150-02020)
- Build the test circuit shown here.
Different Board? if you are using a different Propeller board, build the equivalent circuit using the schematic.
Servo Ports? Another option is to set the Activity Board’s P15 servo port power jumper to 5V, and then connect the PING))) sensor to the port with a 3-pin F/F extension cable. There is a 3.9 k-ohm resistor in series between the signal pin in each servo port and its Propeller I/O pin, so no additional resistor would be needed.
Test Code
The test code displays the distance in centimeters to an object placed in front of the sensor.
- Click SimpleIDE’s Open Project button.
- Open Test Ping Distance from … DocumentsSimpleIDELearnExamplesDevicesSensorPing))) Ultrasonic Distance.
- Click the Run with Terminal button.
- Place an object in front of the PING))) sensor and verify that the cmDist measurements change appropriately as you move the object closer and farther away.
How it Works
After the PING))) sensor receives a low-high-low Start pulse from the Propeller, it makes a brief ultrasonic chirping sound. Since it’s ultrasonic, humans cannot hear it, but it’s loud enough that its own ultrasonic transducer can detect when the echo comes back.
As soon as the PING))) sensor makes its chirp, it sets its output pin high. When the echo comes back, it sets the pin low. The Propeller measures how long the PING))) sensor holds this pin high, and that number is the round trip time for the sound to return.
The ping library has functions that take care of sending the start pulse and measuring the echo time pulse. It also has functions that use the speed of sound in air to convert the echo time to a centimeter distance.
The ping library was added to this project by clicking the Add Simple Library button, and navigating to …DocumentsSimpleIDELearnSimple LibrariesSensor. Then, select the libping folder, and click the Select Folder button. After adding it to your project you’ll have access to all of the the ping library’s functions.
In this program’s while(1) loop, int cmDist = ping_cm(15) makes a PING))) sensor connected to P15 report echo time. It stores that time as a microsecond measurement and then uses the speed of sound in air to convert to a centimeter measurement. int cmDist = ping_cm(15) copies the centimeter result the ping_cm function returns to a variable named cmDist. After that, print(“cmDist = %dn”, cmDist) displays this value along with a newline character so that the next measurement appears on the next line in the SimpleIDE terminal. After a 1/5 second delay made possible by pause(200), the loop repeats to display the next distance measurement.
/* Test Ping Distance.c Measure and display Ping))) Ultrasonic Distance Sensor distance measurements. */ #include "simpletools.h" // Include simpletools header #include "ping.h" // Include ping header int main() // Main function { while(1) // Repeat indefinitely { int cmDist = ping_cm(15); // Get cm distance from Ping))) print("cmDist = %dn", cmDist); // Display distance pause(200); // Wait 1/5 second } }
Did You Know?
The speed of sound in air is 344.8 meters per second at room temperature (about 72 °F or 22.2 °C). That’s c = 344.8 m/s, T = 72 ºF or 22.2 ºC.
The distance (s) sound travels over a certain time is the product of the speed of sound and time. That’s s = c × t. If you are solving for round trip, the sound has to go twice as far, which takes twice as long, so divide your result by 2. That’s s = c × t ÷ 2. There are 100 centimeters in 1 meter, and 1,000,000 microseconds in 1 second. That’s 1 m = 100 cm and 1 s = 1,000,000 µs.
So, if you have an echo time in microseconds and you want a distance measurement in centimeters it’s:
0.01724 happens to be really close to 1/58. The ping library uses s = t ÷ 58 because integer math saves memory and gets that job done more quickly than floating point.
Try This
Here is an example that uses the centimeter distance measurement to control the brightness of the LED connected to P27. (You can use an LED on any available Propeller I/O pin. For a refresher on how brightness control works, see Set Volts.)
This particular example has a 25 cm range. The closer the object gets, the dimmer the light gets.
- Use the Save Project As button to save a copy of your project in …DocumentsSimpleIDEMy Projects.
- Modify the main function as shown below.
- Run the program and verify the output.
This program restricts the measurement to a maximum of 25 with if(cmDist > 25) cmDist = 25. Then, cmDist is multiplied by 10, since the dac_ctr function expects a value from 0 to 255. Finally, dac_ctr(27, 1, cmDist) takes the scaled-up cmDist value to control the LED with a range from 0 to 250 — almost the full range of LED brightness. cmDist is re-initialized with the sensor measurement ping_cm(15) each time the loop repeats.
Your Turn
- Modify your program to control the blinking rate of P26.
- Modify your program to make the light get brighter when the object gets closer. Hint: cmDist = 25 – cmDist; will invert your measurement, but you’ll have to do it before you multiply by 10.
(1) PING))) Ultrasonic Distance Sensor (#28015)
(1) 2 k-ohm resistor (red-black-red, #150-02020)
- Build the test circuit shown here.
Different Board? if you are using a different Propeller board, build the equivalent circuit using the schematic.
Servo Ports? Another option is to set the Activity Board’s P15 servo port power jumper to 5V, and then connect the PING))) sensor to the port with a 3-pin F/F extension cable. There is a 3.9 k-ohm resistor in series between the signal pin in each servo port and its Propeller I/O pin, so no additional resistor would be needed.
Test Code
The test code displays the distance in centimeters to an object placed in front of the sensor.
- Click SimpleIDE’s Open Project button.
- Open Test Ping Distance from … DocumentsSimpleIDELearnExamplesDevicesSensorPing))) Ultrasonic Distance.
- Click the Run with Terminal button.
- Place an object in front of the PING))) sensor and verify that the cmDist measurements change appropriately as you move the object closer and farther away.
How it Works
After the PING))) sensor receives a low-high-low Start pulse from the Propeller, it makes a brief ultrasonic chirping sound. Since it’s ultrasonic, humans cannot hear it, but it’s loud enough that its own ultrasonic transducer can detect when the echo comes back.
As soon as the PING))) sensor makes its chirp, it sets its output pin high. When the echo comes back, it sets the pin low. The Propeller measures how long the PING))) sensor holds this pin high, and that number is the round trip time for the sound to return.
The ping library has functions that take care of sending the start pulse and measuring the echo time pulse. It also has functions that use the speed of sound in air to convert the echo time to a centimeter distance.
The ping library was added to this project by clicking the Add Simple Library button, and navigating to …DocumentsSimpleIDELearnSimple LibrariesSensor. Then, select the libping folder, and click the Select Folder button. After adding it to your project you’ll have access to all of the the ping library’s functions.
In this program’s while(1) loop, int cmDist = ping_cm(15) makes a PING))) sensor connected to P15 report echo time. It stores that time as a microsecond measurement and then uses the speed of sound in air to convert to a centimeter measurement. int cmDist = ping_cm(15) copies the centimeter result the ping_cm function returns to a variable named cmDist. After that, print(“cmDist = %dn”, cmDist) displays this value along with a newline character so that the next measurement appears on the next line in the SimpleIDE terminal. After a 1/5 second delay made possible by pause(200), the loop repeats to display the next distance measurement.
/* Test Ping Distance.c Measure and display Ping))) Ultrasonic Distance Sensor distance measurements. */ #include "simpletools.h" // Include simpletools header #include "ping.h" // Include ping header int main() // Main function { while(1) // Repeat indefinitely { int cmDist = ping_cm(15); // Get cm distance from Ping))) print("cmDist = %dn", cmDist); // Display distance pause(200); // Wait 1/5 second } }
Did You Know?
The speed of sound in air is 344.8 meters per second at room temperature (about 72 °F or 22.2 °C). That’s c = 344.8 m/s, T = 72 ºF or 22.2 ºC.
The distance (s) sound travels over a certain time is the product of the speed of sound and time. That’s s = c × t. If you are solving for round trip, the sound has to go twice as far, which takes twice as long, so divide your result by 2. That’s s = c × t ÷ 2. There are 100 centimeters in 1 meter, and 1,000,000 microseconds in 1 second. That’s 1 m = 100 cm and 1 s = 1,000,000 µs.
So, if you have an echo time in microseconds and you want a distance measurement in centimeters it’s:
0.01724 happens to be really close to 1/58. The ping library uses s = t ÷ 58 because integer math saves memory and gets that job done more quickly than floating point.
Try This
Here is an example that uses the centimeter distance measurement to control the brightness of the LED connected to P27. (You can use an LED on any available Propeller I/O pin. For a refresher on how brightness control works, see Set Volts.)
This particular example has a 25 cm range. The closer the object gets, the dimmer the light gets.
- Use the Save Project As button to save a copy of your project in …DocumentsSimpleIDEMy Projects.
- Modify the main function as shown below.
- Run the program and verify the output.
This program restricts the measurement to a maximum of 25 with if(cmDist > 25) cmDist = 25. Then, cmDist is multiplied by 10, since the dac_ctr function expects a value from 0 to 255. Finally, dac_ctr(27, 1, cmDist) takes the scaled-up cmDist value to control the LED with a range from 0 to 250 — almost the full range of LED brightness. cmDist is re-initialized with the sensor measurement ping_cm(15) each time the loop repeats.
Your Turn
- Modify your program to control the blinking rate of P26.
- Modify your program to make the light get brighter when the object gets closer. Hint: cmDist = 25 – cmDist; will invert your measurement, but you’ll have to do it before you multiply by 10.