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

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 echos may be received after this, but are ignored).
    4. The first echo 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.

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 RX (Green) are the internal signals of the burst being sent and the echos received.

Note that the RX signal has many pulses on it as there are many echos after the first echo is received. Even if you have one object to detect in an open field, the sound waves can be refelected off the object, a small portion picked up by the sensor, and the rest of the sensor reflects back some of the wave which gets reflected several times.

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
  • 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 defraction 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 and until it is not noticeable. See the diagram below showing the wave sent out and refelected 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 by the unit and anything behind the unit.

More echos will be received back as other parts of the wave hits other parts of the same object 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 we need to first know the speed of sound in air.

The speed of sound is variously quoted as

  • 330 m/s
  • 340 m/s
  • 343.2 m/s

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

  Speed Time/cm
To get round trip time per cm gives 330 m/s 60.6061 µs
340 m/s 58.8235 µs
343.2 m/s 58.2751 µs

For the rest of these pages and examples we will use the speed of 343.2 m/s, for your application 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 by the µs by a divisor in µs/cm. Considering accuracy of the devices and speed of calculations, to get to whole cm accuracy, chose a divisor of 58 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.

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.

Version 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 cloths 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 echos and echos of echos 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 recieve echo should be 30000 µ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 echos of the ultrasonic sounds wave have gone
  • With timeout of 30000 us (30 ms), it is recommended that new distance sensor readings are done no more frequently than 20 ms after last reading to ensure all echos have died away.

Back to top

The next Pages after this one cover

Interested in something similar contact sales.

© 2017 onwards by PC Services, Reading UK Last Updated: 1st February 2017
If you encounter problems with this page please email your comments to webmaster