HCSR04 Ultrasonic sensor and STM32

In this project, we will be interfacing an HC-SR04 Ultrasonic sensor with the STM32 microcontroller. To visualise the output, UART communication is utilized to send the data over a cable to a connected PC or laptop. The result can then be observed through the console of the STM32 IDE.

HC-SR04 Interfacing

Create a new project by selecting "New" and then choosing "STM32 Project."


Please select the targeted board correctly (B-L072Z-LRWAN1) and click on Next.



Provide the project name and click on Finish.



Now we have a configuration page that looks like this.
Click on Pinout ->  Clear Pinouts



Now, on the left side, click on Connectivity.
Select USART2 and set the mode to Asynchronous.

PA2 should be configured as USART2_TX.
PA15 should be configured as USART2_RX.



Expand Timers and Enable TIM2

Select "Clock Source" and choose "Internal Clock"




Expand "System Core" and Enable RCC

Select High Speed Clock (HSE) and choose Crystal/Ceramic Resonator.




Set PB9 - GPIO_Output



Set PB8 - GPIO_Input


save it. It will generate the code.

Make changes in the main.c file based on the below code.



Right click on the Project -> Project Properties -> C/C++ build -> Settings -> MCU GCC Linker -> Miscellaneous -> click on add button in the Other flags -> add this "-u _printf_float"



Build the project




Debug the project 





Open the command shell console to view the output.



Click on the resume button.
The distance is shown in the console window.




Comments

Popular Posts