PING) ) ) Ultrasonic Distance Sensor: Onic P Ressu Re Wa Ves Ping) ) ) SP Eaker
PING) ) ) Ultrasonic Distance Sensor: Onic P Ressu Re Wa Ves Ping) ) ) SP Eaker
object
ff
o
s
t
c
e refle PING)))
v
a
w
sound returns to
and
phone
o
r
c
i
m
Computing
Distance
Specificati
ons
POWER
0
1
2
3
4
5
RESET
3V3
5V
GND
GND
Vin
AREF
GND
13
12
PMW 11
PMW 10
PMW 9
8
7
PMW 6
PMW 5
4
PMW 3
2
TX 1
RX 0
Connection to an
Arduino
DIGITAL
ANALOG
Arduino Sketch
The Arduino triggers the PING))) by sending a 5ms pulse to the sensor through pin 7,
which is initially configured as an Arduino OUTPUT.
Immediately after sending this pulse, pin 7 is switched to an INPUT.
When the PING))) receives the 5ms pulse from the Arduino, it sends a 40kHz
(ultrasonic) burst of sound out its speaker and sets pin 7 to HIGH.
The PING))) then waits for the sound burst to reflect off of something and return to
the microphone where it is detected; the PING))) then sets pin 7 to LOW.
The Arduino uses the pulseIn command to measure the time of flight of the sound
wave in microseconds (the time that pin 7, when configured as an input, is HIGH).
The time of flight of the sound wave in ms is stored in the variable duration.
void setup() {
Serial.begin(9600); }
void loop()
{
long duration, inches;
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
delayMicroseconds(2);
digitalWrite(7, HIGH);
delayMicroseconds(5);
digitalWrite(7, LOW);
pinMode(7, INPUT);
duration = pulseIn(7, HIGH);
inches = duration / 74 / 2;
Serial.print(inches);
Serial.print("in ");
Serial.println();
}
Example
The picture shows how stiff wire (such as a coat hanger) can be used to mount the
Application
PING))) to an aluminum plate. An Arduino and breadboard are also mounted to the
plate, and a piezospeaker is installed on the breadboard to allow the device to output
an irritating noise whose frequency is proportional to the distance from the PING))) to
a target.
void setup() {pinMode(8, OUTPUT); }
void loop()
{
long duration, inches, tone_freq;
pinMode(7, OUTPUT);
// make pin 7 an output
digitalWrite(7, LOW);
// send wakeup pulse
delayMicroseconds(2);
digitalWrite(7, HIGH);
delayMicroseconds(5);
digitalWrite(7, LOW);
pinMode(7, INPUT);
duration = pulseIn(7, HIGH);
inches = duration / 74 / 2;
tone_freq = inches*100;
tone(8,tone_freq);
}
6