HC-SR04 Ultrasonic sensor and STM32
Ultrasonic ranging module HC – SR04 provides 2cm – 400cm non-contact measurement function, the ranging accuracy can reach to 3mm.
The modules includes ultrasonic transmitters, receiver and control circuit.
Today in this tutorial we are going to learn How to interface HCSR04 Ultrasonic sensor module with STM32.
Working of hcsr04 is pretty simple and straight.
The module emits an ultrasound at 40 KHz which, after reflecting from obstacle, bounces back to the module.
By using the travel time and the speed of the sound, we can calculate the distance between the sensor and the obstacle.
According to the datasheet of hc-sr04, the following is required to be done :-
- Keep the Trig pin HIGH for at least 10us
- The Module will now send 8 cycle burst of ultrasound at 40 kHz and detect whether there is a pulse signal back
- IF the signal returns, module will output a HIGH PULSE whose width will be proportional to the range of the object.
- Distance can be calculated by using the following formula :- range = high level time * velocity (340m/s) / 2
- We can also use uS / 58 = Distance in cm or uS / 148 = distance in inch
- It is recommended to wait for at least 60ms before starting the operation again.
Method to use
As mentioned above, the module sends a HIGH signal proportional to the distance measured. This signal remains high for few microseconds. To measure the width of the signal in microseconds, I will use the Input Capture to measure this Pulse Width.
As shown above, I have selected the Input Capture Direct Mode.
Prescalar is set to 72 -> This will divide the APB clock by 72, and bring the timer clock to 1 MHz. It is necessary because the HCSR04 sends the signal in microseconds.
ARR is set to 0xffff -> I have selected is as maximum as possible. This basically sets the limit, upto which we can count in microseconds.
Also make sure the TIMx Capture Compare interrupt is enabled. You can enable the Global interrupt also, if you don’t see this option
I2C 1 is selected -> To connect the LCD1602
Pin PA8 -> is automatically selected as TIM1_CH1 pin. I will use it as the ECHO Pin
Pin PA9 -> will be use as TRIG Pin, so select it as the output.
Some Insight into the CODE
In the Function above:-
- First Timestamp is captured, when the rising edge is detected. The polarity is now set for the falling edge
- Second Timestamp will be captured on the falling edge
- Difference between the Timestamps will be calculated. This Difference will be microseconds, as the timer is running at 1 MHz
- Based on the Difference value, the distance is calculated using the formula given in the datasheet
- Finally, the Interrupt will be disabled, so that we don’t capture any unwanted signals.
HCSR04_Read-> Triggers the sensor to start the measurement.
- It will pull the TRIG Pin HIGH for 10 microseconds, and then pull it LOW.
- This will force the sensor to start the measurement, and the sensor will pull the ECHO Pin HIGH for the respective amount of time
- To measure this time, we will enable the Timer interrupt, so that we can capture this Rising and falling edges