♪
Welcome to the Thinkabit Lab.
Today we'll learn about servos.
What are servos? Servos are these little guys.
They are motors that you can program,
that you can control.
Why would that be important?
Have you ever wanted to build a robot?
Or control the way something moves?
You can use servos to do that.
We're going to use two different kinds of servos.
This small one is a position-based servo,
and the big one is a continuous servo.
The big one, you can control
the speed and direction in which it spins,
and it spins 360 degrees.
The small one, you can program its position.
This one is 180-degree range,
but you can buy a 90-degree-range servo,
270-degree-range servo, depending on what you want.
So, we're going to program a very simple example for these,
something that you can manipulate and control.
First, look at how there are three wires
coming out of your servo.
The red and black power the motor,
and the white is the signal it's looking for to control it.
It's called the PWM signal-- pulse-width modulation--
and it's looking for a specific shape.
Like your heart rate, a PWM is a specific shape.
We don't want to program for that,
so in Arduino, there's a library we can reference to use it.
So, let's get started by connecting your Arduino
to your computer using a USB cable.
Connect your USB cable to your Arduino,
and then connect that to your computer.
It takes a moment for the Arduino
to register on your computer.
Once it has, go to "Tools: Find Port"
and choose the com port number that's associated with Arduino.
Go ahead and click that,
Then I like to go to "File: New" and expand that window...
and delete everything inside.
I say Control-A and backspace,
and I do that because I like to write code
from scratch with this exercise.
Next, I want us to get that library.
The library that will generate the signal that we need,
go to "Sketch: Include Library,"
and then we click on "Servo."
Then it pops up with that line of code.
Then we want to name our servo.
Write S-e-r-v-o, space "myservo"...
and then semicolon.
That let's the code and the Arduino know
that we're using a servo.
Then what I'd like us to do
is to attach our servo to the right pin number.
To do that, look at our Arduino
and notice on the digital side there's a parentheses
that says PWM with a squiggly line next to it.
And notice that 3, 5, 6, 9, 10, and 11
have that squiggly line next to it.
That means that those pins
can generate the PWM that the servo needs to move.
We're gonna go ahead and use 9.
In our void setup,
this is where we can set up our Arduino's hardware.
So, "void setup," all lowercase, open, close parentheses,
open bracket-- shift next to the letter "P"--
enter, and it automatically generates that close bracket,
and make sure your cursor's in between those two brackets.
We'll attach our servo here.
So, we write "myservo.attach,"
and then write 9 inside and then semicolon.
Essentially, pin 9 is going to control our servo.
The next thing I want to do is write a void loop.
This is where the action happens.
The lines in the void loop run over and over again.
We're gonna write "void loop,"
open, close parentheses, open bracket, enter.
Notice the close bracket,
and we'll write our lines of code inside there.
The code that I wanna write could be used for both servos.
So, the first line, "myservo.write(0);"--
this line of code for the continuous servo
will have it spinning to the left really fast.
For a small servo, it'll go to position 0 degrees.
How do I know that?
With the small servo, it's intuitive.
I'll discuss that later.
The big servo's not intuitive.
We'll look into how we can use a number line
to understand what numbers correlate to what actions.
We'll do that certain action for a certain amount of time,
so our next line will be "delay."
We'll use the value 2000 and then semicolon.
So 2000 milliseconds is 2 seconds.
The big servo
will spin left fast for two seconds.
The small servo
will go to position 0 degrees for two seconds,
but only if we keep writing code.
Otherwise, it wouldn't make sense to have a delay.
Let's keep writing: "myservo.right(180)."
And then "delay 2000."
Looking at our void loop, this tells me that our big servo
is gonna spin left for two seconds
and right for two seconds,
and keeps doing that.
The small servo will go to position 0 degrees,
then position 180 degrees, then position 0 degrees.
We want to see that happen, so we'll upload our code.
Push the arrow pointing right.
Once it's uploaded, let's start connecting them.
Start with the big servo.
Take your red, black, and white wire,
and make sure they're color-coded.
We color-code wires because it makes it easier
and less likely to actually make mistakes.
Put black wire to black, red to red, and white to white.
Again, black and red are your power.
Black goes GND, or the negative side of power.
Red can go into VIN, although some people use 5V.
And white is where you programmed it
in your void setup, pin 9.
Notice this servo spins to the left and then to the right
and then to the left.
It's fully rotating, which only a continuous servo can do.
The position-based servo will not fully rotate.
Right? It's position-based.
But let's look at the small servo.
Take out the wires,
and be careful that they don't touch each other.
And then plug in the small servo
to see how the same code
behaves differently with each servo.
So, this servo goes to position 180,
then 0,
then 180,
then 0.
How do I know it's 0? It's relative.
Depends on where you want to reference it.
I'm going to decide this is 0
and that's 180.
It's not perfect.
A more expensive servo will be more precise.
Let's start changing our code.
Let's see what happens if we manipulate it a bit.
Before I do that, I want to share with you
the number line for a big servo.
I'll unplug this for a moment.
And so you're aware,
the values you can use for both servos,
according to this library,
is 0 to 180.
Okay? Those are the only values you can use
for myservo.write in that parentheses.
So, for a full-rotation, continuous servo,
we're gonna use a number line.
So, we're gonna say 0 is here,
and then over here is going to be 180.
So, 180 divided by 2 is 90.
That's about halfway.
And 90 for a continuous servo
is stop.
I'll put that in quotation marks,
'cause it's not perfect.
Sometimes you put 91; sometimes 89.
It's trial and error to make it stop.
Choosing a value to the right of 90
will make your servo spin to the right.
Choosing a value to the left of 90
will make your servo spin to the left.
And I said you can control
the speed
Looking at my fingers,
the further away you get from 90, the faster you go.
The closer you get to 90, the slower you go.
If you want the fastest it can go,
you can only use 0 and 180.
And again, to go slower,
just get closer to the value of 90.
And the small servo is more intuitive.
The value you put inside the parentheses
is the angle it will point to.
So, for the position-based servo,
if I say myservo.write(0),
I go to 0 degrees.
If I say, myservo .write(90),
I go to 90 degrees.
If I say, myservo.write(180), I go to 180 degrees.
Let's play around with some examples using the big servo.
You can change values in the delay
or change change values in myservo.write
inside those parentheses.
Let's start with delay, and instead of 2000,
let's put 200 and see what happens.
It's gonna shimmy, because 200 milliseconds
is not enough time for it to fully rotate.
It will shimmy, and it drifts a bit, so be wary of that.
Using the small one, it will shimmy, but it won't drift.
Maybe I don't want it to go back and forth.
I want it to only go one way.
I'll erase three lines of code.
I only want it to do one thing.
I want it to spin slowly to the right.
I'll use the value 100.
I'm gonna upload that.
Notice it spins to the right slowly.
And if you were to use that line of code
and switch it to the small servo,
you're telling it to go to position 100
and not do anything.
So, students will be like, "My servo's broken."
Watch what happens when I plug it in.
Went to position 0 and it stopped.
You can feel that you can't move it,
but it's not very strong and could break easily.
You need at least four lines of code
for the position-based servo.
I'll show you
the example of the shimmy on a position-based servo.
But at least with this one, it will not drift.
I encourage you to change values in your myservo.write.
Change values in your delay.
Write more lines of code in your brackets,
and see what you can do with it.
I hot-glue items on there to see them move.
Maybe you want to get something crawling.
But have fun, see what it can do, and enjoy yourselves.
Thanks.
Funding to purchase and make this educational production
accessible was provided by the U.S. Department of Education.
PH:1-800-USA-LEARN (V) or WEB: www.ed.gov.