PIC Controlling Servo Motor

Discussion in 'Programmer's Corner' started by Skyferia, Jul 15, 2010.

  1. Skyferia

    Thread Starter New Member

    May 5, 2010
    20
    0
    From what I understand, a servo motor (a hobby one, like Parallax/Futaba) requires a certain pulse coded modulation, i.e.
    >1.5ms on-time in a 20ms period - clockwise rotation
    <1.5ms on-time in a 20ms period - anti-clockwise rotation

    Am I correct on the above and is it possible to generate this by code alone?
    For example:
    Code ( (Unknown Language)):
    1. // Hi-Tech C
    2. // Crystal Oscillator used is 20MHz
    3. #include <pic.h>
    4. #include <htc.h>
    5.  
    6. void main()
    7. {
    8.      while(1)
    9.      {
    10.           RB0 = 1;
    11.           __delay_ms(1.5);
    12.           RB0 = 0;
    13.           __delay_ms(20);
    14.      }
    15. }
    16.  
    An as a side note, lets say I have 3 stepper motors being controlled by a single PIC. Is a single source of 5V sufficient for all 3 motors (i.e. 1 source for all three motors, not 1 each)?
     
  2. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    Its like this,the PCM will have its period 20ms each and on this total period of 20ms the dutty cycle (the time its high) will determine the rotation.
    So some thing like this
    Code ( (Unknown Language)):
    1.  
    2. // Hi-Tech C
    3. // Crystal Oscillator used is 20MHz
    4. #include <pic.h>
    5. #include <htc.h>
    6. void main()
    7. {
    8.      while(1)
    9.      {
    10.           RB0 = 1;
    11.           __delay_ms(2);
    12.           RB0 = 0;
    13.           __delay_ms(18);
    14.      }
    15. }
    16.  
    Good Luck
     
  3. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    And yes you can use a singal power source for 3 stepper motors if its rated within the voltage range and if the source can supply enough amount of current,For example each motor takes 5V 1Amp to run so the source should be 5V and should supply more than 3 Amp .

    Good Luck
     
    Skyferia likes this.
  4. Skyferia

    Thread Starter New Member

    May 5, 2010
    20
    0
    Thank you. Well continuing from here, I understand that to reverse the direction of the servo motor you have to change the ON-time, right?

    The problem is, doing that, the servo motor direction still spins clockwise. What could be the problem?
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Is this a servo that has been modified for continuous rotation or just a standard one?
    The standard one would turn all the way to one side with a 1ms pulse, all the way to the other with a 2ms pulse, 1.5ms would go to the middle, etc.
     
  6. Skyferia

    Thread Starter New Member

    May 5, 2010
    20
    0
    It's a Parallax one with continuous rotation. I tried using a pulse below 1ms as well, but the rotation was still clockwise...
     
  7. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I suppose it's possible the servo isn't working properly, or it could be the code. Have you got another servo you could swap with it to see where the problem is? (Or a scope to see the signal).
     
  8. Harrington

    New Member

    Dec 19, 2009
    86
    3
    I cant give you a C interpretation right now but this might throw some light on this

    device = 16F84


    Code ( (Unknown Language)):
    1.  
    2. ' Sweeps left to right, then reverses
    3.  
    4. Symbol B1 = pw        ' create a variable pw
    5. pw = 100              ' start at extreme left
    6. sweep:
    7.  
    8. pulsout 0,pw          ' send pulse to motor
    9. pause 18              ' set frequency to about 50 Hz
    10. pw = pw + 1           ' increase pw by 1
    11. if pw > 200 then back ' at extreme right, turn CCW
    12. goto sweep            ' otherwise, continue
    13. back: pulsout 0,pw    ' send pulse to motor
    14. pause 18              ' set frequency to about 50 Hz          
    15.  
    16. pw = pw - 1                  ' decrease pw by 1
    17. if pw < 100 then sweep   ' at extreme left, turn CW
    18. goto back                     ' otherwise, continue
    19.  
    20.  
    21.  
    22.  
    23.                
    Apart from this Here is some very good information re Pic DC motor control and a lot of other interesting articles In C might help you now or in the future
     
    Last edited by a moderator: Jul 18, 2010
Loading...