In this video, I'm going to teach you about servos!
What they are, how they work, and how to use them.
Then I'm going to show you a practical example where we make a simple laser turret!
Finally, I'm going to show you how to program an Arduino to control
the laser turret using a free, open source, visual
programming language called XOD.
What is a servo?
In this context, the term servo is short for servo motor.
The term servo means there is a closed loop control system that accurately
regulates the position of the servo's output shaft.
Now let me go into more detail.
In my previous video about H bridges I showed you a circuit that can control the speed
and direction of a motor.
But that's all it can do.
The motor just moves faster or slower depending on the
duty cycle.
And if you put a load on the motor it slows down.
And there's no way to accurately control how much the motor rotates.
This is what is called an open loop control system i.e. there is no way to regulate
the output, you're just changing the power going to the motor and hoping for the best.
A servo motor is a motor with a closed loop control system.
This means that you do have a way to control the speed and position, regardless of the
load.
Here's how they work.
You've got a DC motor being powered by an H bridge.
Then you have a series of gears to increase the torque of the system.
Most cheaper servos will have a plastic gearing system, but servos designed for higher holding
torque will usually have metal gears.
(alert noise) Metal Gear?!
Yes.
They're very solid.
Next, a servo will have some sort of feedback system that detects the rotation angle of
the output.
It could be a potentiometer, a magnetometer or
an encoder, but for hobby servos it's usually just a
potentiometer.
Inside the servo motor there's some circuitry that compares the user's desired rotation
angle with what the servo's feedback system is measuring.
If the output has rotated even a tiny bit in the wrong direction, it
adjusts the power to the H bridge, and makes sure the output is exactly where it's supposed
to be.
When you put all these things together, you get a very useful building block in robotics.
From simple toys to industrial robotics and animatronics nearly
every robot will have servo motors in some form or another.
There are many types of servos out there, from many different manufacturers, and they
all have different ratings.
The main specifications to look out for are the input voltage range, the stall torque,
and the speed.
You should also be aware of the servo's travel.
This refers to how much the output shaft can rotate.
It could be anywhere from 90 degrees to 200 degrees
or more.
And you can even get servos that continuously rotate.
Servos come in standardized sizes.
These are "standard" servos, and these are sub micro servos.
These would probably be all you'd ever care about
for projects at home, but I want you to be aware that for
bigger robotics projects you can get servos in larger form factors.
If all these options seem overwhelming to you, don't worry.
A good general purpose servo you can use is the Hitec HS-311.
It's been around for years.
It's functional and it's reliable.
In bulk you can get sub micro servos for $2 a pop.
I'll put links in the video description section.
Ok, so now you know what servos are and where to get one.
Let's talk about how to use them.
Every servo will come with a bag of extra parts.
These things are called servo horns.
The output shaft of a servo has these little teeth on them that grip the horn.
Screw the horn onto the output shaft, and then you can
wiggle the horn around.
(Juvenile laughing)
The point of the horn is that you can screw other mechanical parts onto it.
Here's an example where I have some plastic gears attached to the horn, and
when I rotate the servo shaft the gripper opens and closes.
And you can use servos to control pretty much anything mechanical in any axis.
Next let's talk about how to actually control a servo.
In addition to horns, servos also have pigtails.
I swear I'm not making this stuff up.
The colors will be either orange red brown or yellow red black.
Red and brown is where you connect power to the servo, and this
usually needs to be between 4.5 and 6 volts.
So most people use either a 3 or 4 cell alkaline or NiMH
battery pack, or sometimes people will use a 5 volt switch mode power supply.
For this project I'm just going to use my bench power supply set to
5V.
This third wire is where you connect the input signal that controls the servo.
Technically the signal is a PWM signal, but it's a very special PWM signal.
We're going to start by sending out a pulse that is 1.5ms wide, at 50Hz.
And the pulse is 0 to 5 volts.
This will center the servo.
You can repeat the pulses slightly faster or slower than 50Hz and the servo will still
work.
The most important thing here is the pulse width, not the frequency.
If we change the pulse width from 1.5ms to 2ms, the servo rotates clockwise.
And if we change the pulse width from 1.5ms to 1ms, the
servo rotates counterclockwise.
You can move a little further than those extremes, but the standard signal
is expected to be 1 to 2 ms wide.
I've been using a fancy waveform generator to generate these pulses for demonstration
purposes.
But you don't need one of these to do this at home.
It is possible to generate servo pulses with a 555 timer circuit,
but I would not recommend it.
Really the best way to do it is with a microcontroller like an Arduino
because it will give you very precise control over the timing.
Make sure you've watched my previous tutorial on Arduino if you need an introduction,
and then soon I'm going to show you how to use XOD to
create the servo signals.
Okay, let's talk about assembling the laser turret.
If you want, you could simply glue a second servo to a
servo horn, and tape on a laser, but I wanted something a little nicer than that for the
video.
So I ended up buying a pan-tilt servo kit from Digikey.
You can get them pre-assembled or buy the servos separately.
You can use any dollar store laser pointer for
the laser, or you can get them in bulk for even less than a dollar
on Amazon.
The laser modules have integrated resistors, so you just need to give them 3 to 5 volts
and they will work without any special circuitry.
Since they are cheap, I glued two of them onto the pan tilt head.
To control the turret in the X and Y direction, I'm using two 10k potentiometers.
The output of these will feed a 0 to 5 volt analog signal into the
Arduino on pins A0 and A1.
If you're really creative you could hack an old analog joystick to be the controller.
I already mentioned I'm going to power the servos with an external 5 volt supply, and
over here you can see how I'm using Arduino output pins 8 and
9 to feed the servo pulse signals into the servos.
Here's a view of the overall project.
Notice how I have a jumper wire connecting the ground from the external
5 volt supply to the ground of the Arduino.
They both need to have a common ground connection in order
for things to work reliably!
Okay, that takes care of the hardware side of things, now let's talk about programming
the Arduino so we can control the servos with the potentiometer.
We're going to be using a free, open source visual programming environment called XOD.
Go to XOD.io, then click IDE, Download Desktop IDE at the
top.
Register if you want, but I'm going to proceed without
registration.
Now you can download XOD for any of the major operating systems.
After you run the installer and start XOD, you'll be greeted with the option to run through
a set of tutorials.
I do recommend you check these out at some point, but for now let's create a blank new
project.
We give it a name... and now we have a blank space to work with.
So the first thing we're going to do is go to the common hardware list on the left.
You can see a lot of devices are supported but we are just going
to look for pot, short for potentiometer.
And we're going to drag two of them onto the workspace.
These little boxes are called "nodes", and the way XOD works is
you create a bunch of these little nodes that represent inputs and outputs on the microcontroller,
and you connect them with wires in the interface.
Now let's go back to our hardware list and create two nodes for the two servos we want
to control.
And we're going to connect the output of the potentiometers to the servos by dragging a
couple of wires.
At this point I recommend you go to view, toggle helpbar.
This will give you a guide to what all the little
input and output parts of each node are.
For example, a servo node has two inputs: the output port
number on the Arduino, and the value to send the servo.
In this case the servo node accepts a value from 0
to 1 to represent the servo's rotation.
And for the potentiometer, you can see how the output is a value from 0 to 1, depending
on how much you have rotated the potentiometer.
This is perfect, it's exactly what the servo node would expect to see
on the input, so we can connect the two nodes directly with a wire.
Next, for the sake of clarity, we should start giving our potentiometers and servos meaningful
names.
And we do that by clicking on the nodes, and then
typing names into the properties section on the left.
We're just going to call them X and Y potentiometers
and servos.
Now that we have some meaningful names, we can start defining which pins our hardware
is connected to.
I want my X potentiometer input to be Arduino pin A0, so we are okay here.
I want my Y potentiometer input to be pin A1, so let's change the port
number to 1.
I want my X servo output signal to come from Arduino pin 8, so we set that to 8, and let's
make the Y servo signal appear on pin 9.
And that's it!
We're done!
Let's go to Deploy, Upload code to Arduino.
Double check your board type and COM port settings are correct, and hit upload.
It'll take a few seconds to compile the visual, node based
programming into native microcontroller code.
And there you go!
Now we can use the potentiometers to rotate the laser turret in different directions.
But there is a small problem...
I don't like how in the Y direction, the head can move all the way down to
the floor, or point straight up at the ceiling.
I'm not enemies with Spiderman.
I'd rather have it so a full potentiometer rotation will move the servo
up and down in roughly a 40 degree arc.
To do this, let's go back to XOD, and let's browse the list of core nodes.
These are all the different math operations you can do.
We're looking for one called map range.
Let's drag that onto the workspace, and we can delete the existing wire by clicking
on it and hitting backspace.
Then we connect the potentiometer's output to the map range node's
input, and connect its output to the servo.
If we click on the map range node the help bar will explain how it works, and we're going
to make it so that a value of 0 to 1 coming from the potentiometer
will end up being mapped to a value between 0.5 and
0.75 going to the servo.
This should limit the movement of the Y axis servo.
After compiling and uploading the code to the Arduino again, now the travel range of
the Y servo is a lot more practical.
It's a very simple project, but the principles you learned here will apply to building more
serious robots... or more fun robots!
Thank you for watching!
And be sure to check out other videos on my channel to learn more about
electronics.