"From Payroll to Body Scanners"
PC Services (Electronics)
Ultrasonic Distance Sensing using HC-SR04
Tel: 0118 946 3634
Introduction Ultrasonic Range Sensing
Since my original findings some more detailed analysis has revised my analysis and not be swayed by other people's analysis. Some parts have been changed and the change is shown.
This section covers mainly the physics of sound waves, to understand the limits of ultrasonics and how to use them, such items as
Other Pages after this one cover
So you have a microprocessor or computer and you are doing a project where you need to measure distances, and you have discovered you need or are using an Ultrasonic distance sensor for this, now what or why is it not working as you expected. So you have found this page amongst your searches to try and see how to do things.
No doubt from you searches and copying of examples you have a setup that is basically like the picture on the right, MCU (or microprocessor or Pi or Arduino or...) connected to an Ultrasonic module with 3 or 4 wires, and want to get this working.
First thing to understand is that the order of events is
This round trip or echo is also known as a PING from submarine and sea depth sensing using sound waves.
There is more detailed explanation of the signals on the page The circuitry of the HC-SR04
So far so good all seems simple enough, but
STOP AND STEP BACK !!
First of all understand or remember some of the physics of sound waves and the unit otherwise you may not be able to make sense of some of your readings and why it does not always work in every situation.
Things a distance sensor will NOT tell you
Sound waves are pressure waves as in varying air pressure at a point in time, travelling out. For this device the opening of the sender is a diffraction opening, so the wave at any one time is actually an arc that spreads out and gets flatter the further away it gets.
As the wave gets wider the energy in the wave is spread further and further until it is not noticeable. See the diagram showing the wave sent out and reflected back, consider the lines as the peak of the pulse transmitted.
As you will see by the time the wave is received only a small portion of the original wave gets directed at the receiver, the rest will be
So the further away the object being detected is, a smaller and smaller portion of the wave is detected, till eventually there is not enough of the wave energy being returned to be detected, giving a finite range for detection.
How Fast Sound Waves Travel
To use sound waves to measure a distance we need to time the period between sending out a sound wave and receiving the echo (the round trip), to then know how far away the object is. So we need to first know the speed of sound in air, which is variously quoted as
Realistically these variances are due to humidity, and temperature. Assumes STILL air (no winds).
For the rest of these pages and examples we will use the speed of 343 m/s @ 20°C, for our examples you may have to adjust for other factors.
To convert time of signal echo to an actual distance we need to divide the time measured in µs by a divisor in µs/cm. Considering accuracy of the devices and speed of calculations, to get to whole cm accuracy, choosing an integer divisor of 58 µs/cm should be sufficient and can be borne out by testing stationary objects at known measured distances. This way you can find what is best for you and keep maths to integer operations only.
Measurements for Comparison
Note that standard measurements and characteristics for the Ultrasonic transducers in all manufacturers datashhets are done at a FIXED distance of 30 cm. Unless otherwise stated this distance will be used for any examples. However many datasheets are done at varying temperatures some at 20 ° C and others at 25 ° C, where necessary the actual temperature at time of test will be noted.
For details of typical Ultrasonic Transducers see Prowave selection of products, these are likely to be the types used in most Ultrasonic modules especially the 16 mm diameter Open Type models, with 12 mm opening.
Basics of HC-SRO4 Ultrasonic Sensor Usage
The HC-SR04 is a cheap Ultrasonic Distance Sensor available all over the price very cheap and fairly reliable, however its accuracy is +/- 3mm. This accuracy means that anything less than 1 cm measurement is at best a guess.
Beware just because you have one reading, sound waves echo around areas so to be sure the next reading is valid, always wait for at least 20 ms before any further readings are taken.
Things to remember about using the HC-SR04 -
Software and Rounding
By far the best solution to getting consistent readings is by using hardware counters that save two values one each for rising and falling edge. Doing this in software adds what is known as jitter to your timings because of random delays on each reading between an edge occurring and being able to read a high or low level after it.
Whilst normally you want to be sure you don't lose resolution by rounding up, depending on your application, the accuracy is often quoted as +/- 3 mm for the devices, and the accuracy of how you time the Echo pulse coming back, You should consider -
General advice -
You may be able to find better ways to do this but for most applications saying something is closer than it really is is easier to deal with.
The next Pages after this one cover
Interested in something similar contact sales.
|© 2017 onwards by PC Services, Reading UK
|Last Updated: 17th May 2018
|If you encounter problems with this page please email your comments to webmaster