1
00:00:03,270 --> 00:00:03,860
Hello.

2
00:00:03,900 --> 00:00:05,040
Welcome back.

3
00:00:05,040 --> 00:00:10,790
We started by learning how to measure the time between events using timers.

4
00:00:10,800 --> 00:00:18,930
And as I mentioned the excellent application for use in timers to measure the time between events is

5
00:00:19,410 --> 00:00:24,180
when you are programming the drivers for ultrasonic sensors.

6
00:00:24,240 --> 00:00:31,140
So we're going to learn about ultrasonic sensors in this lesson and then we'll go ahead to write our

7
00:00:31,140 --> 00:00:34,830
own drivers an API and we would apply.

8
00:00:34,830 --> 00:00:36,230
Time is to do that.

9
00:00:36,240 --> 00:00:47,840
So back here our cortex and dot com and I'm gonna go to lessons and when the page opens I'm going to

10
00:00:47,840 --> 00:00:56,390
select over here interface and we sense or so I'm going to select ultrasonic module see as SRO for when

11
00:00:56,390 --> 00:00:56,930
I click.

12
00:00:56,930 --> 00:01:04,330
There are some very nice article that we wrote some time ago on write in drivers for ultrasonic sensors.

13
00:01:04,460 --> 00:01:06,710
So I'll just take you through the arm.

14
00:01:06,890 --> 00:01:10,050
The ultrasonic sense or here.

15
00:01:10,100 --> 00:01:13,450
This is a very nice summary of what a data sheet says.

16
00:01:13,490 --> 00:01:19,000
So you don't need to read a data sheet of this component after this lecture.

17
00:01:19,170 --> 00:01:19,600
Yeah.

18
00:01:20,420 --> 00:01:28,460
So the H.C. CSR all for ultrasonic sensor is a very popular low cost ultrasonic sensor used for non

19
00:01:28,460 --> 00:01:30,750
contact distance measurement.

20
00:01:30,860 --> 00:01:36,410
The module comes with a separate transmitter and receiver to determine the distance of an obstacle in

21
00:01:36,410 --> 00:01:37,850
front of the module.

22
00:01:37,910 --> 00:01:45,590
That transmitter which is known as the trigger pin automatically transmits eight 40 heads ultrasonic

23
00:01:45,590 --> 00:01:53,120
signals the signals or the bursts hit the obstacle and then bounces back to the module.

24
00:01:53,420 --> 00:02:00,830
When and if the module when and if the ultrasonic sense IV gets back the receiver which is known as

25
00:02:00,830 --> 00:02:09,230
the echo pin automatically turns high for a duration of their some of the time taken to send and receive

26
00:02:09,260 --> 00:02:11,460
deep or trust sonic burst.

27
00:02:11,600 --> 00:02:16,080
That time is then multiplied by a constant to get that distance.

28
00:02:16,100 --> 00:02:19,900
So that's how the ultrasonic or works.

29
00:02:19,940 --> 00:02:27,860
So the module we're using it for this experiment has four pins it's got pin one as far as CCD the second

30
00:02:27,860 --> 00:02:35,060
being as their Trig trigger pin and then the third as the echo pin and then the fourth as they can D

31
00:02:35,810 --> 00:02:40,900
deficiency which are connect to our microcontrollers two point three volt.

32
00:02:41,120 --> 00:02:48,410
We shall try to put a three point three volt if it's not enough 42 to power the V.C. we're going to

33
00:02:48,570 --> 00:02:54,980
do next or five fivefold and then a trigger pin which would enable us a digital output pin remember

34
00:02:54,980 --> 00:03:02,140
that trigger pin sends out the ultra sonic burst and then the echo pin we use as the digital input to

35
00:03:02,140 --> 00:03:09,530
pin the echo Bean the pin that receives the bounced back signal when the signals hits when the signal

36
00:03:09,560 --> 00:03:14,380
hits an obstacle it bounces back and then it is detected by the echo pin.

37
00:03:14,390 --> 00:03:21,770
So we would enable this as an input pin and then we will connect the ground to the ground.

38
00:03:21,770 --> 00:03:22,660
Right.

39
00:03:22,700 --> 00:03:28,950
So this is this schematic we're going to use we're going to put 200 K resistor between the echo p and

40
00:03:29,010 --> 00:03:39,470
now what P B6 we still use in the same microcontroller team for C 1 2 3 TVC Launchpad yes more about

41
00:03:39,510 --> 00:03:47,010
the specifications of the board of the ultrasonic module it draws about 15 merely umpires the maximum

42
00:03:47,010 --> 00:03:48,830
range is four meters.

43
00:03:48,830 --> 00:03:54,160
That is very long and the minimum range is two centimeters.

44
00:03:54,330 --> 00:03:59,580
The work and frequency as we said is 400 hertz and a trigger signal.

