16F Servo Motor Example
How
Servo Motor Control Works
Also known
as “closed feedback†systems, Servo motors come with a control
circuit, which senses if motor mechanism is in desired location and
if not it continuously corrects an error until motor reaches the
proper point. Servo's usually come in small plastic packages like
the following, but keep in mind there is a whole system inside;
motor itself, gears and motor driving and control circuit.



Servo's
usually have great torque because of their gearing. The motor speed
decreases, but their torque increases, allowing them to drive high
loads. The closed feedback loop mentioned earlier is actually an
internal potentiometer that is connected to a mechanical shaft to
sense the angle of turn. The signal from the potentiometer provides
data to the control circuit to ensure the motor is at the desired
angle.
Most
servos are designed to operate within a limited range, e.g. 90° or
180°, but can be modified for continuous rotation. This accurate
control of rotation and torque thanks to the gearing makes the servo
a great device to include in any mechanical-robotic type project.
Most servo's operate from 4.8 to 7.2V DC.
Servo control signals
Servo
motor shaft is positioned with pulse width modulated signals (PWM).
So all servos comes with three wires (Power, Ground and Control).
Usually in hobby servos with rotation angle 90° signal width vary
between 1 and 2ms. If pulse is more wide rotation continues until
reaches mechanical limits. This signal is applied to the control
line of the servo. Refer to your datasheet for more information, but
for the example of servos in this tutorial, our servo has a maximum
rotation of 180° and operates with a control signal from 1mS to 2mS.
With this in mind, a signal with a 1.5mS high pulse will tell the
servo to go to half way, or a signal at 2mS will make the servo go
to 100% deflection. This signal must be sent to the servo once every
1/50th of a second (50Hz), allowing your program to perform many
other functions in between.
In Proton+ the Servo command does just as we have described
above. By using it we can tell the servo to go to any position, and
stay there. Some servo's have different pulse requirements for
different angles, but most common RC Servos will operate from 1mS to
2mS.

Once a servo is in position, the potentiometer feedback to the
servos internal control circuit will ensure that the servo does not
drift from the desired position, (providing they are refreshed with
the position every 50Hz or 20mS), if there is too much time between
the control signals, the servo will drift, and to fast a signal will
make the servo chatter. Refer to the datasheet of your servo for its
optimal refresh rate.
Servo's have 3 wires, each will be for; +Ve supply, -Ve Supply and
Signal. Here's a basic program to interface with Servo's (For more
detail on specific commands, see the Proton+ Help File)
Device = 16F877
Xtal = 4
Dim Servo_Position as Word
LCD_DTPIN = PORTB.4
LCD_RSPIN = PORTB.2
LCD_ENPIN = PORTB.3
LCD_INTERFACE = 4
LCD_LINES = 2
LCD_TYPE = 0
All_Digital = True
Delayms 150
Cls
Servo_Position = 1500
Main:
Repeat
SERVO PORTA.0, Servo_Position
DelaymS 20
Inc Servo_Position
Print At 1, 1, "Servo = ", Dec4 Servo_Position
Until Servo_Position = 2000
Repeat
SERVO PORTA.0, Servo_Position
DelaymS 20
Dec Servo_Position
Print At 1, 1, "Servo = ", Dec4 Servo_Position
Until Servo_Position = 1000
Goto Main

Note the PIC's power supply/oscillator are not shown
Click
here to watch this circuit in action!
One thing to keep in mind is that servo's generate a lot of noise in
your circuit. This can lead to very inaccurate timing and strange
things happening. To help stop this, be sure to have a large
capacitor (around 100uF) near the output of your servo's +Ve supply,
and a 0.1uF Tantalum capacitor as close as possible between the
PIC's +Ve and -Ve pins.
Optoisolators are great for isolating circuits from each other,
and could be used to great effect with servo's.
A great
supplier of Servo's is
Hobby Engineering

 | Site Tutorial Index |
|  | 16F PIC Examples |
| |  | Motors |
| |  | Thermometers |
| |  | Code Snippets |
|  | 18F PIC Examples |
|  | Handy Tips |