3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Use a PIC Microcontroller to Control a Hobby Servo

Step 5Improvements

Which brings me to my next point: this code sucks. You can do hardly any processing other than signaling to the servo, which is ridiculous because the vast majority of that time is spent locked up in a delay function. Also, it's hard to predict just how much overhead you have from other parts of the code that could be throwing the timing off; that constant of proportionality shouldn't have required an experiment to get it working correctly.

What this code should use instead of delays is the PIC's built in timers. Code for this might look something like:
OpenTimer0( TIMER_INT_OFF &
T0_SOURCE_INT & T0_16BIT &
T0_PS_1_2 ); // i think this makes a timer that raises a flag every 21.8 ms

while (1) {
while(!INTCONbits.TMR0IF){ // wait for timer to set flag
// do useful things
}
INTCONbits.TMR0IF=0; // reset timer flag

// send pulse to servo
}

I know I said there was supposed to be a pulse every 10 to 20 ms, but 21.8 ms is pretty close and hobby servos are generally very forgiving.
« Previous StepDownload PDFView All StepsNext Step »
2 comments
Jun 25, 2009. 5:49 PMleevonk says:
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)

Sep 28, 2011. 7:24 AMruss_hensel says:
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,
Jul 9, 2009. 8:40 AMpete_l says:
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.
Jul 9, 2009. 9:47 AMleevonk says:
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.
Dec 11, 2011. 6:08 AMx8 says:
sir , would you help me in constructing a servo contoller using pic 18f.. i'm just a beginner.

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
2
Followers
1
Author:dtydc