PC Services

 

PC Services (Electronics)

Ultrasonic Distance Sensing using HC-SR04

Tel: 0118 946 3634
Email

linked-in profile facebook profile

   The Company 
   Resources / Examples 
     Examples 
   -  Ultrasonic Distance Sensor 
   Commentaries           
   Personal 

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

Setup Overview

How connected to Microprocessor and signals

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

  1. Micro sends a trigger pulse to the unit to start a measurement, which INSIDE the unit causes the following steps
    1. Sends a burst of 8 x 40 kHz pulses via Ultrasonic sender
    2. Set the Echo output signal HIGH
    3. In the real world the sound wave is sent out and reflected back off objects and the FIRST reflection back (echo) is deemed as the NEAREST object (other echoes may be received further oscillations of the sensor RX and possibly TX, are seen after this, but are ignored).
    4. The first echo echo oscillations will hopefully at end of 8th cycle cause the unit to set the Echo output signal LOW
  2. Micro has to time how long the Echo signal from the unit is high to determine the time of the echo
  3. Micro can then convert time of echo to distance away, knowing the time is time to the NEAREST object and back again (round trip)

This round trip or echo is also known as a PING from submarine and sea depth sensing using sound waves.

Scope shot of signal timing
Scope shot of signal timing

Picture Left is a scope screen shot showing the external and internal signals for this process, where -

  • TRIG (Yellow) and ECHO (Magenta) are the signals between unit and micro,
  • TX (Cyan) and TX- (Green) are the internal signals of the burst being sent.

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

  • How Many objects there are
  • The Speed of any object
  • The Direction of travel for any object
  • The Size of any object (could be a bee, a bird or a brick wall)
  • The Angle of object from you
  • Distortion effects due to temperature, surface of object, winds, draughts, thermal currents etc. .
  • There is a Limited Range check what range yours can do

Back to top

Wave Propagation

Diagram of sound wave sent out and reflected back

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 reflected again scattered back and dissipated as the wave front is expanded, then passing the unit and going behind the unit.

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.

More echoes will Some extra echoes may be received back as other parts of the wave hits other parts of the same object and get reflected back as well.

Back to top

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

  • 331 m/s @ 0°C
  • 340 m/s @ 15°C
  • 343 m/s @ 20°C

Realistically these variances are due to humidity, and temperature. Assumes STILL air (no winds).

To get round trip time per cm
One way Round Trip
Temp Speed Time / metre Time / cm Time / cm
0°C 331 m/s 3.021148 ms/m 30.21148 µs/cm 60.42296 µs/cm
15°C 340 m/s 2.941176 ms/m 29.41176 µs/cm 58.82363 µs/cm
20°C 343 m/s 2.915252 ms/m 29.15452 µs/cm 58.30904 µs/cm

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.

Back to top

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.

Revision of HC-SR04 as of January 2017
Top View of HC-SR04 board
Top View of HC-SR04 board
Bottom View of HC-SR04 board
Bottom View of HC-SR04 board

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 -

  • Measuring distance is from the front grill of the sensors. So for accurate measurements the board must be exactly 90 degrees from (perpendicular) the object being measured.
  • More accurate measurements occur when the object distance is centred to the centre of the unit.
  • The minimum usable distance is 3 cm
  • Ultrasonics works better on hard objects with flat surfaces in the same orientation as the sensors as close to parallel as possible.
  • Soft materials, like clothes and people give strange results.
  • Slope of object to sensors will give inaccurate readings, how inaccurate you will have to determine.
  • The closer the object(s) are, or in a more confined space the more echoes and echoes of echoes there will be.
  • When mounting ensure sensor is raised from surface so no stray reflections occur from surface or ground to confuse sensor with distant object.
  • The usable cone is +/- 15 degrees (in 3 dimensions) of the centre of the PCB between the sensors, so beware that objects close by and to the side may not be found
  • The module provides a pulse that is TIME of flight in microseconds range of the sound wave out and back.
  • When measuring echo, it is suggested a TIMEOUT to receive echo should be 30,000 µs (30 ms) approx 5m. To ensure anything beyond 5m is ignored and not use too much software time up
  • If shorter TIMEOUT value used you still need to wait longer to ensure all echoes and oscillations of the ultrasonic sounds wave have gone
  • With timeout of 30,000 µs (30 ms), it is recommended that new distance sensor readings are done no more frequently than 20 ms after last reading to ensure all echoes and oscillations have died away.

Back to top

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 -

  • If you are trying to get distances to avoid things round DOWN so object appears slightly closer than it is.
  • You cannot detect on most platforms the rising edge of the Echo pulse, only it has gone high with some jitter on accuracy so timings are best guess ONLY.
  • Similarly on most platforms you cannot detect the falling edge, only at some point it has gone low.
  • Longer distance measurements are more prone to lack of accuracy from the physics of less sound wave energy is being returned as smaller portion of the total wave front hits the sensor. See Advanced Analysis for more details.

General advice -

  1. Stick to resolution of cm as anything less is full of noise and uncertainty (from real world, software and electronics).
  2. Round down to err on side of caution.

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.

  Back to top Simple Software example ---->

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