# Ultrasonic sensor for distance measurement

In following application we will use the Parallax PING))) Ultrasonic sensor.

Features:

• Provides precise, non-contact distance measurements within a 2 cm to 3 m range
• Simple pulse in/pulse out communication
• Burst indicator LED shows measurement in progress
• 20 mA power cosumption
• Narrow acceptance angle

Key Specifications:

• Power requirements: +5 VDC
• Communication:Positive TTL pulse
• Dimensions: 22 x 46 x 16 mm
• Operating temp range: 0 to +70 °C

### Principle of operation

The Ping sensor measures distance using sonar; an ultrasonic (well above human hearing) pulse is transmitted from the unit and distance-to-target is determined by measuring the time required for the echo return. Output from the PING))) sensor is a variable-width pulse that corresponds to the distance to the target.

The PING))) sensor detects objects by emitting a short ultrasonic burst and then "listening" for the echo. Under control of a host microcontroller (trigger pulse), the sensor emits a short 40 kHz (ultrasonic) burst. This burst travels through the air, hits an object and then bounces back to the sensor. The PING))) sensor provides an output pulse to the host that will terminate when the echo is detected, hence the width of this pulse corresponds to the distance to the target.

## Example program

int pingPin = 11;

void setup()
{
Serial.begin(9600);
}

void loop()
{
long duration;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse.

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): 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(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

Serial.print(duration);
Serial.print("us");
Serial.println();

delay(100);
}

### pulseIn()

Description

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds. Gives up and returns 0 if no pulse starts within a specified time out.

The timing of this function has been determined empirically and will probably show errors in longer pulses. Works on pulses from 10 microseconds to 3 minutes in length.

Syntax

pulseIn(pin, value) pulseIn(pin, value, timeout)

Parameters

pin: the number of the pin on which you want to read the pulse. (int)

value: type of pulse to read: either HIGH or LOW. (int)

timeout (optional): the number of microseconds to wait for the pulse to start; default is one second (unsigned long)

Returns

the length of the pulse (in microseconds) or 0 if no pulse started before the timeout (unsigned long)

## Connection

Interfacing to the microcontrollers is a snap: a single (shared) I/O pin is use to trigger the Ping sensor and "listen" for the echo return pulse. An onboard three-pin header allows the PING))) to be plugged into a solderless breadboard (on an Acrob, for example), and to be connected to its host through a standard three-pin servo extension cable.

## Sources of errors

### Air Temperature

Changing environmental conditions are the main source of the differences. Many quantities influences measurement including temperature, humidity, turbulences, pressure and damping. Main source of error is air circulation and temperature. Many equations appears in literature, two of them are mentioned here:
$(1) \qquad c = c_0 ( 1 + \gamma \vartheta )( 1 + A_V \eta )$

where

• $c_0$ - speed of the sound at 0° C and 0% RH (c0 = 331,5 [m/s]),
• $\gamma$ - temperature coefficient of the speed (@ 0° C), γ = 1.83 10-3 [°C-1],
• $A_V$ - humidity constatnt (AV = 2,2 . 10^-4, for 50 ÷ 200 kHz),
• $\eta$ - relative humidity of air [%],
• $\vartheta$ - air temperature [°C].

For practical purposes we can abandon humidity influences and then simplified formula applies:
$(2) \qquad c = c_0 + 0,6 \vartheta [m/s]$

The percent error over the sensor’s operating range of 0 to 70 ° C is significant, in the magnitude of 11 to 12 percent. The use of conversion constants to account for air temperature may be incorporated into your program.