Available via license: CC BY 3.0
Content may be subject to copyright.
Journal of Physics: Conference Series
PAPER • OPEN ACCESS
Application of ultrasonic sensor for measuring distances in robotics
To cite this article: V A Zhmud et al 2018 J. Phys.: Conf. Ser. 1015 032189
View the article online for updates and enhancements.
This content was downloaded from IP address 178.171.48.216 on 22/05/2018 at 01:47
1
Content from this work may be used under the terms of the Creative Commons Attribution 3.0 licence. Any further distribution
of this work must maintain attribution to the author(s) and the title of the work, journal citation and DOI.
Published under licence by IOP Publishing Ltd
1234567890 ‘’“”
International Conference Information Technologies in Business and Industry 2018 IOP Publishing
IOP Conf. Series: Journal of Physics: Conf. Series 1015 (2018) 032189 doi :10.1088/1742-6596/1015/3/032189
Application of ultrasonic sensor for measuring distances in
robotics
V A Zhmud1, N O Kondratiev1, K A Kuznetsov1, V G Trubin1, L V Dimitrov2
1Novosibirsk State Technical University, 20, Karla Marksa Ave., Novosibirsk,
630073, Russia
2Technical University of Sofia, Sofia, 8, Kliment Orhidski Boulevard, Bulgaria, 1756
E-mail: zhmud@corp.nstu.ru
Abstract. Ultrasonic sensors allow us to equip robots with a means of perceiving surrounding
objects, an alternative to technical vision. Humanoid robots, like robots of other types, are,
first, equipped with sensory systems similar to the senses of a human. However, this approach
is not enough. All possible types and kinds of sensors should be used, including those that are
similar to those of other animals and creations (in particular, echolocation in dolphins and
bats), as well as sensors that have no analogues in the wild. This paper discusses the main
issues that arise when working with the HC-SR04 ultrasound rangefinder based on the
STM32VLDISCOVERY evaluation board. The characteristics of similar modules for
comparison are given. A subroutine for working with the sensor is given.
1. Introduction
Robotic devices are very relevant and various. The development of such devices is important also for
training of the future engineers and scientist. It is very important for students to be able to develope
robots independently, beginning from simple robots, using cheap sensors and driving devices [1–8].
This demands the use of modern methods resulting from the developments in the field of automatics
and in various relative fields of knowledge, such as identification, techniques for measuring, and
others.
Most robots require accurate information about the location of surrounding objects. Optical
sensors (including laser sensors and sensors based on the stereoscopic effect with the help of two or
more photo or video cameras with additional illumination or without it) are most effective for this
purpose, as well as sound locators and electromagnetic locators. Electromagnetic location is most
effective at large distances to objects and if the objects are electrically conductive. For the perception
of closely located objects, ultrasound location and optical sensors are most suitable. This article is
devoted to ultrasonic sensors.
2. Principle of Operation and Selection of Sensors
The principle of the ultrasonic rangefinders is to measure the time during which that signal propagates
at the distance from the transmitter to the receiver. The propagation speed of the signal is known. The
ultrasonic range finder HC-SR04 is considered in this paper.
The sensor consists of a transmitter that generates ultrasonic waves, a receiver that perceives the
echo, and auxiliary nodes for normal operation of the module.
2
1234567890 ‘’“”
International Conference Information Technologies in Business and Industry 2018 IOP Publishing
IOP Conf. Series: Journal of Physics: Conf. Series 1015 (2018) 032189 doi :10.1088/1742-6596/1015/3/032189
The ultrasonic rangefinder HC-SR04 is shown in Figure 1, where the transmitter and the receiver
are designated as T and R, respectively. The rangefinder generates sound waves at a frequency of
40 kHz. The sound waves reflects from the object and returns to the receiver, the sensor gives the
information about the time, which was demanded to sound waves for propagation from the sensor to
the object and back. This process can be clearly seen in Figure 2.
Figure 1. Ultrasound rangefinder HC-SR04
Figure 2. Movement of the ultrasonic signal
from the transmitter to the receiver
The ultrasonic signal is propagated by a wave directed at an angle of 30°. The direction of
propagation of the ultrasonic signal from the transmitter is shown in Figure 3. The most effective
measuring angle is 15°. External objects falling under this measurement angle interfere with the
determination of the distance to the desired object.
The readings of the ultrasound range finders are not affected by sunlight or the color of objects, as
is the case with infrared sensors. The ultrasonic wave is reflected from almost any surface, even
transparent, but it can be difficult to determine the distance to fluffy or small objects. In addition, the
readings are affected by the angle of incidence of the wave. If the sensor is directed perpendicular to
the object, the measurements will be most accurate. In addition, if, the angle of incidence is too large,
then the wave, reflected from the object, does not enter the receiver, which will lead to an incorrect
measurement.
Figure 3. The ultrasonic wave pattern
Figure 4. Movement of the ultrasonic wave
from the transmitter to the receiver at an angle
Next, let us calculate the distance from the sensor to the object. The sensor itself does not count
anything on its own, but only gives an impulse of certain duration. All calculations must be made by
the microcontroller.
3. Practical Relations and the Accuracy of Measurement Obtained
Calculation of the distance is based on the obtained time. It is calculated using the following
relationship:
; / 2 / 2S vt t T S vT
. (1)
Here, υ is speed of sound (≈ 340 m/s); t is time of motion of the wave from the sensor to the object; T
is time of motion of the wave from the sensor to the object and back.
3
1234567890 ‘’“”
International Conference Information Technologies in Business and Industry 2018 IOP Publishing
IOP Conf. Series: Journal of Physics: Conf. Series 1015 (2018) 032189 doi :10.1088/1742-6596/1015/3/032189
Division by two is necessary because the signal passes the distance to the object and back, then
when only the distance to the object is required. This paper uses a timer to measure the duration of a
signal. The timer is adjusted so that 1 digit is 0.000001 s. Therefore, the equation will look like this:
6
/ 2 10 / 2
im
S vT S vT
. (2)
Here,
im
T
is the number of timer counts in microseconds.
The final equation will look like this:
66
340 10 / 2 170 /10
im im
S T T
. (3)
In order to represent this distance in centimeters, it is necessary to multiply it by 100:
3
17 /10
cm im
ST
. (4)
As a result, using one simple equation, the distance to the object in centimeters can be calculated.
0.017
cm im
ST
. (5)
However, the microcontroller does not provide operations on floating-point numbers. Therefore, to
calculate the distance, it is necessary to use equation (4). Alternatively, the division can be replaced by
the multiplication by the reciprocal value:
/58.8
cm im
ST
, (6)
The User Guide [11] has similar equation; however, the value of denominator in it is 58.
The above formulas are sufficient for a correct measurement of the distance, but if there is a need
to improve the accuracy of the measurement, it is necessary to take into account a number of factors.
First, it is desirable to take into account the ambient temperature in which the measurements are
made. This is because the speed of sound in gases increases with increasing temperature. With an
increase in air temperature by 1°C, the speed of sound in it increases by 0.6 m/s. The graph of the
dependence of the speed of sound on temperature is shown in Figure 5, and Table 1 shows some
values. To be able to quickly adapt to changing ambient temperature, it is necessary to add a
temperature sensor to the measurement system and to use the results of its measurements. Some
rangefinders have inbuilt temperature sensors.
Figure 5. Dependence of sound velocity on
temperature
Figure 6. Geometric representation of sound
wave propagation
Table 1. Speed of sound at a certain temperature
t, °С
-20
-10
0
10
20
30
υ, m/s
318.8
325.1
331.5
337.3
343.1
348.9
4
1234567890 ‘’“”
International Conference Information Technologies in Business and Industry 2018 IOP Publishing
IOP Conf. Series: Journal of Physics: Conf. Series 1015 (2018) 032189 doi :10.1088/1742-6596/1015/3/032189
Secondly, it is necessary to take into account the propagation path of the signal. From the
geometric point of view, finding the distance from the sensor to the object of measurement is the
finding of the height of an isosceles triangle h (Figure 6). Here the length a should be calculated with
the help of equation (5) or (6). To find the height h let us turn to the Pythagorean theorem:
2 2 2
( / 2)h a b
(7)
Here, h is the distance from the sensor to the object (the height of an isosceles triangle); a is the
distance traveled by the sound wave from the transmitter to the object or from the object to the
receiver (the triangle side); b is distance from the transmitter to the receiver (base), for HC-SR04 b = 3
cm.
Because base b is rather small, it will have a significant influence at small values of a, in other
words, when measuring small distances. For an example, let us take for h the minimum and maximum
possible distance of the sensor measurement, this is 2 cm and 400 cm.
For h = 2 cm:
22
2 3 / 4 4 2.25 2.5a
For h = 400 cm:
22
400 3 / 4 160000 2.25 400.0028a
Based on the calculations, there are the following: when measuring the real 2 cm to the object, the
sensor will show 2.92 cm (46% error), and when measuring 400 cm, the error is only 0.0015%. To
compensate for this error, the distance found by formula (6) must be transformed into a real one:
22.25
hcm cm
SS
,
Hence, the measuring error is 0.3 mm.
4. Connecting of Ultrasonic Distance to the microcontroller STM32
To get started, one should familiarize oneself with the characteristics of this module:
• Supply voltage +5 V;
• Consumption in silent mode 2 mA;
• Consumption at work of 15 mA;
• Measurement range from 2 to 400 cm;
• Effective measuring angle 15°;
• The dimensions are 45×20×15 mm.
The board has 4 outputs:
• Vcc - power supply +5 V;
• Gnd - "earth";
• Trig - to start the sensor;
• Echo - to obtain the measurement results.
When the connection is done, one should take into account that the microcontroller these findings
must maintain the ability to work with 5V levels [11]. In the documentation, this output is marked as
FT.
The sensor operates as follows:
Initially, the "Echo" and "Trig" outputs are set to "0". In order to start the measurement process, it
is necessary to send a "Trig" output, a pulse with duration of 10 μs. Repeat measurement can be done
no earlier than 50 ms. After that, the transmitter receives 8 short pulses to generate an ultrasonic wave.
After the generation of a series of sound waves, the output "Echo" is set to "1". After reflecting a
series of waves from the object and returning to the receiver, the output of "Echo" is set to "0". The
5
1234567890 ‘’“”
International Conference Information Technologies in Business and Industry 2018 IOP Publishing
IOP Conf. Series: Journal of Physics: Conf. Series 1015 (2018) 032189 doi :10.1088/1742-6596/1015/3/032189
result is a pulse, the duration of which is equal to the time spent on passing the sound from the sensor
to the object and back. The duration of this pulse lies in the range from 118 μs to 24 ms. Knowing this
time one can determine the distance. In Figure 7, a timing chart of the range finder signals is shown.
Figure 7. Timing chart of the range finder signals
Figure 8. The signal supplied by the
microcontroller to the input "Trig"
The user can calculate the pulse width from the output "Echo" using STM32 in various ways. In
this paper, let us propose a method of measuring with the help of a timer, without interrupts. To do
this, one must configure one of the timer channels in capture mode. In this mode, the timer records the
duration of the input pulse in cycles in the counting register.
To generate the triggering signal to "Trig", the developer must configure any other contact to be
output. The adjusted output forms a pulse with duration of 10 μs.
In this example, the authors used the STM32VLDISCOVERY evaluation board based on the
STM32F100RBT6B microcontroller using the SPL library. To work with the rangefinder, the first
TIM4 timer channel was used. The code is given below.
To verify that the sensor is working correctly, consider the oscillograms of real signals from the
module. The pulse that triggers the sensor is shown in Fig. 8.
To test this, one calculates the theoretical value of time and compare it with the real signal from the
sensor. Based on the equation (1), one calculates the pulse duration, when measuring distances of 30
cm and 100 cm.
/2 2 /S vT T S v
. (8)
At 30 cm:
6
2 0.3/340 1764 10T
= 1764 μs. At 100 cm:
6
2 1/ 340 5882 10T
= 5882 μs.
Fig. 9 and 10 show the actual duration of the pulses, when measuring distances of 30 cm and 100
cm and compare it with the theoretical. Also it shows the trace change in pulse width from the output
"Echo", depending on the distance measured.
Figure 9. The signal at the output "Echo" with a
duration proportional to the distance of 30 cm
(red) and the signal at the input "Trig" (blue)
Figure 10. The signal at the output "Echo" with
a length proportional to the distance of 100 cm
(red) and the signal at the input "Trig" (blue)
6
1234567890 ‘’“”
International Conference Information Technologies in Business and Industry 2018 IOP Publishing
IOP Conf. Series: Journal of Physics: Conf. Series 1015 (2018) 032189 doi :10.1088/1742-6596/1015/3/032189
Figure 11 clearly shows the operation of the sensor, from the moment the trigger pulse is applied,
until the generation of a series of ultrasonic waves and the formation of a time pulse. As can be seen,
the real picture of the signals is similar to the signals in Fig. 6, from the documentation.
Figure 11. View of three signals taken from the input "Trig" (blue), from the module when generating
a sound signal (black) and from the output "Echo" (red)
5. Similar Devices, Accessories and Additional Periphery
In addition to HC-SR04, there are other ultrasonic sensors. However, their differences are
insignificant. Each, equipped with a transmitter and receiver, have an identical measurement principle,
the physical dimensions of the devices are almost identical. Various parameters are such as: the
maximum possible measurement distance, accuracy. Some sensors support specialized interfaces for
pairing them with microcontrollers. Let us consider some of them.
Ultrasonic sensor HY-SRF05 (see Figure 12), a more expensive improved model, compared to HC-
SR04. The main difference, according to the manufacturer, is the maximum possible measurement
distance of -450 cm. In addition, one more "OUT" pin was added to the board; however, the
functionality of this "output" could not be established.
The ultrasonic sensor US-015 (see Figure 13), according to the manufacturer, performs
measurements with greater accuracy than HC-SR04. In addition, the price of this device is slightly
higher, because the sensor was less popular. In connection with the non-distribution of the module, it
is difficult to establish the genuine characteristics of the device. For example, the accuracy in different
sources is indicated as 0.1 mm or 0.3 mm. Pin arrangements are exactly the same as for HC-SR04. The
similarity of the characteristics of the sensors makes them interchangeable.
Figure 12. Ultrasound range finder HY-SRF05
Figure 13. Ultrasound range finder US-015
The following sensor URM37 provides the connectivity on a variety of interfaces: Serial or PWM,
TTL or RS232. Also, different modes of operation are provided: determination of the distance on
request or permanent scanning. In addition, a temperature sensor is installed on the board, which is
used to correct the readings of the rangemeter, as discussed earlier, the sound speed at different
ambient temperatures is different.
In addition to the considered sensors, there are specialized modules that, working in conjunction
with an ultrasonic rangefinder, expand the functionality of the system. For example, module RCW
0012 (see Figure 15) independently performs all necessary calculations, thereby losing the need to
connect the microcontroller STM32 or any other. In addition, the built-in three-digit seven-segment
indicator allows one to immediately display the distance to the object in centimeters. The board is
7
1234567890 ‘’“”
International Conference Information Technologies in Business and Industry 2018 IOP Publishing
IOP Conf. Series: Journal of Physics: Conf. Series 1015 (2018) 032189 doi :10.1088/1742-6596/1015/3/032189
based on the microcontroller STC 11 f-04e. There is a 4-pin connector on the board, in which an
ultrasonic module is installed.
Another module that can perform these tasks is DYP-ME008. In parallel, the developers released
their own ultrasonic sensor DYP-ME007, suggesting their joint work. A bundle of these two devices is
shown in Figure 16. A comparison of the characteristics of these ultrasonic sensors and some others is
given below, in Table 2.
The Octasonic 8 x HC-SR04 Ultrasonic Breakout Board (see Figure 17), which is a private
development of the "Super Awesome Robots!", allows the use of several ultrasonic sensors
simultaneously (meaning in one system). The developer suggests using his device as an unusual
synthesizer.
The next important question is the fixing of the module on the robot, because ultrasonic rangefinder
is often used as a "sight" of robots. For these purposes, there are specialized cases available. To
provide the possibility of turning the ultrasonic module, there are fasteners for connection with the
servo drive.
Figure 14. Ultrasound range finder URM37
Figure 15. Module RCW 0012
Figure 16. Module DYP-ME008 and ultrasonic
rangefinder DYP-ME007
Figure 17. Module Octasonic 8 x HC-SR04
Ultrasonic Breakout Board
Figure 18. Mounts for ultrasound module
8
1234567890 ‘’“”
International Conference Information Technologies in Business and Industry 2018 IOP Publishing
IOP Conf. Series: Journal of Physics: Conf. Series 1015 (2018) 032189 doi :10.1088/1742-6596/1015/3/032189
Table 2. Comparative table of characteristics of ultrasonic sensors
HC-
SR04
HY-
SRF05
US-015
US-100
URM37
GH-311
DYP-
ME007
Supply voltage, V
+5
+5
+5
+5
+5
+5
+5
Consumption in silent
mode, mA
2
2
2,2
2
-
-
-
Consumption at
work, mA
15
15
20
15
20
-
15
Measurement range,
cm
2 - 400
2 - 450
2 - 400
2 - 450
4-500
2 - 300
2 - 500
Effective measuring
angle, gradus
15
15
15
15
15
15
15
Ultrasound
frequency, kHz
40
40
40
40
40
40
40
Accuracy, mm
0,3
0,3
0,1 +1%
0,1
1
-
0,3
Dimensions, mm
45×20×1
5
44×20×1
4
45×20×1
2
44×26×1
4
51×22×2
2
46×20×1
8
45×20×15
Average price for the
end of 2017, rubles
≈ 50
≈ 70
≈ 80
≈ 155
≈ 1000
≈ 520
≈ 240
Notes: Some characteristics of the sensors from different open sources may vary; references to the sources used are given in
the LITERATURE section; “-” means unknown parameters;
6. Conclusions
Ultrasonic rangefinder HC-SR04 is a good and cheap module for measuring distance, in comparison
with its analogues.
The measurement process is fairly simple and straightforward, and the interface between the sensor
and the microcontroller does not require specialized interfaces.
The accuracy of measurements is influenced by many factors that should be considered. This is the
ambient temperature, the propagation path of the signal, the angle of incidence of the wave, the shape
and size of the object. However, even taking into account all the errors, it was not possible to achieve
the accuracy stated by the developer.
If one needs the most accurate measurements, one should use laser rangefinders. And ultrasound is
better suited, for example, as motion sensors at short distances or as a vision of a robot, to find objects
around them.
There are modules that are designed specifically for ultrasonic sensors. Using these modules allows
one to work with sensors without connecting to the microcontroller, because it can independently
perform calculations and display results.
When using any sensors, it is necessary to remember the general rules for their selection and
application [9–20].
References
[1] Sablina G V, Stazhilov I V, Zhmud V A. 2016 Development of rotating pendulum stabilization
algorithm and research of system properties with the controller (APEIE–2016): Proc. 13 Intern.
Sci.-Techn. Conf., Novosibirsk.
[2] Zhmud V A, Trubin V G, Fiodorov D S, Ivoilov A Yu. 2015. Development of Deflection Angle
Stabilizing System for Balancing Robot (Journal of Advances in Management Sciences &
Information Systems) 1 65–82
[3] Gayvoronskiy S A, Ezangina T, Khozhaev I. 2016 Analysis and synthesis of dual mode control
systems of a tethered descent undersea vehicle OCEANS 2016 - Shanghai, article № 7485341
[4] Gayvoronskiy S A, Ezangina T, Khozhaev I. 2017 Analysis of Interval Control System Robust
Quality Indices on the Base of Root and Coefficient Methods IOP Conference Series: Materials
Science and Engineering, 235 (1) 012016
9
1234567890 ‘’“”
International Conference Information Technologies in Business and Industry 2018 IOP Publishing
IOP Conf. Series: Journal of Physics: Conf. Series 1015 (2018) 032189 doi :10.1088/1742-6596/1015/3/032189
[5] Gayvoronskiy S A, Ezangina T, Khozhaev I. 2017 The analysis of permissible quality indices
of the system with affine uncertainty of characteristic polynomial coefficients. 22nd
International Conference on Methods and Models in Automation and Robotics, MMAR 2017,
article № 8046801, 73-77
[6] Ezangina T A, Gayvoronskiy S A 2014 The synthesis of the robust stabilization system of cable
tension for the test bench of weightlessness simulation. Advanced Materials Research 1016,
394-399
[7] Gayvoronskiy S A, Ezangina T, Khozhaev I. 2015 The interval-parametric synthesis of a linear
controller of speed control system of a descent submersible vehicle. IOP Conference Series:
Materials Science and Engineering, 93 (1) 012055
[8] Gayvoronskiy S A, Ezangina T. 2015 The algorithm of analysis of root quality indices of high
order interval systems. Proceedings of the 2015 27th Chinese Control and Decision
Conference, CCDC 2015, article № 7162444 3048-3052
[9] User Guide: STM32F100x4 STM32F100x6 STM32F100x8 STM32F100xB.
http://www.st.com/content/ccc/resource/technical/document/datasheet/dd/87/fd/2a/fb/3f/48/5c/C
D00251732.pdf/files/CD00251732.pdf/jcr:content/translations/en.CD00251732.pdf
[10] User Guide: STM32VLDISCOVERY: UM0919 User Manual.
http://www.st.com/content/ccc/resource/technical/document/user_manual/f3/16/fb/63/d6/3d/45/
aa/CD00267113.pdf/files/CD00267113.pdf/jcr:content/translations/en.CD00267113.pdf
[11] User Guide: HC-SR04: Ultrasonic Ranging Module
https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf
[12] User Guide: HY-SRF05: Ultrasonic Ranging Module
http://dl.behnamrobotic.com/shop/datasheet/module/module-srf05-ultrasonic.pdf
[13] User Guide: US-015: Ultrasonic Ranging Module
http://akizukidenshi.com/download/ds/sainsmar/US-015Manul.pdf
[14] User Guide: US-100: Ultrasonic Ranging Module
http://tinkbox.ph/sites/mytinkbox.com/files/downloads/US_100_ULTRASONIC_SENSOR_M
ODULE.pdf
[15] User Guide: User Guide: URM37: Ultrasonic Ranging Module
http://files.amperka.ru/datasheets/urm37.pdf
[16] User Guide: GH-311: Ultrasonic Ranging Module https://goo.gl/qNTTLV
[17] User Guide: DYP-ME007: Ultrasonic Ranging Module https://goo.gl/W77pXL
[18] User Guide: DYP-ME008: Ultrasonic Ranging Module https://arduino-
kit.ru/userfiles/image/DYP-
ME008%20Rang%20Ranging%20Distance%20Detecting%20Ultrasonic%20Sensor%20Displa
y%20Module.pdf
[19] User Guide: Octasonic 8 x HC-SR04 Ultrasonic Breakout Boardboard
https://www.tindie.com/products/andygrove73/octasonic-8-x-hc-sr04-ultrasonic-breakout-
board/
[20] URL: https://goo.gl/NCkoAt