Hello.
Welcome back.
We started by learning how to measure the time between events using timers.
And as I mentioned the excellent application for use in timers to measure the time between events is
when you are programming the drivers for ultrasonic sensors.
So we're going to learn about ultrasonic sensors in this lesson and then we'll go ahead to write our
own drivers an API and we would apply.
Time is to do that.
So back here our cortex and dot com and I'm gonna go to lessons and when the page opens I'm going to
select over here interface and we sense or so I'm going to select ultrasonic module see as SRO for when
I click.
There are some very nice article that we wrote some time ago on write in drivers for ultrasonic sensors.
So I'll just take you through the arm.
The ultrasonic sense or here.
This is a very nice summary of what a data sheet says.
So you don't need to read a data sheet of this component after this lecture.
Yeah.
So the H.C. CSR all for ultrasonic sensor is a very popular low cost ultrasonic sensor used for non
contact distance measurement.
The module comes with a separate transmitter and receiver to determine the distance of an obstacle in
front of the module.
That transmitter which is known as the trigger pin automatically transmits eight 40 heads ultrasonic
signals the signals or the bursts hit the obstacle and then bounces back to the module.
When and if the module when and if the ultrasonic sense IV gets back the receiver which is known as
the echo pin automatically turns high for a duration of their some of the time taken to send and receive
deep or trust sonic burst.
That time is then multiplied by a constant to get that distance.
So that's how the ultrasonic or works.
So the module we're using it for this experiment has four pins it's got pin one as far as CCD the second
being as their Trig trigger pin and then the third as the echo pin and then the fourth as they can D
deficiency which are connect to our microcontrollers two point three volt.
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
do next or five fivefold and then a trigger pin which would enable us a digital output pin remember
that trigger pin sends out the ultra sonic burst and then the echo pin we use as the digital input to
pin the echo Bean the pin that receives the bounced back signal when the signals hits when the signal
hits an obstacle it bounces back and then it is detected by the echo pin.
So we would enable this as an input pin and then we will connect the ground to the ground.
Right.
So this is this schematic we're going to use we're going to put 200 K resistor between the echo p and
now what P B6 we still use in the same microcontroller team for C 1 2 3 TVC Launchpad yes more about
the specifications of the board of the ultrasonic module it draws about 15 merely umpires the maximum
range is four meters.
That is very long and the minimum range is two centimeters.
The work and frequency as we said is 400 hertz and a trigger signal.
The signal is triggered at least 40 microseconds.
Let's see how do we pyramids these six steps to program.
This is just a summary would go into details when we start writing the actual code.
Over here it says set a trick pin of the ultrasonic module high.
Wait for a signal to bounce back Necco and then start a timer when equities detect it stop time.
When echo pain goes low.
So this how we measure the time between the event read the time value converts value to a distance.
So to even give you more details you can come back to this article and read it at your own convenience
but I would still summarize and tell you how it works yet.
It's called X and dot com.
You can check it out.
It's the the official site for all the cortex courses we have on all our you know our online schools.
How do you detect the echo.
So remember from the Peanuts above which we've seen we have said the echo pin as an input.
So to detect echo we can simply check whether the state of the inputs pin is high or not in the circuit
diagram above.
We have connected the echo into PBS six or four of our microcontroller to detect if p B6 is high and
it starts a timer.
So all we have to do is detect if P6 is high and it starts a timer.
And this is a very short pseudocode.
We can check with a simple logical comparison and then keep incrementing account of variable while the
condition is true.
A simple us while P B6 is not equal to zero.
Therefore it's high current plus plus distance cause counter times are constant.
We would have to derive this constant that would help us convert this counter value to our appropriate
time unit.
So that's one way of doing it and then converting the value of their counter to distance by more to
plan it to a constant.
That's what I've just said over here.
This method is straightforward but it's not very precise.
The more precise and efficient with which I highly recommend is by set in a general purpose timer to
detect the rising and falling edge of P6.
That's where you can detect exactly when P B6 gets high and when it's course low.
Nice.
So that is what we are going to do.
And this is a so getting your time is right.
Like I said when I come to this website I just read a blog post a bit like you know back in the days
at school.
Yeah.
So just bear with me.
It's very important to understand this stuff.
And what is written here is just yes.
A blog post I wrote so it's just like my script to write a more robust piece of code to interface with
ultrasonic sense or we need to set two timers.
If you refer back to the specifications of our H.R. off our HECS are all for addressing an excess or
in the table above.
You'll find that it says trigger signal at least 10 microseconds long.
This means this means that when we set our Trig up in high we have to wait for 10 microseconds and then
turn its back too low.
To achieve this microsecond delay we have to set a time to create this precise delay for us and we already
learned how to create delays using timers so that it's already covered.
Let's call this time a time a one for the sake of this example we shall call this time a time of 1.
As we mentioned earlier we need to detect the rise and fall in Edge of the Echo speed.
We shall need another time for this.
Let's call this time a time of zero the first and you should know about time as is that they are directly
affected by the run and frequency of the microcontroller.
You already know this course we've been right in time and drivers throughout this lesson or try this
course.
So yeah I'm glad you already know that.
Let's say we are running our team for C 1 2 3 2 C launchpad which by default runs at a 16 megahertz
to set the microsecond delay timer.
After performing the necessary initialization requirements such as enabling the clock for the time a
block sets and a bit option and selecting the running mode with a periodic or one shot or down counter
you will have to load the number sixteen minus one into their time into file load value.
This is how we derive 16.
You already know this with this example.
For a second delay we loaded 16 million into the interval load register and for a millisecond delay
we load it sixteen thousand into the interval load register now for a microsecond early we load just
16 into the interval load register and this is how it's derived.
You already know this suggests the same calculations we did at a time.
So let's see what we've got here.
Therefore at 16 megahertz our microcontroller Ryan 16 clocks runs this thing clock cycles in one microseconds
so 16 clock cycles is run in y and microseconds hence we load 16 so this is this is right up the border.
This is a delay function a very simple delay function microsecond delay function were written for this
very same thing like we did in our previous lessons.
We initialize the arm that propagates in mechanism of the time a block we disable the timer.
We can forget everything we loaded 16 into the time up.
Time a load register.
And then we initialize the timer back and then we created a loop and then we pulled for the time.
This is the same as the time delay functions we've been right.
Just that this is a microsecond version because we are lowered in 16 over here.
Now we are in a position to create microsecond delays to create a 10 microseconds delay as required
by the trigger pin.
We simply need to call our delay microsecond function like this delay microseconds.
Don't worry about all of this we are going to write all of the code and we are going to see its work.
So just hold on with me.
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
our equipping.
Again using our second diagram above as an example we can see the echo penis connected to p B6 of our
microcontroller in our function for initializing this timer.
We have set p p six alternates function so let's register.
We've said the appropriate PTL value of the pin.
This can be found in data sheet and we showed this in the very end an example we did for count an event.
We have said the bit option and finally set the running mode as both edges inputs capture mode again
for details you can check the data sheet or just wait until we start writing the code.
Yeah this is what the initialization function looks like.
We've initialized the function here.
This these are the clock gate initialization is initialized the timer with initialized the deport b
over here and then we've just said it would be disposed to be put to be direction register we set in
it as input by the direction the IRS admitted here and then we set the PTO CTO with the alternate function
as well.
So then we come here and then we disable the timer before we start configuring it and then we do the
configurations.
We set a time on mode register in the mode register we decide that we want to detect both edges discord
corresponds to that if you if we start code and you would see what I mean.
And then and then that's it we enabled a timer.
So this is just a very quick summary of how this is done to prepare you before we write the actual code.
So if there is something you do not understand please leave it in a comment section.
So how do we really measure the distance of an object.
Let's see to measure the distance of an object in front of the ultrasonic module we have to set a trick
below to just make sure it is low wait for about 10 microseconds for it to take effect set a trip in
high with 410.
Wait for at least 10 microseconds set a trip in low again start time of zero captured a timestamp of
the rising edge capture the timestamp of the falling edge get a difference of the two timestamps calculate
the distance in centimeters and then formula is time difference multiplied by six two point five we've
taken the time difference this formula.
The time difference multiplied by one period.
You know we run that 16 megahertz in one period is sixty two point five nanoseconds.
That is this sixteen point five race to the power minus nine.
This sixty two point five nanoseconds and then we're more to play by this constant.
I'm going to get into the details of derive and this constant this constant is derived from the speed
of sound.
And when we multiply this we get that distance in centimeters and we are going to verify that we will
do that.
And good to see it's working.
So this is what the code looks like this the function we are going to write.
We've called it measure D and then we stop by.
We'll connect the trigger pen to put a X terror.
I wouldn't go into this.
We would just go and write the actual code instead.
So we'll write the code.
And still if you still want to read this you can come to cortex.
Dot com go to lessons and then you can select interface interfacing H.S. S R O for all trust on extend
source and then you see all of this here.
It's all free of charge and we keep updating our lessons with free lessons to help students and professionals
get updated their new standards and new ways of coding the cortex architecture.
Yeah.
So I'll see you in the next lesson.
We will go to you vision and write the actual code for this lesson.
See you.