Beruflich Dokumente
Kultur Dokumente
Home
Buy
Download
Products
L earning
Forum
Support
Blog
L OG IN
SIGN UP
Output
Input
User Interface
Storage
Communication
Power supplies
General
Libraries
Tutorials
Navigation
-
History
Background
Features
Download
Constructor
Methods
Examples
pdfcrowd.com
History
Release
Date
Added support for non-AVR microcontrollers. Can now set a new max
Initiatives
Arduino People/Groups & Sites
1.8
07/30/2016
Exhibition
Formatting guidelines
PmWiki
WikiSandBox training
Basic Editing
Documentation index
Removed Due and Zero compatibility because boards are 3.3 volts and
1.7
09/29/2015
1.6
06/17/2014
1.5
8/15/2012
1.4
7/14/2012
1.3
6/8/2012
1.2
5/25/2012
1.1
5/16/2012
1.0
5/15/2012
Initial Release.
Participate
-
Project Ideas
Languages
Changes
Rebuilt the ping timing code from scratch, now yields very accurate
results.
Background
When I first received an ultrasonic sensor I was not happy with how poorly it performed. I
open in browser PRO version
pdfcrowd.com
soon realized the problem wasn't the sensor, it was the available ping and ultrasonic
libraries causing the problem. The NewPing library totally fixes these problems, adds many
new features, and breathes new life into these very affordable distance sensors.
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.
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".
Doesn't use pulseIn, which is slow and gives incorrect results with some ultrasonic
sensor models.
pdfcrowd.com
Download
Download here: Download NewPing Library
Put the "NewPing" folder in "libraries\".
In the Arduino IDE, create a new sketch (or open one) and select from the menubar
"Sktech->Import Library->NewPing".
Constructor
NewPing sonar(trigger_pin, echo_pin [, max_cm_distance]);
Example:
NewPing sonar(12, 11, 200);
This initializes NewPing to use pin 12 for trigger output, pin 11 for echo input, with a
maximum ping distance of 200cm. max_cm_distance is optional [default = 500cm].
Methods
-
sonar.ping(); - Send a ping, returns the echo time in microseconds or 0 (zero) if no ping
echo within set distance limit
pdfcrowd.com
sonar.ping_in(); - Send a ping, returns the distance in inches or 0 (zero) if no ping echo
within set distance limit
sonar.check_timer(); - Check if ping has returned within the set distance limit.
Examples
Sample NewPing Sketch
1. #include <NewPing.h>
2.
3. #define TRIGGER_PIN 12
4. #define ECHO_PIN
open in browser PRO version
11
pdfcrowd.com
pdfcrowd.com
15 // Number or sensors.
pdfcrowd.com
29.
30. NewPing sonar[SONAR_NUM] = { // Sensor object array.
31. NewPing(41, 42, MAX_DISTANCE),
32. NewPing(43, 44, MAX_DISTANCE),
33. NewPing(45, 20, MAX_DISTANCE),
34. NewPing(21, 22, MAX_DISTANCE),
35. NewPing(23, 24, MAX_DISTANCE),
36. NewPing(25, 26, MAX_DISTANCE),
37. NewPing(27, 28, MAX_DISTANCE),
38. NewPing(29, 30, MAX_DISTANCE),
39. NewPing(31, 32, MAX_DISTANCE),
40. NewPing(34, 33, MAX_DISTANCE),
41. NewPing(35, 36, MAX_DISTANCE),
42. NewPing(37, 38, MAX_DISTANCE),
43. NewPing(39, 40, MAX_DISTANCE),
44. NewPing(50, 51, MAX_DISTANCE),
45. NewPing(52, 53, MAX_DISTANCE)
46. };
47.
48. void setup() {
49. Serial.begin(115200);
50. pingTimer[0] = millis() + 75; // First ping start in ms.
51. for (uint8_t i = 1; i < SONAR_NUM; i++)
52.
open in browser PRO version
pdfcrowd.com
53. }
54.
55. void loop() {
56. for (uint8_t i = 0; i < SONAR_NUM; i++) {
57.
58.
59.
60.
61.
sonar[currentSensor].timer_stop();
62.
currentSensor = i;
63.
cm[currentSensor] = 0;
64.
sonar[currentSensor].ping_timer(echoCheck);
65.
66. }
67. // The rest of your code would go here.
68. }
69.
70. void echoCheck() { // If ping echo, set distance to array.
71. if (sonar[currentSensor].check_timer())
72.
73. }
74.
75. void oneSensorCycle() { // Do something with the results.
76. for (uint8_t i = 0; i < SONAR_NUM; i++) {
open in browser PRO version
pdfcrowd.com
77.
Serial.print(i);
78.
Serial.print("=");
79.
Serial.print(cm[i]);
80.
Serial.print("cm ");
81. }
82. Serial.println();
83. }
[Get Code]
By:
Tim Eckel
Share
pdfcrowd.com
NEWSLETTER
Enter your email to sign up
2016 Arduino
Copyright Notice
Contact us
pdfcrowd.com