by Giovanni
Carrera rev. 30/05/16
Ultrasonic ranging modules as the HC-SR04 are widespread
in the electronics and amateur robotics, but it is also important to know the
limitations of these sensors and the possible improvements of their accuracy. Before
introducing the measurement circuits and firmware, I dedicate a space to the
speed of sound and its propagation.The speed of sound in air
The sound waves propagate in a medium by means
of oscillatory displacements of its particles, around the rest position and
along the direction of propagation of the wave. The vibratory movements are transmitted
to neighboring particles and so on. The speed of propagation of sound waves
depends on the density of the medium, so it is almost nothing in the vacuum and
maximum in metals. The acoustic waves are generated by a source, whose
geometrical dimensions are in some way related to the wavelength, therefore the
inverse of the frequency generated. For this reason, the speakers used for the
bass of a stereo system are considerably larger compared to ultrasonic
transducers.
If we know the propagation velocity v we can calculate the wavelength l, the space occupied by a cycle:
v = space/time =
l/T = l×f Þ l = v/f
Indicating with T the period of the
oscillations of the particles, i.e. the duration of a wave cycle and the
inversion of the frequency (T= 1 / f).
In the case of a source from 40 kHz ultrasonic,
like that used, the wavelength is:
l = 344/40000 =
8.6 [mm]
For a distance meter, the wavelength is very
indicative, since it gives an idea of the spatial resolution and the size
that the objects must have to reflect these waves, that is, much larger thanl. These concepts are very similar to
those of the radar, which uses electromagnetic waves instead of acoustic waves.
The wavelength for typical nautical radar is about three centimeters. Using
acoustic waves at higher frequency would create problems because of the greater
absorption and therefore require significant peak powers of the transducers, which
are currently of the piezoelectric type. Also the echo sounder has the same
problem: to be able to detect small targets, such as fish, the frequency must
be increased from 30kHz, for bathymetry, to 400kHz for fishing, knowing that
the speed of sound at sea is about 1500 m/s, the respective wavelengths are: 5
and 0.375 cm.
Unlike marine transducers, there are major
problems in the air, due to the worst acoustic coupling of the transducer in
the air with respect to water.
The speed of the acoustic waves is not
constant, but depends on various factors such as temperature, humidity and
atmospheric pressure in the environment in which it operates. The most
significant is the temperature; a linear approximate formula is as follows:
v » 331.45 + 0.6 Tc
(1)
Where Tc is the temperature in degrees Celsius.
The previous formula is derived from the linearization of this more accurate expression:
Where:
·
R = universal gas constant = 8.314
[J/(mol×°K)]
·
T= absolute temperature in Kelvin degrees
[°K]
·
g
= adiabatic constant of gases
For dry air, g = 1.4 and M =
0.02895 [kg /mol], grouping constants, we obtain:
From which we see the dependence of the speed
with the square root of the temperature, expressed in Kelvin degrees. For
modest temperature variations do not have large differences, as we can see in
figure 1.
Figure 1 - Speed of sound versus temperature.
For our application, we can use the linear
formula (1), this is because we don’t expect large changes in temperature. Not
having the information of the ambient temperature, it is hypothesized that this
is fixed: Tc = 20 °C, so v = 343.85. In this case the percentage error on speed
is visible in the following table:
T
[°C]
|
-10
|
-5
|
0
|
5
|
10
|
15
|
20
|
25
|
30
|
35
|
40
|
Ev
|
-5.24%
|
-4.36%
|
-3.49%
|
-2.62%
|
-1.75%
|
-0.87%
|
0.00%
|
0.87%
|
1.75%
|
2.62%
|
3.49%
|
In our case, making a measurement, in summer,
at 35°C, we have an error of almost 10 cm at full scale (400). Measurements
more accurate require a temperature sensor to correct the speed.
Even the air humidity and the pressure
modifying the speed of sound: from 50% to 80% relative humidity it has a speed
increase of about 1.1% to 101,325 kP atmospheric pressure. The figure 2 shows
the variation of speed with relative humidity at various ambient temperatures
in Celsius degrees. You may notice that the humidity influence is most evident
at high temperatures, as could be expected.
Figure 2 - Speed of sound in air
All the considerations previously views lead to
say that the distance measurements based on the detection of the time of flight
(TOF) with ultrasound have an accuracy around the centimeter, if corrected with the ambient temperature.
Recent techniques are based on measurement of
the phase between continuous wave transmitted and received (Binary Frequency
Shift Keyed-or BFSK). With these techniques, using an FSK modulation with
frequencies of 40 and 41 kHz, we can achieve
an accuracy of the distance measurement below the millimeter. This
greater accuracy is paid with a considerable complexity of electronic circuits
and a higher energy consumption, because we have to generate a continuous wave.
An alternative is to use a laser rangefinder.
The HC-SR04 sensor
|
pin
|
function
|
Vcc
|
+5v Power supply input
|
Trig
|
trigger (TTL input)
|
Echo
|
Measurement (TTL output)
|
Gnd
|
Common ground
|
The module has a microcontroller that, when the
trigger pulse is received (10μs min), send a burst of 8 pulses at 40 kHz. A
MAX232 is used as driver for the Tx transducer in order to elevate the driving
voltage to approximately +/- 10V. The return echo, received by the Rx transducer,
is amplified by an LM324 and processed by the microcontroller that generates an
output pulse which duration Dt is proportional to the target distance. It
corresponds to the time that elapses between the emission of the burst and the
instant at which it receives the echo, also called TOF. The timing diagram
shows the sequences described.
Figure 4 -Timing diagram.
It follows that the distance Dt will be proportional to the length
of the output pulse according to the expression:
In our
case, expressing d in [cm] and Dt in [µS], we get:
d = ΔT / 58.165 [cm]
The measurement range of this
sensor is from a minimum of 2 cm to a maximum of 400, measured in microseconds
the Dt, these limits
correspond to 116 µs (2cm) and 23266 µs (400cm). With a system like Arduino it
is very easy to measure these times because there is a specific and powerful
function.
Teensy (or Arduino) interface and program
Four wires are required for
the connection to Teensy, as shown in figure 4. Changing only the signal pins,
any Arduino board or compatible can be used for this application, I used a
rev.3.1 Teensy interfaced with a LCD display. The figure 5 shows the wiring
diagram for my board.
Figure 5. - Wiring diagram for Teensy board.
As shown, the pin 22 of
Teensy is connected to the Trigger and pin 23 to Echo.
The code does not
require much effort, I add the definitions:
#define Trigger 22 // Trigger Pin
#define Echo 23 // Echo Pin
long duration, distance;
In the main loop it
creates the trigger pulse and then uses the pulseIn for Echo pulse duration:
digitalWrite(Trigger, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger, LOW);
duration = pulseIn(Echo, HIGH);
distance = duration/58.165;
A good choice for pulseIn is
to use a third parameter:
pulseIn(pin, value, timeout)
As default timeout = 1e6
microseconds, in our case, we might ask timeout = 23266, corresponding to 400cm
away. The function returns zero if timeout, or over range.
In the remaining of the
program there are instructions for printing on the LCD display the distance
measurement.
It would be advisable to check
the distances with calibration measurements: placing the sensor at known
distances from a flat wall, taking care to position the transducer axis
perpendicular to the wall. With three or four measures you can make a good
calibration.
As described earlier, a
significant improvement of the system is to measure also the temperature. There
are several temperature sensors for Arduino; some have the analog output, as
the LM35 which has a linear output with a scale factor of approximately 10 mV/°C.
Since Arduino has a 10-bit ADC, with full scale of 5 V it would be possible to
obtain a resolution of approximately 0.5 °C, more than good for the compensation
of the temperature using the formula (1). A more economic system, which
requires a better calibration, is to use a thermistor, i.e. a resistor that
varies its resistance with temperature. In the web there are many applications
of this type.
References
1. “HC-SR04 User's_Manual”, Cytron Technologies, May 2013.
2. “Speed of sound”, https://en.wikipedia.org/wiki/Speed_of_sound?oldid=722097430
3. “A high accuracy ultrasonic distance measurement system using binary frequency
shift-keyed signal and phase detection”, S. S. Huang, C. F. Huang, K. N.
Huang and M. S. Young, REVIEW OF SCIENTIFIC INSTRUMENTS VOLUME 73, NUMBER 10
OCTOBER 2002.
4. “LM35,
Precision Centigrade Temperature Sensors”, National Semiconductor, November 2000