Now that you have added ultrasonics insult to your in a secret, let's understand a few things about
it and how it works so we can better understand then how to control it in the original code.
So what does the ultrasonic sensor do?
Well, it will measure a distance from an object on a wall that is in front of the sensor.
OK, so here we are using the HTC as our four sensor and here are the practical characteristics of the
sensor.
So the characteristic you need to know in order to use it correctly.
So the range of the sensor is between two centimetre and 400 centimeter, which means that, as you
can see here on this diagram, you can start measuring objects and obstacles from two centimeters away
from the sensor up to about four meter here, which is about one hundred and fifty something inches.
If you need the conversion to whatever is to close from the sensor, less than two centimeter will not
be measured and whatever is too far will not be measured either.
OK, then you have the measuring angle, which is 15 degrees.
OK, so on this diagram, it's not exactly 15 degrees.
OK, just so you can understand, but you can see the angle.
So everything which is below that angle or above that angle will not be measured.
So the sensor, as you can see, will measure mostly what is in front of it.
OK, you don't need to remember 15 degrees, just that the sensor will measure what is in front of it.
OK, not on the side of the sensor.
OK, mostly what's in front and from two centimeter to four meters.
And so it's not uncommon in a robot, for example, a mobile robot to have multiple of those sensors.
OK, we can have different sensors for different angles of the robots and different positions.
Now, how does it work?
Very quickly.
So you don't need to understand everything that is written here.
This is just the diagram from the sensor data sheet.
So here, what are you going to do first?
You have seen that we have a trigger in your trigger digital.
What you are going to do to start measuring the distance is you're going to send so the signal will
be low by default.
You are going to send a high signal or 10 microseconds and then look after the ultrasonic sensor will
be triggered and thus will start to measure the distance.
OK, this is an active measurement, not a passive one.
So because this is active, what the ultrasonic sensor will do, it will send burst, OK, with some
wave or some pulse.
It will send it in front of its OK to the range that is in front of it and then it will just wait until
it receives some pulse back.
OK, so if there is an object in front of the sensor, the wave will bounce on the object and will be
returned to the sensor.
OK, so the sensor will measure the duration of how long the wave takes to come back to the sensor.
And then once it has measured this, then you will get some data on the echo pin.
OK, so you have the training and equipping so the equipment will be low by default and then the ultrasonic
sensor will simply but this thing.
So you will read this as an input.
It will put this being high for a certain duration and then low and these duration will be proportional
to the distance of the object.
OK, so if the waves from the sensor comes back very quickly, then the time is quite quick and this
pulse would be quite quick.
Also, if the object is very far at the maximum distance, then these bursts will be longer and the
exact computation for how to measure the distance from the duration here we are going to see that in
the code.
So to quickly, a record.
As a practical recap, you only need to do two things.
First, to send a 10 microsecond high signal on the trigger pin that we are going to do with data.
Right.
And then so this you just need to know it exists, but then you need to simply monitor the echo pin,
OK, and measure the duration of that puts so that ECOFIN will be digital being set as input.
But then how can we measure the duration of that.
But this we are going to use the pulse in function.
OK, and I'm just going to describe quickly.
How does the policing function works so here, this signal here is that one, OK, so it is low and
then it is high and then it is low.
This is a pulse.
Now, what do we want to do is to measure the duration of the outputs.
OK, how do we do that?
Because, well, with digital radio we can know if the signal is low or high, but then how do we measure
the exact duration for the pulse?
Well, we're going to use then the pulse in function.
So when you call the pulse and function the four steps, first one, so you call it, it will start
and then it will wait.
OK, as long as the signal is low, it will wait.
OK, then when the signal rises, so when the signal goes from low to high, the function we start to
measure the duration.
And when the signal goes back to low, the pulse in function will then be able to measure that direction
and it will react in the pulse duration in microseconds.
So you just need to use pulse in function and you can see how it works basically here and you will get
the duration of the pulse.
Also note that this is the high mode, which means that the signal will be first low and then you are
going to measure the bones that is high, but you can also have the opposite.
OK, imagine that the signal is always high and then the pulse is simply the signal going low like this
and then going higher again.
Then you can measure the pulse also with pulse in, but not with the high mode, but with the low mode.
But of course here, because the sensor works like this, we're going to use the high mode.