Monday , October 6 2025

STM32 HC-SR04 Ultrasonic Distance Sensor – VN49

STM32 example of how to use HC-SR04 Ultrasonic Distance Sensor. In this video, we will be using Timer gated mode, Timer Update Interrupt, trigger and commutation interrupts, global interrupts, GPIO, LCD and I2C. We will also be looking at HC-SR04 Library, LCD menu,  reading echo signal time duration (tx_us) in microseconds and distance (dx_cm) in centimeters.  You may download example source codes and the library by clicking the link below. Change default connections of HC-SR04 by using “hcsr04_userConf.h”. If you would like to use USART to transmit the sensor values, the parameter settings are the same as in previous video VN48.

Connections(default):

STM32F407 HC-SR04
5V Vcc
PD13(GPIO_Output) Trig
PE9(TIM1_CH1) Echo
GND Gnd
*TIM2 *it is used for 10us trigger delay
*PD15(GPIO_Output) *Blue led; it shows Echo signal status
LCD
PB7(I2C1_SDA) SDA
PB6(I2C1_SCL) SCL
5V VCC
GND GND
BUZZER
PD14(GPIO_Output) IO
3V VCC
GND GND
USB RS232 TLL
PA2(USART2_TX) Rx
PA3(USART2_RX) Tx

 

                                           <<SourceCode>> Download

 

Check Also

VN41 – Make a simple library

In this video, I will show you how to make a simple library while programming …