You can access the source code files via this link. in this project
In this project, we delve into the seamless integration of the HC-SR04 ultrasonic sensor with STM32 microcontrollers, leveraging the advanced Timer functionality offered by the STM32 series. By harnessing the capabilities of STM32’s Timer peripherals, developers can significantly enhance the accuracy and efficiency of distance measurement applications. This article explores the comprehensive process of integrating the HC-SR04 sensor with STM32 microcontrollers, focusing on optimizing sensor performance through precise Timer control.
HC-SR04 PinoutThe pin configuration of the HC-SR04 sensor encompasses VCC, Ground, Trigger, and Echo pins. VCC and Ground serve to power the sensor, requiring a 5-volt supply for VCC and grounding for stable operation. The Trigger pin, functioning as an input, initiates distance measurement upon receiving a 10 µs pulse. Conversely, the Echo pin, an output, generates pulses whose width corresponds to the distance between the sensor and the detected obstacle.
The HC-SR04 Ultrasonic Distance Sensor emits 40, 000 Hz ultrasound waves, which bounce back if obstructed, allowing distance calculation based on the speed of sound.
To initiate the ultrasound burst, the Trig pin is set to a High State for 10 µs. Subsequently, the Echo pin detects reflections and remains High until the echo is received or timesout after 38ms.
Distance calculation is achieved using the formula Distance = (Speed x Time) / 2, where Time represents the duration the Echo pin remains High. This value, when multiplied by the speed of soundand divided by 2, yields the distance from the sensor to the object, enabling efficient obstacle detection in various applications.
To commence this project, we’ll initially activate TIMER1 in input capture direct mode. Following this, we’ll configure PA10 as the output TRIGER_PIN. Finally, we’ll establish UART communication to transmit both the ultrasonic value readings and system status.
STM32CubeMX Configuration:- Open CubeMX & Create New Project Choose The Target MCU STM32F103C6 & Double-Click Its Name
- Go To The Clock Configuration & Set The System Clock To 16MHz
- Configure The GPIO Pins PB10 as Output Pin
- In the Categories tab, select the TIM1
- set Channel 2 to Input Capture direct mode
- In the Parameter settings tab, set the (Prescaler=16-1 & Counter Peroid 65535)
- Enable USART1 Module (Asynchronous Mode)
- Set the USART1 communication parameters (baud rate = 115200, parity=NON, stop bits =1, and word length =8bits)
- Generate The Initialization Code & Open The Project In CubeIDE
- In the DMA settings tab, enable the DMA for the USART1_TX peripheral
- Set Mode to Circular & Increment Adress to Memory & data width Byte.
- Write The Application Layer Code
- main.c
- Open Proteus & Create New Project and click next
- Click on Pick Device
- Search for STM32F103C6 & SRF04
- Click on Virtual Instruments Mode then choose VIRTUAL TERMINAL & OSCILLOSCOPE
- finally make the circuit below and start the simulation
If you have any questions or suggestions don’t hesitate to leave a comment below
Comments