cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble integrating HC-SR04 with STM32F407 Discovery

Vulcan
Associate

Hi everyone,

I'm currently working on an obstacle-avoiding robot using the STM32F407 Discovery board and I'm running into issues with testing my HC-SR04 ultrasonic sensor.

Hardware setup:

  • TRIG connected to PA0 (configured as push-pull output)

  • ECHO connected to PA1 (configured for TIM2_CH2 input capture mode)

  • Using TIM2 to measure pulse width via input capture on Channel 2

  • Running the code bare-metal (no HAL or CubeMX)

The issue:

I tried having Chat walk me thru how to use UART but that didn't work, so I tried testing the sensor by debugging it and see if the value for my distance changed in the expression window.

When I tried running it, it wouldn't work, so I tried stepping over to see where it broke, and its stopping on line 118-119, right after the while loop. I had this issue earlier with the for loop on 113, but GPT was able to help me thru that one, but so far I am stuck on this part. 

I am doing this solo with GPT walking me thru it, so I am open to any and all pointers/advice.Appreciate for any help I could get. I basically just watched a beginner course off Udemy and dived right in. 

 

Thanks in advance

 

 

Screenshot 2025-07-15 183633.png

1 REPLY 1
mƎALLEm
ST Employee

Hello,

Before coding with bare metal use HAL at least to validate the function and your hardware.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.