Description

100%
100%

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 HC-SR04 Ultrasonic sensor module with STM32.

hcsr04

WORKING

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.

HOW TO

According to the datasheet of hc-sr04, the following is required to be done :-

  • Keep the 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.
hcsr04 timing
100%
100%

Connection

100%
100%
hcsr04 connection
100%
100%

CODE

100%
100%

YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST

uint32_t local_time, sensor_time;
uint32_t distance;

uint32_t hcsr04_read (void)
{
	local_time=0;
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);  // pull the TRIG pin HIGH
	delay(2);  // wait for 2 us


	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);  // pull the TRIG pin HIGH
	delay(10);  // wait for 10 us
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);  // pull the TRIG pin low

	// read the time for which the pin is high

	while (!(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4)));  // wait for the ECHO pin to go high
	while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4))    // while the pin is high
	 {
		local_time++;   // measure time for which the pin is high
		delay (1);
	 }
	return local_time;
}
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  sensor_time = hcsr04_read();
	  distance  = sensor_time * .034/2;


	  HAL_Delay(200);
  }
100%
100%

RESULT

100%
100%
hcsr04 working
Check out the VIDEO Below
100%
100%

DOWNLOAD

100%
100%

You can buy me a coffee sensor ūüôā

Or just Download the code below

100%
100%

13
Leave a Reply

avatar
4 Comment threads
9 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
adminTigranyuvalyuvalAndy Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
mohammad
Guest
mohammad

thanks for this toturial i need it

Andy
Guest
Andy

I could compiled the download source code and appeared the undefined errors about lcd_init,lcd_send_cmd,lcd_send_data and DWT_Delay_Init

yuval
Guest
yuval

the sensor seems to work fine but it keeps on getting the answer 2!
(for “local time’)
and I cant see why .. I doubled checked the pin config like in the video and moreover when I disconnect the sensor at mid_run the “local time” var is reached zero and halt – witch means that 2 is actually the same answer it gets in any time .. any ideas?

Tigran
Guest
Tigran

Hi, file “stm32f4xx_hal.h” in zipfile is encrypted, could you upload correct version ?

Menu