Professional Documents
Culture Documents
Arduinoi
Arduinoi
Participate
Formatting guidelines
All recent changes
PmWiki
WikiSandBox training
Basic Editing
Documentation index
Contact: tim@leethost.com
Navigation
History
Background
Features
Download
Constructor
Methods
Examples
Information about this page
History
Release Date Changes
Added support for non-AVR microcontrollers. Can now set a new max
distance when pinging. Added support
1.8 07/30/2016 for ATmega16, ATmega32 andATmega8535 microcontrollers. Changed
convert_cm() and convert_in() methods to static members so you can
call them directly.
Removed Due and Zero compatibility because boards are 3.3 volts and
1.7 09/29/2015
not 5 volt tolerant.
Support for new Arduino boards and ultrasonic sensors. Better timeout
1.6 06/17/2014
method.
New ping_median() method does multiple pings and returns the median
1.5 8/15/2012
(digital filter).
1.4 7/14/2012 Interface with sensors using only one Arduino pin.
Rebuilt the ping timing code from scratch, now yields very accurate
1.2 5/25/2012
results.
1.1 5/16/2012 Uses port registers for ultra-fast and lean code.
Features
Works with many different ultrasonic sensor models: SR04, SRF05, SRF06, DYP-
ME007& Parallax PING)))™.
Option to interface with all but the SRF06 sensor using only one Arduino pin.
Doesn't lag for a full second if no ping echo is received like all other ultrasonic
libraries.
Compatible with the entire Arduino line-up (and clones), Teensy family (including
$19.80 96Mhz 32 bit Teensy 3.2) and non-AVR microcontrollers.
Ping sensors consistently and reliably at up to 30 times per second.
Timer interrupt method for event-driven sketches.
Built-in digital filter method ping_median() for easy error correction.
Uses port registers when accessing pins for faster execution and smaller code size.
Allows setting of a maximum distance where pings beyond that distance are read as
no ping "clear".
Ease of using multiple sensors (example sketch with 15 sensors).
More accurate distance calculation (cm, inches & uS).
Doesn't use pulseIn, which is slow and gives incorrect results with some ultrasonic
sensor models.
Actively developed with features being added and bugs/issues addressed.
Download
Download here: Download NewPing Library
Constructor
NewPing sonar(trigger_pin, echo_pin [, max_cm_distance]);
Example:
NewPing sonar(12, 11, 200);
Methods
sonar.ping(); - Send a ping, returns the echo time in microseconds or 0 (zero) if no
ping echo within set distance limit
sonar.ping_in(); - Send a ping, returns the distance in inches or 0 (zero) if no ping
echo within set distance limit
sonar.ping_cm(); - Send a ping, returns the distance in centimeters or 0 (zero) if no
ping echo within set distance limit
sonar.ping_median(iterations); - Do multiple pings (default=5), discard out of range
pings and return median in microseconds
sonar.convert_in(echoTime); - Converts microseconds to distance in inches
sonar.convert_cm(echoTime); - Converts microseconds to distance in centimeters
sonar.ping_timer(function); - Send a ping and call function to test if ping is complete.
sonar.check_timer(); - Check if ping has returned within the set distance limit.
timer_us(frequency, function); - Call function every frequency microseconds.
timer_ms(frequency, function); - Call function every frequency milliseconds.
timer_stop(); - Stop the timer.
Examples
Sample NewPing Sketch
1. #include <NewPing.h>
2.
3. #define TRIGGER_PIN 12
4. #define ECHO_PIN 11
5. #define MAX_DISTANCE 200
6.
7. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
8.
9. void setup() {
10. Serial.begin(115200);
11.}
12.
13.void loop() {
14. delay(50);
15. Serial.print("Ping: ");
16. Serial.print(sonar.ping_cm());
17. Serial.println("cm");
18.}
[Get Code]
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
First you have to define the Trig and Echo pins. In this case they are the pins number 9 and 10 on
the Arduino Board and they are named trigPin and echoPin. Then you need a Long variable,
named “duration” for the travel time that you will get from the sensor and an integer variable for the
distance.
In the setup you have to define the trigPin as an output and the echoPin as an Input and also start
the serial communication for showing the results on the serial monitor.
In the loop first you have to make sure that the trigPin is clear so you have to set that pin on a
LOW State for just 2 µs. Now for generating the Ultra sound wave we have to set the trigPin on
HIGH State for 10 µs. Using the pulseIn() function you have to read the travel time and put that
value into the variable “duration”. This function has 2 parameters, the first one is the name of the
echo pin and for the second one you can write either HIGH or LOW. In this case, HIGH means that
thepulsIn() function will wait for the pin to go HIGH caused by the bounced sound wave and it will
start timing, then it will wait for the pin to go LOW when the sound wave will end which will stop the
timing. At the end the function will return the length of the pulse in microseconds. For getting the
distance we will multiply the duration by 0.034 and divide it by 2 as we explained this equation
previously. At the end we will print the value of the distance on the Serial Monitor.
1. ;
2.
3. void setup() {
4. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
5. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
6. Serial.begin(9600); // Starts the serial communication
7. }
8.
9. void loop() {
10. // Clears the trigPin
11. digitalWrite(trigPin, LOW);
12. delayMicroseconds(2);
13.
14. // Sets the trigPin on HIGH state for 10 micro seconds
15. digitalWrite(trigPin, HIGH);
16. delayMicroseconds(10);
17. digitalWrite(trigPin, LOW);
18.
19. // Reads the echoPin, returns the sound wave travel time in microseconds
20. duration = pulseIn(echoPin, HIGH);
21.
22. // Calculating the distance
23. distance= duration*0.034/2;
24.
25. // Prints the distance on the Serial Monitor
26. Serial.print("Distance: ");
27. Serial.println(distance);
28. }
Modified Arduino Ping))) example to work with 4-Pin HC-SR04 Ultrasonic Sensor Distance Measuring
Module
hc-sr04.ino
/* HC-SR04 Sensor
https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-
133696
The circuit:
* VCC connection of the sensor attached to +5V
* GND connection of the sensor attached to ground
* TRIG connection of the sensor attached to digital pin 2
* ECHO connection of the sensor attached to digital pin 4
Original code for Ping))) example was created by David A. Mellis
Adapted for HC-SR04 by Tautvidas Sipavicius
This example code is in the public domain.
*/
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);