Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Ping Library for Arduino
Author:  Caleb Zulawski
Contact: caleb.zulawski@gmail.com


Navigation


Alternatives

The library below is a simple implementation that is easy to understand and great for simple testing. However, it is slow, is not meant to be run from interrupt code, and does not bound the pulseIn call by adding ", 38000" or whatever the appropriate maximum timeout (in microseconds) is for your sensor. In other words this code can introduce up to a 1 second delay as the worst case timeout to pulseIn.

If you need more efficient code that can be run from interrupts and can be used to talk to multiple sensors at the same time, look at NewPing from http://code.google.com/p/arduino-new-ping/


History

ReleaseDateChanges
1.0a2009-12-05Initial Alpha Release
1.12009-12-06Formal Release
Changed calibration format
1.22010-08-17Fixed incorrect files (Jesus Alonso)
1.32010-10-06Fixed keywords.txt (Alan Carey)
2.02012-04-11Updated for Arduino 1.0 (Cody Ketchum)

Description

Ping is a library for the Arduino.

The Ping library simplifies the usage of the Ping))) Ultrasonic Sensor with Arduino. It is an implementation of the concepts provided in the Arduino Ping))) tutorial.


Download

Download here: Ping-2_0.zip

Old Alpha Versions (Not compatible with alpha 0023 and older)

Ping-1_3.zip

Ping-1_2.zip

Ping-1_1.zip

Ping-1_0a.zip

Install and Import

Put the "Ping" folder in "hardware\libraries\".

In the Arduino IDE, create a new sketch (or open one) and select from the menubar "Sketch->Import Library->Ping".


Creation

Ping string name = Ping(int pin, double inMod, double cmMod);
Ping ping = Ping(13);
Ping ping = Ping(13,0,0);

This initializes a Ping sensor on pin 13. The optional inMod and cmMod arguments are used to correct erroneous output by the inches() and centimeters() functions. For example, if the inches() function returns a high value, increasing inMod would correct it, and vice versa. The same applies for the centimeters() function. The default values of inMod and cmMod are 0.


Methods

int microseconds()

This returns the amount of time in microseconds from when the sensor emits the sound to when the sound hits an object and returns to the sensor.

double inches()

This returns the distance in inches between the sensor and the nearest object in front of it.

double centimeters()

This returns the distance in centimeters between the sensor and the nearest object in front of it.


Example

SimpleReadout

  1. /*  This code initializes a Ping))) sensor on pin 13 and
  2. outputs the information over a serial connection */
  3. #include <Ping.h>
  4.  
  5. Ping ping = Ping(13,0,0);
  6.  
  7. void setup(){
  8.   Serial.begin(115200);
  9. }
  10.  
  11. void loop(){
  12.   ping.fire();
  13.   Serial.print("Microseconds: ");
  14.   Serial.print(ping.microseconds());
  15.   Serial.print(" | Inches ");
  16.   Serial.print(ping.inches());
  17.   Serial.print(" | Centimeters: ");
  18.   Serial.print(ping.centimeters());
  19.   Serial.println();
  20.   delay(1000);
  21. }


Information about this page

Last Modified: November 09, 2012, at 11:39 AM
By: marcmerlin