STM32 with HC-SR04

Started 1st February 2016 - work in progress - correctness level low

The background to this page is my experience with the HC-SR04 ultrasonic ranging module using an ATMega 328p.

Having learnt to program the STM32F103C8T6 board with Arduino, I created some simple programs to use it with the HC-SR04. First with interrupts and then a timer capture register; the latter relies on the code from [1]. Compared to the ATMega 328p interrupt results are more stable.

There is the problem of matching the 5 V world of the HC-SR04 to the 3.3 V of the STM32. I started by using the safe option of a couple of FET level shifters[3].


However a sketch to use the HC-SR04 is included with the STM32 addition to the Arduino IDE, it can be found here:


or using the sketch menus to navigate to the same location.

I extracted the analogue final amplifier output signal from the HC-SR04 as described. To get the STM32 to digitise it I copied the ADC DMA code from [4].


  1. Maple frequency measurement - capture how to
  2. tone(), noTone() - timer device and channel from pin
  3. Bi-Directional MOSFET Voltage Level Converter 3.3V to 5V
  4. $10 O-Scope revisited - ADC code
  5. Spread Spectrum Phased Array Sonar

Click to return to index

Page last modified on October 17, 2016, at 12:52 PM
Powered by PmWiki