Figure 1 Ultrasonic sensor, HC-SR504
The ability to estimate the distance using a sensor enables several applications:
Linear travel range
Liquid level indicator
This application note uses an ultrasonic sensor, HC-SR504, to illustrate these applications.
Bill of Materials
The table below summarizes the key components to build the circuit for the initial round of tests:
Part type Description Quantity
LED LED with legs, assorted colors 3
Resistor Resistor, 220Ω ±5% 3
UNO Arduino UNO Rev 3 1
Sensor Ultrasonic sensor, HC-SR504 1
BBH Breadboard, half-size 1
Wires DuPont connection wire, male-to-male, assorted colors and length 14
The schematic diagram for the initial round of tests is shown below:
Figure 3 Green LED on
The components may be assembled as shown in the figure below. Any unintentional obstruction of the signal path in front of the transmitter and receiver pair of the sensor (e.g. protruding wires) should be avoided.
The ultrasonic sensor, HC-SR504, has four pins. Viewing the sensor from front in elevation view, the pins are as follows from left to right:
VCC, +5V DC
Trigger for pulse, input
Echo detector, output
The diagram below summarizes the operation of the sensor:
When the state of the trigger pin changes from LOW to HIGH and lasts for more than 40 microseconds, the module’s piezo element transmits a burst of 8 cycles at 40kHz (approximately 200 microseconds total)
The duration of the echo for this burst at the receiver represents the time taken for the signal to travel from the piezo element to the reflecting object and then back to the echo receiver
Figure 5 Yellow LED on
The echo width is converted to a distance measurement using the basic formula based on the speed of sound.
The maximum echo width for the sensor is approximately 30 milliseconds which varies nominally depending on the manufacturer’s specifications for the sensor. The corresponding timeout value for the echo detection is 38 milliseconds which represents the state that no echo was received.
The best practice recommendation for this sensor is to not issue another trigger request until 60 milliseconds have elapsed since the previous request. It is also prudent to reset the echo detection (see example code) in the event of a timeout.
The equation for the speed of sound is:
a=speed of sound
γ,ratio of specific heats=1.4 for air at standard temperature and pressure
R,gas constant=286 m^2/(s^2 K)
A simpler derivation from the above equation is:
a=speed of sound,
This simpler derivation does not account for changes in the value of γ (heat capacity ratio) for barometric and hygrometric changes from the standard.
For the purposes of the current exercise, the speed of sound at 25°C and standard atmospheric conditions with no humidity is 346.3 meters/second. This value can be recast to 0.03463 centimeters per microsecond.
Since the pulse travels twice the distance between the sensor and sound reflecting object, the scale multiplier in effect is 0.01732.
There were two test cases for this assembly using:
The PulseIn method
The hardware external interrupt
This approach, as the name implies, relies on the Arduino library method called PulseIn to determine the duration of the pulse echo. Since there is no external trigger is applied, this approach relies on constant polling which may lead inefficient use of computing cycles in the general sense.
Hardware External Interrupt
This approach measures the elapsed time between the RISING and FALLING states of the echo signal on the assigned pin. While this approach is more efficient if nominal timeout considerations are built into the measurement.
Since the HC-SR504 may be sourced from a variety of sources, the quality of specific versions may not be exactly to the original specifications. For example, some models of this sensor require the echo pin to be reset if a timeout occurs. The unpredictability of this condition makes it prudent (a.k.a. defensive programming) to reset the echo pin through code as shown explicitly in the example for the PulseIn method.
This note examined two techniques to use an ultrasonic sensor to estimate the distance from the sensor to a sonar reflector. The success of the tests is manifested by the blinking of the LED devices in approximation to the estimated range. The references in the succeeding section provide further insight to the use of the ultrasonic sensor on Arduino boards.