45
00:03:59,580 --> 00:04:04,780
The signal is triggered at least 40 microseconds.

46
00:04:05,250 --> 00:04:09,060
Let's see how do we pyramids these six steps to program.

47
00:04:09,060 --> 00:04:13,830
This is just a summary would go into details when we start writing the actual code.

48
00:04:13,950 --> 00:04:18,760
Over here it says set a trick pin of the ultrasonic module high.

49
00:04:19,140 --> 00:04:28,010
Wait for a signal to bounce back Necco and then start a timer when equities detect it stop time.

50
00:04:28,020 --> 00:04:30,520
When echo pain goes low.

51
00:04:30,780 --> 00:04:38,820
So this how we measure the time between the event read the time value converts value to a distance.

52
00:04:38,820 --> 00:04:45,660
So to even give you more details you can come back to this article and read it at your own convenience

53
00:04:45,710 --> 00:04:49,800
but I would still summarize and tell you how it works yet.

54
00:04:49,820 --> 00:04:51,150
It's called X and dot com.

55
00:04:51,150 --> 00:04:52,370
You can check it out.

56
00:04:52,560 --> 00:05:00,940
It's the the official site for all the cortex courses we have on all our you know our online schools.

57
00:05:00,960 --> 00:05:02,730
How do you detect the echo.

58
00:05:02,790 --> 00:05:11,160
So remember from the Peanuts above which we've seen we have said the echo pin as an input.

59
00:05:11,460 --> 00:05:19,560
So to detect echo we can simply check whether the state of the inputs pin is high or not in the circuit

60
00:05:19,560 --> 00:05:20,420
diagram above.

61
00:05:20,430 --> 00:05:29,420
We have connected the echo into PBS six or four of our microcontroller to detect if p B6 is high and

62
00:05:29,420 --> 00:05:30,290
it starts a timer.

63
00:05:31,220 --> 00:05:36,440
So all we have to do is detect if P6 is high and it starts a timer.

64
00:05:36,650 --> 00:05:38,990
And this is a very short pseudocode.

65
00:05:38,990 --> 00:05:45,950
We can check with a simple logical comparison and then keep incrementing account of variable while the

66
00:05:45,950 --> 00:05:47,670
condition is true.

67
00:05:47,800 --> 00:05:51,350
A simple us while P B6 is not equal to zero.

68
00:05:51,500 --> 00:05:56,770
Therefore it's high current plus plus distance cause counter times are constant.

69
00:05:56,780 --> 00:06:04,460
We would have to derive this constant that would help us convert this counter value to our appropriate

70
00:06:04,490 --> 00:06:05,780
time unit.

71
00:06:05,780 --> 00:06:12,590
So that's one way of doing it and then converting the value of their counter to distance by more to

72
00:06:12,590 --> 00:06:13,910
plan it to a constant.

73
00:06:13,910 --> 00:06:15,960
That's what I've just said over here.

74
00:06:16,040 --> 00:06:20,000
This method is straightforward but it's not very precise.

75
00:06:20,000 --> 00:06:27,770
The more precise and efficient with which I highly recommend is by set in a general purpose timer to

76
00:06:27,770 --> 00:06:31,780
detect the rising and falling edge of P6.

77
00:06:31,910 --> 00:06:38,700
That's where you can detect exactly when P B6 gets high and when it's course low.

78
00:06:39,200 --> 00:06:39,770
Nice.

79
00:06:39,800 --> 00:06:42,050
So that is what we are going to do.

80
00:06:42,170 --> 00:06:46,310
And this is a so getting your time is right.

81
00:06:46,310 --> 00:06:52,220
Like I said when I come to this website I just read a blog post a bit like you know back in the days

82
00:06:52,220 --> 00:06:52,740
at school.

83
00:06:52,850 --> 00:06:53,140
Yeah.

84
00:06:53,180 --> 00:06:54,200
So just bear with me.

85
00:06:54,200 --> 00:06:57,630
It's very important to understand this stuff.

86
00:06:58,220 --> 00:07:01,760
And what is written here is just yes.

87
00:07:01,760 --> 00:07:08,590
A blog post I wrote so it's just like my script to write a more robust piece of code to interface with

88
00:07:08,720 --> 00:07:12,560
ultrasonic sense or we need to set two timers.

89
00:07:12,560 --> 00:07:20,060
If you refer back to the specifications of our H.R. off our HECS are all for addressing an excess or

90
00:07:21,890 --> 00:07:22,880
in the table above.

91
00:07:22,880 --> 00:07:27,800
You'll find that it says trigger signal at least 10 microseconds long.

92
00:07:27,830 --> 00:07:35,300
This means this means that when we set our Trig up in high we have to wait for 10 microseconds and then

