In this video, we connect the HC-SR04 ultrasonic sensor to an STM32 microcontroller and measure distance using Input Capture mode in STM32CubeIDE. Learn how to configure timers, handle echo pulses, and calculate precise distances step by step.
00:00Welcome to Controllers Tech. I made a video about HC-SR04 ultrasonic sensor to find distance.
00:09This video will cover the HC-SR04 sensor, but this time we will use a different, but better approach towards it.
00:17We will see how can we use input capture to measure the pulse width, and which will be used to find the distance.
00:25Let's start with Cube IDE.
00:30I am using STM32F103C8 controller.
00:36Give the name to the project, and click Finish.
00:42Let's set up the clock first.
00:45I am selecting External Crystal.
00:48Do this if you have Cortex-M3 only.
00:52Let's run the system at maximum clock.
00:55I am going to use Timer 1 for this project.
01:04Select the Input Capture mode.
01:08I have made a video on input capture few months ago.
01:11You can watch it by clicking the top right corner.
01:15Timer 1 is connected to a PB2 clock, and which is clocked at 72 MHz.
01:22If you are using any other timer, check the datasheet to know, where it is connected to.
01:27Pre-scaler in my case will be 71.
01:31This will bring the timer clock to 1 MHz, and we will get a timer period of 1 µs per count.
01:39This is important because, HCSR04 sends the pulse of few microseconds, so in order to read the width of the pulse, we need the timer working in microseconds range.
01:51Now, ARR will be the maximum possible value.
01:57For a 16-bit timer, it would be 65,535.
02:01Make sure you enable the timer capture compare interrupt.
02:13Let's assume that you used another timer, like Timer 2.
02:18And you don't see the capture compare interrupt, then in that case, enable the global interrupt.
02:24Pin PA it is selected by default, and we will connect the echo pin here.
02:37Select the pin PA9 as the output for the trig pin.
02:42Everything is set now.
02:44Click save to generate the project.
02:54Let's start now.
03:01First of all I will create a delay function, which can provide us the microsecond delay.
03:07I have already made a video on it.
03:10Check the top right corner.
03:24Here are the variables defined, which will be used.
03:41This callback function will be called, whenever a rising, or falling edge will be captured.
03:47First, on detecting a rising edge, the timestamp will be stored in the ICVAL1 variable.
03:56Now we will set the polarity for capturing the falling edge.
04:00When the falling edge is captured, another timestamp will be stored in the ICVAL2 variable.
04:07Now the counter will be reset, and we will calculate the difference between two captured timestamps.
04:13This difference will be in microseconds, as our timer is running in that range.
04:20Based on this difference, the distance will be calculated, by using the formula provided in the sensors datasheet.
04:28After finishing up all the calculations, we will change the polarity, in order to capture the rising edge again.
04:36And this loop will keep repeating.
04:38If you are using any other timer channel, make sure you update it at all the respective positions.
04:48This is the function to trigger the HCSR04, to start capturing data.
04:56Let me quickly define the trig pin, and port.
04:59To trigger the HCSR04, we have to pull the trig pin high for around 10 microseconds, and then pull it low.
05:17For this purpose only, I created a delay function to give delay in microseconds.
05:23Now in the main function, we will start the timer in input capture interrupt mode.
05:40And in the while loop, we will read the HCSR04, every 200 milliseconds.
06:04We got a lot of errors here.
06:26I have defined the pin in a wrong way.
06:28We still got few errors.
06:39This error is actually present in the default generated code.
06:49You can also click the error in the console, and you will directly reach here.
06:55Click at this end, and delete this bracket.
06:58I didn't got this error in F4 series controllers.
07:03You may or may not get this, based on your software version.
07:08If you are not looking for the LCD, this is it.
07:12You will get the distance accurately, within plus minus 2 centimeters range.
07:17Let's include the I2C for the LCD.
07:29I am using I2C1 for this purpose.
07:32Let the filter request.
07:41That error got generated again.
07:44Let me quickly fix it.
07:54If you are not looking for the Alexa Mask,
07:56Now let's include the LCD library files.
08:23Refresh the code, so that you can see them.
08:26If you are using another I2C, make sure you change here.
08:38Initialize the LCD first.
08:46Let's print this string on the display.
08:52Now inside the while loop, we will print the distance values on the display.
09:03Let's say the distance is 23, then 23 divide by 10 will give us 2, and plus 48 is to convert
09:10to the respective character.
09:11Similarly, 23% 10 will give us 3.
09:17Let's build it.
09:18Let's build it.
09:25Looks like I forgot to include the header file.
09:32Include i2clcd.h.
09:39Include i2clcd.h.
09:46Include i2clcd.h.
09:55Everything is good now.
10:09Let's flash it to the board.
10:16Let's do it.
10:17Let's do it.
10:18Let's do it.
10:19Let's do it.
10:23Let's do it.
10:24Let's do it.
10:25Let's do it.
10:26Let's do it.
10:30You can see the distance shown is as expected.
10:58This is it for the video.
11:01I hope you understood the concept, and the procedure.
11:05You can download the code from the link in the description.
Be the first to comment