0:00
welcome to controllers tech I made a
0:03
video about HCS a zero for ultrasonic
0:06
sensor to find distance this video will
0:10
cover the HCS a zero force sensor but
0:13
this time we will use a different but
0:15
better approach towards it we will see
0:18
how can we use input capture to measure
0:20
the pulse width and which will be used
0:23
to find the distance let's start with
0:26
cube ID I am using stm32 f103 c8
0:34
controller give the name to the project
0:38
and click finish let's set up the clock
0:44
first I am selecting external crystal do
0:49
this if you have cortex m3 only let's
0:53
run the system at maximum clock I am
1:01
going to use timer 1 for this project
1:04
select the input capture mode I have
1:08
made a video on input capture a few
1:10
months ago you can watch it by clicking
1:12
the top right corner timer 1 is
1:16
connected to a PB 2 clock and which is
1:19
clocked at 72 megahertz if you are using
1:23
any other timer check the datasheet to
1:25
know where it is connected to prescaler
1:29
in my case will be 71 this will bring
1:33
the timer clock to 1 megahertz
1:35
and we will get a time a period of 1
1:37
microsecond per count this is important
1:40
because HCS are 0 for sends the pulse of
1:44
few microseconds so in order to read the
1:48
we need the timer working in
1:49
microseconds range now a r r will be the
1:54
maximum possible value for a 16-bit
1:57
timer it would be 65535
2:08
make sure you enable the timer capture
2:11
compare interrupts let's assume that you
2:15
used another timer like timer two and
2:18
you don't see the capture compare
2:20
interrupts then in that case enable the
2:23
global interrupt pin PA it is selected
2:32
by default and we will connect the echo
2:35
pin here select the pin pa9 as the
2:39
output for the trick pin everything is
2:42
set now click Save to generate the
2:45
project let's start now first of all I
3:02
will create a delay function which can
3:04
provide us the microsecond delay I have
3:08
already made a video on it check the top
3:36
here are the variables defined which
3:39
will be used this callback function will
3:43
be called whenever a rising or falling
3:45
edge will be captured first on detecting
3:50
a rising edge the timestamp will be
3:52
stored in the IC Val one variable now we
3:56
will set the polarity for capturing the
3:58
falling edge when the falling edge is
4:01
captured another timestamp will be
4:04
stored in the IC Val two variable now
4:07
the counter will be reset and we will
4:10
calculate the difference between two
4:11
captured timestamps this difference will
4:15
be in microseconds as our timer is
4:17
running in that range based on this
4:21
difference the distance will be
4:23
calculated by using the formula provided
4:26
in the sensors datasheet after finishing
4:29
up all the calculations we will change
4:31
the polarity in order to capture the
4:34
rising edge again and this loop will
4:37
keep repeating if you are using any
4:40
other timer channel make sure you update
4:42
it at all the respective positions this
4:49
is the function to trigger the HC SR 0 4
4:52
to start capturing data
4:55
let me quickly define the trick pin and
5:07
to trigger the HCS are 0-4 we have to
5:11
pull the trigger pin high for around 10
5:13
microseconds and then pull it low for
5:18
I created a delay function to give delay
5:34
now in the main function we will start
5:37
the timer in input capture interrupt
5:57
and in the while loop we will read the
6:01
HCS a zero for every 200 milliseconds
6:19
we got a lot of errors here I have
6:26
defined the pin in a wrong way
6:32
we still got few errors this error is
6:40
actually present in the default
6:42
generated code you can also click the
6:50
error in the console and you will
6:52
directly reach here click at this end
6:56
and delete this bracket I didn't got
6:59
this error in f4 series controllers you
7:03
may or may not get this based on your
7:06
software version if you are not looking
7:09
for the LCD this is it you will get the
7:13
distance accurately within plus minus 2
7:24
let's include the i2c for the LCD I am
7:29
using i2c one for this purpose that
7:41
arrow got generated again
7:43
let me quickly fix it now let's include
8:01
the LCD library files
8:22
refresh the code so that you can see
8:25
them if you are using another eye to see
8:28
make sure you change here initialize the
8:39
LCD first let's print this string on the
8:47
display now inside the while loop we
8:54
will print the distance values on the
8:56
display let's say the distance is 23
9:05
then 23 divided by 10 will give us 2 and
9:08
plus 48 is to convert to the respective
9:15
similarly 23% 10 will give us three
9:38
let's build it looks like I forgot to
9:42
include the head of file
9:54
include I to see LCD H
10:08
everything is good now let's flash it to
10:53
you can see the distance shown is as
10:56
expected this is it for the video I hope
11:01
you understood the concept and the
11:03
procedure you can download the code from
11:07
the link in the description keep
11:09
watching and have a nice day