HC-SR04 Ultrasonic Range Finder

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package, from about 3cm to 300cm. Its operation is not affected by sunlight or black material like other kind of sensor, although acoustically soft materials like cloth can be difficult to detect – at least in the far range. It comes complete with ultrasonic transmitter and receiver module.

hc-sr04_module

Features:

  • Power Supply +5VDC

  • Quiescent Current less than 3mA

  • Working Current less than 20mA

  • Ranging Distance from 3cm  to 300cm

  • Resolution (with good repeatability) of 0.3 cm

  • Measuring Angle of 30°

  • Dimension 45mm x 20mm x 15mm

Let’s see how HC-SR04 sensor works

Like any other Sonar (originally SOund Navigation And Ranging) is a technique that uses sound waves propagation to detect object in the path. The principle is really simple: any object in the path will reflect back a portion of incident waves, if we are able to measure the time required by the waves to propagate and come back (known the speed of sound in the air, about 340m/s), we can calculate the distance with a simple formula:

Distance[meters] = (SpeedOfSound[340m/s] * Time[seconds])/2

Obviously have divided by a factor of 2 because the reflected waves travel two times: from transmitter to object and from object to receiver.

How to interface HC-SR04

Setting TRIG pin high for about 10uS triggers the sending of a short sequence of 8 sound waves at 40kHz frequency (above human listening capability) that probe the environment. Then the ECHO pin, after a short time, switches from low to high for a time proportional to the object’s distance (given by the formula above) and then returns to low. If no object is detected, the ECHO pulse width will be 38mS. TRIG and ECHO should work with TTL voltages (0-5Vdc), but I’m using 3.3V without troubles.

To debug the whole system and read out the sensor readings, I’ve used one of the STM32F051′s USART modules and an Arduino Mini has “USB-to-Serial Adapter” (i.e. burned with “Blink” example only).

A serial 470ohm resistor is used to avoid faults due to misconfiguration (for example configuring a GPIO as output that should be an input) and to protect in case of different voltage. This resistor is used between:

  • HC-SR04 TRIG pin and STM32F0-Discovery board
  • HC-SR04 ECHO pin and STM32F0-Discovery board
  • Arduino RX pin and STM32F0-Discovery boa
  • Arduino TX pin and STM32F0-Discovery board

hc-sr04_whole_sys_bb

Connections are simple:

  • Arduino GND to STM32F0-Discovery GND
  • Arduino TX (pin 0) to resistor to STM32F0-Discovery pin PA9
  • Arduino RX (pin 1) to resistor to STM32F0-Discovery pin PA10
  • HC-SR04 GND to STM32F0-Discovery GND
  • HC-SR04 VCC to STM32F0-Discovery 5V
  • HC-SR04 TRIG to resistor to STM32F0-Discovery pin PC6
  • HC-SR04 ECHO to resistor to STM32F0-Discovery pin PC7

The debug is made via serial interface (115200 bps, 8N1) (thanks to Arduino mini board) and will look like this

sr04_terminal

Source Code

Starting from a Standard Peripheral Library template (or, better, from the example provided in my other post), only one file is of interest: main.c

You can download directly from here: hc-sr04_code

I hope you enjoyed!

2 thoughts on “HC-SR04 Ultrasonic Range Finder

  1. Fabio Angeletti,
    Ho utilizzato 3 sensori HC-SR04 per realizzare una bilancia peso/volume. Ovviamente i 3 sensori mi sono serviti per ottenere le 3 dimensioni del collo e ricavare quindi il volume. La risoluzione di questi sensori non mi ha entusiasmato, ed ora ho acquistato un metro laser SNDWAY SW-S100 con interfaccia USB-HID.
    Ho estrema difficoltà a connettermi con questo strumento che viene rilevato con Vendor ID 0x0483 e Device Id 0x5750. In una delle ricerche su Stack Overflow ho visto che hai avuto problemi con questo stesso strumento. Sei in grado di darmi ulteriori delucidazioni, vorrei acquisire i dati in modo da ottenere una bilancia con risoluzioni superiori. Attualmente ottengo i dati dagli HC-SR04 e li elaboro con Python, ma credo che se supero lo scoglio di accedere ai dati con qualsiasi programma poi si possa gestirli anche con Python.
    Grazie in anticipo per qualsiasi informazione sei in grado di fornirmi
    Buona giornata

    • Ciao Antonio, non ho idea di quale strumento tu stia parlando, infatti non ho mai utilizzato un metro laser con interfaccia USB. Comunque considera che gli HC-SR04 hanno un’ottima risoluzione, inferiore a 5mm. Non ho idea delle specifiche del tuo progetto ma se i colli non hanno tutti gli angoli esattamente a 90° (molto probabile), una precisione superiore del sensore sarebbe inutile e verrebbe mascherata proprio da questa caratteristica.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.