Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Living
Outside
Play
Technology
Workshop
http://www.instructables.com/id/Use-a-PIC-Microcontroller-to-Control-a-Hobby-Servo/
Image Notes 1. Potentiometer 2. DC motor somewhere in side . . . 3. Most servos are heavily geared down 4. Control circuitry
http://www.instructables.com/id/Use-a-PIC-Microcontroller-to-Control-a-Hobby-Servo/
http://www.instructables.com/id/Use-a-PIC-Microcontroller-to-Control-a-Hobby-Servo/
File Downloads
servo.c (2 KB) [NOTE: When saving, if you see .tmp as the file ext, rename it to 'servo.c']
Step 5: Improvements
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.
Related Instructables
http://www.instructables.com/id/Use-a-PIC-Microcontroller-to-Control-a-Hobby-Servo/
Comments
16 comments Add Comment
shanakaplus says:
um looking for elevator pic program. Do you have example program ?
gmg says:
Jan 18, 2011. 4:18 PM REPLY 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.
Motta says:
DELAY.ASM?
leevonk says:
Jun 25, 2009. 5:49 PM REPLY 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 says:
Jul 9, 2009. 8:40 AM REPLY 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 says:
Jul 9, 2009. 9:47 AM REPLY 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.
akatsuki666 says:
Feb 7, 2009. 9:29 AM REPLY 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???????????????????????????????????? Aug 13, 2008. 3:58 PM REPLY 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. Apr 9, 2008. 7:33 AM REPLY 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) ('")('") Mar 14, 2008. 9:16 PM REPLY 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. Nov 9, 2007. 8:26 PM REPLY 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.
collard41 says:
squares says:
marcastro says:
TheMadScientist says:
TechnoMancer says:
in this servo the potentiometer shaft is the shaft through the gear at the bottom of the parts picture!!!
http://www.instructables.com/id/Use-a-PIC-Microcontroller-to-Control-a-Hobby-Servo/
TechnoMancer says:
Jul 8, 2007. 11:27 PM REPLY 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!!!! Oct 17, 2006. 10:40 PM REPLY 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 Aug 15, 2006. 11:34 PM REPLY 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!
blueroomelectronics says:
offlogic says:
Relativity says:
Aug 6, 2006. 8:50 PM REPLY Thanks for making this. Have dealth with servos before but you go pretty in depth. Understood most of it. Keep up the good work!
http://www.instructables.com/id/Use-a-PIC-Microcontroller-to-Control-a-Hobby-Servo/