Instructables

Use a PIC Microcontroller to Control a Hobby Servo

This instructable describes how to integrate hobby servos (the kind used in RC planes, cars, etc.) into your microcontroller projects.
 
Remove these adsRemove these ads by Signing Up

Step 1: How Servos are Different from Regular Motors

In a regular DC motor, the amount of torque the motor exerts on the shaft is proportional to the amount of current flowing through the motor. A simple way to control it is by varying the voltage across the motor; more voltage means more current which means the motor pushes harder against its load which means the shaft turns faster.

When using a servo, however, you don't control torque or velocity. Instead, you specify what angle you want the shaft at. In other words, you have positional control of the motor.

Inside a servo is a traditional DC motor, a potentiometer (variable resistor), and control circuitry. The potentiometer is connected to the motor such that when the motor shaft turns it also turns the potentiometer. The controller can then measure the voltage at the center pin of the potentiometer and get an indication of the shaft's position. The controller receives a signal (see next step) from the user that sets a desired position. The controller compares the desired position to the current position of the motor and uses that information to turn the motor in a direction that minimizes the error.

The way this works in practice is you specify the angle you want the shaft at using your PIC, the shaft turns to that position, and then holds there. The further it gets pushed away from that position, the harder it tries to turn back. Hobby servos are usually geared way down, so even a wimpy $15 or $20 one can hold its position reasonably well.
do you have a version of using assembly codes? most of the codes were in C language... please?
leevonk5 years ago
actually what this code which sucks should be using is the built in hardware PWM pins on most PICs. it will let you do whatever you want while the pwm runs in the background.
google pic hardware pwm

here's a good page on it:
http://www.winpicprog.co.uk/pic_tutorial8.htm
(you can usually use assembler in line with other higher level code if you're using PICBasic or PICC)

pete_l leevonk5 years ago
No, you're wrong. The code in the example you cite is to control normal, DC motors - not steppers. Most people who know, will tell you that trying to control a servo from using the PICs hardware PWM is hard, as you can't get the delay times long enough for the 20mSec pulses (unless you run at a very low clock speed). As a consequence, experienced PIC programmers will code the servo control explicitly - not using the hardware PWM. The code shown doesn't suck - it's a nice, simple example that couples a pot. to a servo, so you can control the servo's position by rotating the pot.
leevonk pete_l5 years ago
first of all I said it 'sucks' because the person that wrote the code said it 'sucks'. secondly, a servo motor is not the same as a stepper motor, completely different things. thirdly, you're probably right about hardware pwm incompatibility with servos, I've used hardware pwm only for regular dc motors and assumed it would work with servos. Sorry for the bad suggestion.
x8 leevonk3 years ago
sir , would you help me in constructing a servo contoller using pic 18f.. i'm just a beginner.
Usually the pwm pins are not good for servos, they are usually set up to cover almost 0 to 100 % duty cycle and have poor resolution in the 1 to 2 % used by servos. To have code that does not suck you should use interrupt processing. Managing each servo in say 3 ms lets you easily fit 6 servos in 20 ms. And since the processing is by interrupts I would guess you use less than 10 % of the processing cycles. Examples of interrupt processing are not too hard to find on the net,
shanakaplus3 years ago
um looking for elevator pic program. Do you have example program ?
gmg3 years ago
I think a PIC running at 4MHz clock speed executes one instruction cycle in 1 usec. This is consistent with what you see on your oscilloscope, with a delay of 20 cycles.
Motta5 years ago
DELAY.ASM?
akatsuki6665 years ago
this is baisic not intended question for this specific project but my arduino duemilaove has a error for ANY program i put in there. it always finds a error..help????????????????????????????????????
collard416 years ago
i usually put a 1000uF capacitor (electrolytic) across the power terminals, as this smoothes out the big spikes in the power supply. if you use assembly code to program the device, every 4 oscillations it advances one clock cycle, most instructions take one cycle but some instructions take cycles (e.g. goto or return). using this it approximately (because of the instructions take 2 cycles) takes a millionth of a second for each instruction and therefore you can calculate delays using instructions rather than the onboard timers. this way you can calculate the pulses accurately.
squares6 years ago
Brilliant, this is fascinating thank you! However a schematic and a basic explanation your PIC/USB interface for controlling multiple servos would really harden my nipples. Uh, if you wanted to. \) _\) (o.0) ('")('")
marcastro6 years ago
Awesome projects. I got my MS in Computer Science and have been working as Software Engineer. I'm going back to school in PHd in Computer Engineering @ UC Berkeley next year--got accepted. I'll specialize in robotics. I'll try to learn and purchase the products you have before I start my school and learn it. My goal is to create java libraries for microcontroller programming. Oh boy...It's going to be fun fun fun.
I was wondering if either of these two microcontrollers would work or if anyone has any information on them, or what type they are... they are 40 pins each, heres the writing on each of them. 1: Front: C34451BE CHICONY VER-L 105-08049-250 9708 CTI (c) INTEL 1980 Back: TC7903.1 2: Front: CHICONY (r) ver-c 105-06868-031 6868A-10063 0212M E2NE3 Back: [none] any help would be appreciated, and pin outs/manuals would be great too.
in this servo the potentiometer shaft is the shaft through the gear at the bottom of the parts picture!!!
I'm afraid you're mistaken about where the potentiometer is inside the servo!!!! the thing you indicated was the output shaft!!!! the potentiometer is actually under one of the gears next to the motor!!!!
Excellent tutorial. You mention the commands should arrive between 50 to 100x per second. Is 100hz the fastest you can send the commands? I wanted to use it with a PIC PWM @ 8MHz (you can view the schematic at site under updates, see Firefly) www.blueroomelectronics.com
offlogic8 years ago
re: best practices: 1. Don't count on a higher level language like 'C' to execute quickly. If you have a deterministic time-frame in the millisecond domain, use assembler (c'mon, it's easy and deterministic, and doesn't eat up 50K for a 'HELLO WORLD' demonstration). 2. Nothing less than a 0.1uF cap across the supply pins of every device, placed right next to it. 3. The USB specification defines a unit load as 100mA, so watch out for your power demands, especially the little power spikes. Per 2 (above) more capacitance will help average these out. 4. Great Instructable!
Relativity8 years ago
Thanks for making this. Have dealth with servos before but you go pretty in depth. Understood most of it. Keep up the good work!