(light music)
- So today we are going to be going
over Sergei's 14th PIC10F200 tutorial
and today we are going to be creating a robot
with an ultrasonic sensor.
This is the chassis that we've used
in the past couple of tutorials
but today we are going to be using this ultrasonic sensor
and all we're going to be doing is setting this
up so that it continuously goes forward
until it comes within about four centimeters of an object.
And once it gets within four centimeters
which is about two inch, two-ish inches, then it detects it
and backs up and then keeps on going forward.
And so that's basically all we are going to do
in this video.
I'm going to be going over things from a high level
kind of talking about the concept of the ultrasonic sensor
and some of the trickier parts of the code.
But if you do have any particular questions
about the code that I mentioned and you're like, wait
what is he saying?
Sergei has done a very, very in-depth written tutorial.
Go check it out on circuitbread.com.
He will answer basically any questions you have
that I simply cannot get to to make this video, eh
at least reasonably long.
Okay, with that
let's start out with the ultrasonic sensor and
first how ultrasonic sensors in general work
and then how this particular one works.
So the way an ultrasonic sensor works is it emits a sound
usually a ping, some sort of encoded ping that
it can recognize and it sends that sound out
that sound goes, hits something and comes back
and then it detects that sound.
And so as soon as it shoots that sound out
it starts a timer and then as soon as it detects that sound
it stops that timer.
It takes that amount of time in seconds, multiplies it
by approximately 340 meters per second or exactly 340 meters
per second depending on the settings here.
And then divides that by two
because you need to take into account
that the signal is going out and back and then it figures
out in meters exactly how far away something is.
And that is the basic idea of ultrasonic sensors in general.
Now this one, this is very
very common ultrasonic sensor that's been simplified.
So all you have to do is send it a trigger signal
and then as soon as it receives that trigger signal
it takes care of the ping encoding and the detection.
It just takes the echo pin
and puts it high as soon as it sends out the pulse.
And then as soon
as it receives the pulse in return, it drops it.
So it takes care of all
of that sound detection and all that sort of stuff.
And all we have to do
with the microcontroller is to measure the amount
of time that the echo pin was high
and then we can do the calculations ourselves.
So in reality, these things are pretty simple
and doing that simple multiplication
and division is quite straightforward.
Even though with assembly as we're using here
it's a little bit more complicated
If we're using C, it'd be incredibly simple
but it's a little bit more complicated with assembly.
But that is okay.
So that's the general idea here is what we are going to
do is we're gonna take some code that's going to send
out a trigger to the ultrasonic sensor on a regular basis
going to measure that amount of time.
And as long as the distance isn't less
than four centimeters, we're just going to
do our sub-routine that has us moving forward.
And then if it does detect something
within four centimeters, it'll say, oh yeah, let's jump
into our reverse sub-routine, do that reverse sub-routine
and then start the detection process all over again.
And that's the basic architecture
of what we are going to be doing in assembly today.
So two things before we jump into the code.
First we will go over it, but
in the code we set GP1 as trigger and GP3 as echo.
So that's gonna be important as we're going
through just when you're looking at the code
making sure like you understand what's actually going on.
Second, this particular ultrasonic sensor that I'm using
and it's what Sergei recommended in the written tutorial
it does not do well with high voltages.
And because we have a limited amount
of pins on our PIC10F200, we need to share some pins
with the VPP programming line with our picket four.
So when you are programming the PIC10F200
remove that line that goes to your ultrasonic sensor
to the VPP line, make sure it's disconnected
or else you're gonna be getting upwards
of 12 volts on your ultrasonic sensor
and you have a high likelihood of frying it.
So that's in the tutorial.
I'm gonna say it again when you're doing this
make sure you don't do that
because you don't wanna fry the ultrasonic sensor
because I mean most
of us don't have a bunch of these laying around.
Some of us do, but most of us don't.
And you don't wanna fry it unnecessarily.
Okay, so with that, let's jump into the code itself.
So this is one of the simpler codes that we've done
in quite a while.
So there's actually only one particular part
that I'm gonna go over in depth
otherwise I'm just gonna kind
of clarify what each section does.
So as we know, lines one through basically eight.
That's all just definitions.
Nothing unique.
10 through 15 is just your initialization.
And you can see there that we are setting GP0
GP1 and GP2 as outputs
and that's all we are doing in the initialization.
Then in 16, we're in the loop
we're in the main portion of our program.
So the first thing we do is we set GP1 high
and then we do attend microsecond delay
by just doing basically no op instructions that go
to current location plus one.
It's just killing time, but it's very specific time
which is exactly what we want.
What we are doing here with GP1
if you remember, that's the trigger.
So we are telling the microcontroller
with a ten second trigger signal that, hey
we want you to take a sample right here.
So from lines 17 through 23
we've basically got GP1 high
waited 10 microseconds and then dropped GP1 down low.
Now in 24 we move three
the number three into our W or our working register.
And that's gonna be important later.
Right now that seems random, we'll get back to that later.
This is part of that more complicated portion
but don't worry about it so much right now.
And then in 25, we are checking
for the echo line to go high.
So to get the timing exactly right, we send the trigger
we wait for that echo line to go high
and then we'll wait for the echo line to drop.
So in 25 we are waiting for the echo to go high
and then as soon as it does go high, we stop doing that 25
and 26 loop and we jump to 27 where we clear timer zero.
So now that's resetting timer zero
and starting it from scratch.
And then in 28 and 29
we're doing basically the same thing as we did in 25 and 26
but the exact opposite by waiting for it to go low.
And then as soon as it goes low, then we jump to 30.
Now 30 through 31, surprisingly
is probably the most complicated part of this whole thing.
So we'll come back to this in just a second.
So that's important.
Just 30 and 31 is where we decide
whether or not we're close enough to something or we're not.
So if in 30 and 31 we decide that we are not too close
to anything, we jump to line 33, which 33
through 39 is simply a sub-routine that controls both
of our servos and says go forward.
And that's it.
It once it says go forward, it jumps back
to the beginning of the loop where it's doing the trigger
and the echo detection, all that sort of stuff.
Very straightforward.
Now if it says, Hey, there is something close
then it jumps to line 40 where it has that
in line 32 go to move backward, which is line 40.
It does basically the same thing as move forward
but it has one of the servos moving backwards
and one of 'em moving forward.
So you get that twisting tank motion goes through
does the steps backward, and then it's done.
And then it goes back to the beginning
and starts the whole thing, which you got in lines 53 up
until the very end in 83 are simply our delay subroutines
which we have gone over in depth in previous tutorials
and also the way we control the servos, which again
we've gone over in other tutorials.
So again, very, very straightforward.
We're just, is it close or not forward back?
Is it close or not forward back?
That's all we're doing.
So going back to the most complicated portion of this is
in line 24 where we put the number three
into our working register, and then in lines 30
and 31 where we are taking our timer zero
and subtracting W from that.
So basically if the result is one, you skip the next line.
If it's not, you execute the next line.
All right, so I'm gonna have to look
at my notes to explain this
so I don't screw up the numbers here exactly.
But the reason we chose these values is
because we know the clock cycle, we know how fast the speed
of sound is, and we can basically figure
out how much distance there is per cycle.
So where we get the numbers for this is
that we know that timer zero is set up
to be at a prescale area of one to 128.
So each timer tick takes 128 microseconds.
We also know that we can get one centimeter
of measurement for every 59 microseconds.
So each timer tick is actually 128 microseconds divided
by 59.
So each timer tick is actually 2.1 ish centimeters.
So when we load the value into the working register
we would expect to have two ticks to give us
4.2 centimeters, close enough to four centimeters.
So that's the rationale.
That's why we chose to put the value three in there.
Okay, see it.
So you just wait a second, you just said two.
Why are we saying three?
What we're doing is we're measuring two timer
ticks to give us the four centimeters
but when we look at the code and what's going
on is we're actually looking at the carry bit
and seeing whether or not it's set in line 30
we have W being subtracted from timer zero and then 31
we're actually checking to see if the carry bit
in the status register is going to be one or zero.
Now, if, let's just say timer zero is five, five minus three
that gives you two.
There's no carry bit because it
didn't have to pull any number from anywhere else
four minus three, no carry bit
didn't have to pull anything from anywhere else.
But if you have three minus three zero
you still aren't pulling anything though.
Two minus three gives you a negative number.
It has to carry from somewhere else,
it flags that carry bit.
It says, oh yes, this is a negative number.
So even though we're looking for two ticks
the reason we put three
in the working register is because two
minus three is the way we get that flag bit set
in the status register.
Okay, so I hope that made sense.
I hope that clarified what is potentially
the only confusing part of all of this.
At least that was the only part that I took a
little while to figure out what was going
on because again, it's like, oh, I want two
so I need to have three, but it's the two minus the three
then it's the carry bit and it just took a moment.
So if that doesn't make sense, I'm sorry
you can listen to it again.
You can go check out Sergei's written tutorial.
But other than that
this code is very, very straightforward.
So I'm actually going to hook this up
to the oscilloscope and give it some power.
And then I'm gonna show how the pulses change
in their width as I get closer and farther away
so that you can see that it's actually sending
out a different signal like, oh, hey, it's closer
and it sends a different signals to the servo.
So let's do that really quick.
Okay, so let's give this thing some power
and we're gonna look at the servo pulses
and see how they change
as they get closer and farther away from the sensor.
(machine puttering)
Okay, so you can see those pulses
they're happening about every, oh, we got five milliseconds.
1, 2, 3, 4, 5.
So between five and six divisions.
So about 25 ish milliseconds apart.
As I put my hand closer, you can hear the motor change pitch
and then you can also see those come closer together
pull it out, and there we go.
So this is a lot lamer than Sergei's
quite cool demonstration.
So he provided me a video.
So we will go over and show his video
of it working in a fully mobile live environment.
(machine whirring)
So that's about it.
This is actually really straightforward and it's fantastic
because ultrasonic sensors are ubiquitous.
They are everywhere.
They're in cars, they're on a robots, they're used
in sensing for liquids, for grain silos, all sorts of stuff.
So this is really, really quite cool.
I think this is the last tutorial.
We are going to be using this tank chassis on
so we can say goodbye
and thank you for your service, golden chassis of power.
If you found this tutorial helpful, useful
please give it a thumbs up, subscribe to our channel
all of that good stuff, and we will see you in the next one.
Have a great one.
(upbeat music)
Hey, I hope this tutorial is helpful.
Did you know that circuitbread.com also has more
useful engineering content?
In addition to the tutorials, textbooks
tools and other things, we have dozens
of EE FAQs that explain quick standalone concepts
that are helpful for electrical engineers.
Go check 'em out.
(upbeat music continues)