Library 30- Measure distance with HC-SR04 and STM32F4

I have at least 10 HC-SR04 sensors at home so I decided to made a library for it. This sensor can measure distance between 2cm and 4meters. It can produce about 40 measurements per second. His working angle is about 15degrees,

On the start, you have to send 10us starting pulse at TRIGGER pin. After that, on ECHO pin high pulse is returned in relation to the distance. Large distance means large high signal.

You have to give sensor some time after you make a conversion. If not, some measurements may be currupted.

Sensor can be used in a lot of projects, like autonomous robot or something like that.



  • Measure distance with HC-SR04 sensor
    • Number of sensors depends on available GPIO pins on STM32F4 device package
  • Checks if sensor is connected


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
  • TM
    • TM DELAY
    • TM GPIO
    • defines.h

Functions and structure

Two simple functions are used to work with sensor.


Example was tested on my all 3 STM32F4 boards. Everytime same result.

Project is available on my Github account, download library below.


Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...