93
00:07:35,690 --> 00:07:37,540
turn its back too low.

94
00:07:37,580 --> 00:07:45,140
To achieve this microsecond delay we have to set a time to create this precise delay for us and we already

95
00:07:45,140 --> 00:07:50,790
learned how to create delays using timers so that it's already covered.

96
00:07:50,790 --> 00:07:56,390
Let's call this time a time a one for the sake of this example we shall call this time a time of 1.

97
00:07:56,540 --> 00:08:02,060
As we mentioned earlier we need to detect the rise and fall in Edge of the Echo speed.

98
00:08:02,300 --> 00:08:05,300
We shall need another time for this.

99
00:08:05,300 --> 00:08:12,350
Let's call this time a time of zero the first and you should know about time as is that they are directly

100
00:08:12,350 --> 00:08:15,920
affected by the run and frequency of the microcontroller.

101
00:08:15,920 --> 00:08:22,250
You already know this course we've been right in time and drivers throughout this lesson or try this

102
00:08:22,250 --> 00:08:22,670
course.

103
00:08:22,680 --> 00:08:26,050
So yeah I'm glad you already know that.

104
00:08:26,060 --> 00:08:34,190
Let's say we are running our team for C 1 2 3 2 C launchpad which by default runs at a 16 megahertz

105
00:08:34,730 --> 00:08:37,160
to set the microsecond delay timer.

106
00:08:37,280 --> 00:08:42,350
After performing the necessary initialization requirements such as enabling the clock for the time a

107
00:08:42,350 --> 00:08:50,020
block sets and a bit option and selecting the running mode with a periodic or one shot or down counter

108
00:08:50,780 --> 00:08:57,990
you will have to load the number sixteen minus one into their time into file load value.

109
00:08:58,010 --> 00:08:59,780
This is how we derive 16.

110
00:08:59,810 --> 00:09:03,250
You already know this with this example.

111
00:09:03,260 --> 00:09:10,670
For a second delay we loaded 16 million into the interval load register and for a millisecond delay

112
00:09:10,700 --> 00:09:17,990
we load it sixteen thousand into the interval load register now for a microsecond early we load just

113
00:09:18,050 --> 00:09:23,000
16 into the interval load register and this is how it's derived.

114
00:09:23,000 --> 00:09:27,640
You already know this suggests the same calculations we did at a time.

115
00:09:29,060 --> 00:09:33,000
So let's see what we've got here.

116
00:09:33,500 --> 00:09:41,900
Therefore at 16 megahertz our microcontroller Ryan 16 clocks runs this thing clock cycles in one microseconds

117
00:09:42,130 --> 00:09:52,280
so 16 clock cycles is run in y and microseconds hence we load 16 so this is this is right up the border.

118
00:09:52,300 --> 00:09:59,470
This is a delay function a very simple delay function microsecond delay function were written for this

119
00:09:59,470 --> 00:10:02,950
very same thing like we did in our previous lessons.

120
00:10:02,950 --> 00:10:09,780
We initialize the arm that propagates in mechanism of the time a block we disable the timer.

121
00:10:09,820 --> 00:10:14,870
We can forget everything we loaded 16 into the time up.

122
00:10:15,420 --> 00:10:16,810
Time a load register.

123
00:10:16,870 --> 00:10:23,830
And then we initialize the timer back and then we created a loop and then we pulled for the time.

124
00:10:23,830 --> 00:10:28,400
This is the same as the time delay functions we've been right.

125
00:10:28,510 --> 00:10:34,650
Just that this is a microsecond version because we are lowered in 16 over here.

126
00:10:34,730 --> 00:10:41,470
Now we are in a position to create microsecond delays to create a 10 microseconds delay as required

127
00:10:41,470 --> 00:10:42,880
by the trigger pin.

128
00:10:42,880 --> 00:10:48,380
We simply need to call our delay microsecond function like this delay microseconds.

129
00:10:48,400 --> 00:10:54,890
Don't worry about all of this we are going to write all of the code and we are going to see its work.

130
00:10:54,910 --> 00:10:56,650
So just hold on with me.

131
00:10:57,210 --> 00:11:04,180
Now let's take a look at how we are going to use time as Europe to get the rise and fall in urge of

132
00:11:04,270 --> 00:11:05,470
our equipping.

133
00:11:05,770 --> 00:11:13,210
Again using our second diagram above as an example we can see the echo penis connected to p B6 of our

134
00:11:13,210 --> 00:11:17,350
microcontroller in our function for initializing this timer.

135
00:11:17,350 --> 00:11:21,710
We have set p p six alternates function so let's register.

136
00:11:21,910 --> 00:11:25,340
We've said the appropriate PTL value of the pin.

137
00:11:25,420 --> 00:11:33,750
This can be found in data sheet and we showed this in the very end an example we did for count an event.

