# PIC Controlling Servo Motor

#### Skyferia

Joined May 5, 2010
20
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:
Rich (BB code):
// Hi-Tech C
// Crystal Oscillator used is 20MHz
#include <pic.h>
#include <htc.h>

void main()
{
while(1)
{
RB0 = 1;
__delay_ms(1.5);
RB0 = 0;
__delay_ms(20);
}
}
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)?

#### debjit625

Joined Apr 17, 2010
790
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
Rich (BB code):
// Hi-Tech C
// Crystal Oscillator used is 20MHz
#include <pic.h>
#include <htc.h>
void main()
{
while(1)
{
RB0 = 1;
__delay_ms(2);
RB0 = 0;
__delay_ms(18);
}
}
Good Luck

#### debjit625

Joined Apr 17, 2010
790
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

Joined May 5, 2010
20
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?

#### Markd77

Joined Sep 7, 2009
2,806
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.

#### Skyferia

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

#### Markd77

Joined Sep 7, 2009
2,806
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).

#### Harrington

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

device = 16F84

Rich (BB code):
' Sweeps left to right, then reverses

Symbol B1 = pw        ' create a variable pw
pw = 100              ' start at extreme left
sweep:

pulsout 0,pw          ' send pulse to motor
pause 18              ' set frequency to about 50 Hz
pw = pw + 1           ' increase pw by 1
if pw > 200 then back ' at extreme right, turn CCW
goto sweep            ' otherwise, continue
back: pulsout 0,pw    ' send pulse to motor
pause 18              ' set frequency to about 50 Hz

pw = pw - 1                  ' decrease pw by 1
if pw < 100 then sweep   ' at extreme left, turn CW
goto back                     ' otherwise, continue
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: