Range finder (Part 1)

Part 1, 2

A lot has been said and written about the HC-SR04 range finder so that the aim of this post is not to rewrite the same code sample again and again. The idea here is to get one step forward by looking at this sensor in more details and to try to tidy-up some confusing points;


The principle of operation of this sensor is based on the time needed for a sound to travel from a source to a destination sensor. A sound burst is generated by a piezoelectric element. This burst is made of a sine wave at 40 000 Hz. This burst travels in a sound cone of approximately 15° towards the target. As the base of the cone expends with the distance from the burst generator, the surface of the target must be large enough for echoing properly the signal. Next plot illustrates the relationship between the distance of the target and its diameter


Also, while flat surfaces are ideal for reflecting the sound burst, this sound burst may slip on an irregular surface and fail to echo the signal properly. The HC-SR04 makes provision for this type of situation and a timeout prevents the module from waiting and waiting, and waiting the echo.

 Once these parameters taken into account, the module proves to be working fine and is able to accurately measure distances ranging from few centimeters to few meters. The datasheets say 2 cm to 400 cm with a resolution of 3 mm. This distance is calculated from the signal produced by the HC-SR04. It consist in a positive square pulse which rising edge occurs when the module generates the sound burst and falling edge happens when the module reads the echo. Knowing the time needed for sound to travel in free air, it is easy to calculate the distance with the following formula: d = 1/2 * burst_travel_time * speed_of_sound. The travel time is halved as the burst has to travel back and forth.

Often neglected is the real speed of sound. Most publications talk about 340 m/s in free air. In reality, the speed of sound in free air – in other words, in earth’s atmosphere – the main factor affecting the speed of sound is the temperature. Next example shows the difference in distance estimates versus temperature.

At 10°C, a signal duration of 10 ms translates in a distance of 1.68 m, while the same pulse duration at 30°C translate in a distance of 1.75 m. So that the temperature parameter will have to be taken into account for accurate measurements. At 20°C, the speed of sound is 343 m/s.

Exercising the HC-SR04 in excellent conditions (stable temperature, concrete walls used as targets, stable measuring devices), the observed performances of the devices are slightly different. Principally in terms of short distances: 10 cm seems to be the shortest distance that the module can measure accurately. Even with oversampling and averaging techniques, 3 mm resolution looks fully theoretical, and I would not advise to build a critical equipment based on this specification. Talking about a 1 cm resolution sounds more reasonable.

In theory, running the HC-SR04 looks straightforward, and it is really as long as no adverse events contradict its measuring process. The measuring procedure is as follows:

  • Send a positive pulse which duration is at least 10 µs on the trigger pin
  • Just after that wake-up signal, the emitting piezoelectric transducer sends 8 ultrasound bursts and sets the echo line to the HIGH state
  • When the receiving piezoelectric element reads the echoed signal, the echo line is set to LOW state
  • Compute distance based on speed of sound in free air

If the receiving piezoelectric element fails to receive and echo within the next 38 µs, the HC-SR04 will timeout. Also, the datasheet specifies that the cylcle time should not be less than 60 ms.

This procedure looks plain simple, and most (not to say all) code samples match this simple approach by using a digitalWrite() HIGH and LOW, followed by a pulseIn() function with an appropriate timeout. Well the reality is not as plain trivial as we will see in the next posts…

Next post on same subject


Leave a Reply

You must be logged in to post a comment.