138
00:11:33,760 --> 00:11:41,080
We have said the bit option and finally set the running mode as both edges inputs capture mode again

139
00:11:41,080 --> 00:11:47,470
for details you can check the data sheet or just wait until we start writing the code.

140
00:11:47,480 --> 00:11:53,290
Yeah this is what the initialization function looks like.

141
00:11:53,290 --> 00:11:55,660
We've initialized the function here.

142
00:11:55,700 --> 00:12:05,340
This these are the clock gate initialization is initialized the timer with initialized the deport b

143
00:12:05,830 --> 00:12:11,670
over here and then we've just said it would be disposed to be put to be direction register we set in

144
00:12:11,670 --> 00:12:21,280
it as input by the direction the IRS admitted here and then we set the PTO CTO with the alternate function

145
00:12:21,280 --> 00:12:22,020
as well.

146
00:12:22,660 --> 00:12:28,930
So then we come here and then we disable the timer before we start configuring it and then we do the

147
00:12:28,930 --> 00:12:30,310
configurations.

148
00:12:30,550 --> 00:12:38,070
We set a time on mode register in the mode register we decide that we want to detect both edges discord

149
00:12:38,110 --> 00:12:42,620
corresponds to that if you if we start code and you would see what I mean.

150
00:12:42,850 --> 00:12:47,450
And then and then that's it we enabled a timer.

151
00:12:47,590 --> 00:12:55,510
So this is just a very quick summary of how this is done to prepare you before we write the actual code.

152
00:12:55,510 --> 00:12:59,600
So if there is something you do not understand please leave it in a comment section.

153
00:13:00,730 --> 00:13:04,400
So how do we really measure the distance of an object.

154
00:13:04,450 --> 00:13:10,600
Let's see to measure the distance of an object in front of the ultrasonic module we have to set a trick

155
00:13:10,630 --> 00:13:18,370
below to just make sure it is low wait for about 10 microseconds for it to take effect set a trip in

156
00:13:18,370 --> 00:13:20,410
high with 410.

157
00:13:20,590 --> 00:13:28,210
Wait for at least 10 microseconds set a trip in low again start time of zero captured a timestamp of

158
00:13:28,210 --> 00:13:36,520
the rising edge capture the timestamp of the falling edge get a difference of the two timestamps calculate

159
00:13:36,520 --> 00:13:45,370
the distance in centimeters and then formula is time difference multiplied by six two point five we've

160
00:13:45,370 --> 00:13:47,810
taken the time difference this formula.

161
00:13:47,810 --> 00:13:51,270
The time difference multiplied by one period.

162
00:13:51,300 --> 00:13:57,700
You know we run that 16 megahertz in one period is sixty two point five nanoseconds.

163
00:13:57,700 --> 00:14:01,870
That is this sixteen point five race to the power minus nine.

164
00:14:01,890 --> 00:14:07,080
This sixty two point five nanoseconds and then we're more to play by this constant.

165
00:14:07,210 --> 00:14:13,300
I'm going to get into the details of derive and this constant this constant is derived from the speed

166
00:14:13,300 --> 00:14:14,210
of sound.

167
00:14:14,370 --> 00:14:20,810
And when we multiply this we get that distance in centimeters and we are going to verify that we will

168
00:14:20,810 --> 00:14:21,320
do that.

169
00:14:21,340 --> 00:14:22,790
And good to see it's working.

170
00:14:24,040 --> 00:14:29,670
So this is what the code looks like this the function we are going to write.

171
00:14:29,740 --> 00:14:33,490
We've called it measure D and then we stop by.

172
00:14:33,730 --> 00:14:37,480
We'll connect the trigger pen to put a X terror.

173
00:14:37,480 --> 00:14:39,240
I wouldn't go into this.

174
00:14:39,450 --> 00:14:42,090
We would just go and write the actual code instead.

175
00:14:42,130 --> 00:14:44,090
So we'll write the code.

176
00:14:44,260 --> 00:14:48,960
And still if you still want to read this you can come to cortex.

177
00:14:49,020 --> 00:14:56,630
Dot com go to lessons and then you can select interface interfacing H.S. S R O for all trust on extend

178
00:14:56,640 --> 00:14:59,060
source and then you see all of this here.

179
00:14:59,170 --> 00:15:06,370
It's all free of charge and we keep updating our lessons with free lessons to help students and professionals

180
00:15:06,760 --> 00:15:13,520
get updated their new standards and new ways of coding the cortex architecture.

181
00:15:13,950 --> 00:15:14,220
Yeah.

182
00:15:14,230 --> 00:15:16,830
So I'll see you in the next lesson.

183
00:15:16,850 --> 00:15:20,920
We will go to you vision and write the actual code for this lesson.

184
00:15:20,920 --> 00:15:21,420
See you.
