Professional Documents
Culture Documents
It is highly recommended that you understand how to program a PIC before continuing with
this tutorial. I have a tutorial written that explains this process. Take a quick look through it if you're
not familiar with the PIC Microprocessor.
The purpose of this project is to explain what a servo motor is, how it is used & how to control
it using the PIC microcontroller. Basic servo theory will be explained and then we'll look at some
sample programming methods to control servo motors.
A great advantage of servo motors is that they are very easy to control in comparison with
other types of motors (DC, Stepper) that require an H-Bridge or external circuitry. The servo motor
has all this circuitry internally.
Like DC motors, servo motors have been around a long time and are used in a wide variety of
applications. The basic design of the servo motor is the same no matter the size or manufacturer.
The picture below illustrates a cut-away of a servo and shows you what it looks like on the inside.
The bottom has the servo controller board which interprets the signal input & turns on the
motor inside which is geared up. You cannot see it clearly but there is also a potentiometer (aka a
variable resistor) inside the servo as well. The servo uses the potential meter for finding the different
angles that it will rotate to. These so called "pots" can go bad so, don't automatically throw away
servos if they don't work properly, often times all that needs to be replaced is the pot inside.
The power & ground wires are hooked directly up to whatever battery or power supply you are
using to power the servos. The Signal wire will be hooked up to the microcontroller used to control
the servo, in our case the PIC. A noticeable first impression, the servo only requires 1 pin from the
pic.
Frequency: 50Hz
Up-time: 0.9mS->2.1mS
Down-time: 19.1mS-17.9mS
At first glance these definitions & numbers might make little or no sense. So lets look at a
simple PWM wave at 50Hz.
So a PWM wave is just a signal that changes between 0 volts & 5 volts (digital logic 0 and 1).
We see that the wave is symmetrical; uptime is 10mS & downtime is 10mS which when added
together give us the period (10mS + 10mS = 20mS).
You may have noticed that the servos we're using are rated at 4.8v & 6v. We're hooking 5v to
the servos in this tutorial just to make things easier. This will not harm the servos in any way. They
can be safely operated between 4.8 and 6 volts. The increase in voltage gives the servos more torque.
Now that we know the necessary delays to create in order to get a proper servo PWM input
signal, let's look at the code. The code is fairly long so I'll go over the most important part.
for(count=0;count<50;count++) //50 * 0.020 Seconds = 1 Second
{
PORTB = 0x01; //PortB Pin 0 = 5v (logic 1)
Delay1KTCYx(7); //Delay 1.5mS
Delay100TCYx(5);
PORTB = 0x00; //PortB Pin 0 = 0v (logic 0)
Delay1KTCYx(92);//Delay 18.5mS
Delay100TCYx(5);
}
END CODE
This little chunk of code will move the servo to a 45° angle and hold there for 1 second. The
servos are very strong so for that 1 second tries as you might but you won't be able to change the
angle it's at.
Servo Motors are definitely very useful in the world of robotics. They allow us torque &
precise movements. They are great 'actuators' for imitating things like fingers, hand or leg
movements. The ease of control via the PIC makes them all the more desirable to use in projects,
however even servos have their limitations.
What To Do Now
There are many different types of servos out there. Hi-Tec, Futaba just to name a few. Some
have different wiring & even use different types of PWM input signals. This may force you to change
the programming style I've created to better suite your specific servos.
Please note that the methods I've used in this tutorial for controlling servos are not the only
ones. You can potentially control as many servos as you want with a PIC and your own creative
programmings so don’t feel that you are limited by the code I've shown you here today.
Conclusion
The tutorial was able to cover everything I had hoped it would and we had success in
controlling servos which was our main purpose & goal. I will have more projects coming which use
servos so you can see how they fit into projects which are doing many things at once.
Having now gone over how to control DC Motors & Servo Motors I'll be able to move onto
stepper motors in a future tutorial. So for those of you who have been waiting to see it, a stepper
motor tutorial is in the works & coming.