# Using Servos Without Servo Library/Commands

Hello everyone and thank you so much! This is, I would think, a very basic question. But I am slightly new.

I recently began using micrcontrollers and got my Arduino only a month ago.
To this day, since a few years ago with a picaxe, I've been using preconstructed libraries made to handle servo motors. Today, I finally want to learn exactly how they work.

I understand they require PWM signals but when I used a simple analogWrite(3, 255), nothing happened. Then I did analogWrite(3, 128) and it barely moved. Shouldn't it have rotated 180 degrees and 90 degrees?

Perhaps I am getting pwm and analogWrites confused (that was my first guess).
I have the understanding that analogWrites uses PWM to simulate voltages between 0V and 5V?

I pleasseee request the code to control a servo without using the Servo class.
I appreciate all the help.

012anonymousxyz:
...servo motors... I understand they require PWM signals...

Sort of.

These should help...

I understand they require PWM signals but when I used a simple analogWrite(3, 255), nothing happened. Then I did analogWrite(3, 128) and it barely moved. Shouldn't it have rotated 180 degrees and 90 degrees?

On what do you base that thinking?

Okay...

So lets see.

if an analog output of 255 is 100% of the time on

And analog output of 0 is 0% of the time onon.

In a 22milisecond time span, to turn it 180 degrees, it must be on 2 milliseconds
That makes the ration 1:11, meaning it must be on 9.1% of the time.
0.091 x 255 = 23.205 = 23

Yes, I did read that one should not use analogWrite() function to control servos but the way I see it, the servo library must also use analogWrite at some point somehow, because in the end, ppm = pwm, because you can vary the time inbetween from 0 to whatever, increasing the width.

It is quite late up here now but I appreciate ALL the help! I will definately try analogWrite(3, 23) tomorrow.

zoomkat:

I understand they require PWM signals but when I used a simple analogWrite(3, 255), nothing happened. Then I did analogWrite(3, 128) and it barely moved. Shouldn't it have rotated 180 degrees and 90 degrees?

On what do you base that thinking?

On the basis of naive and innocent stupidity :).

I figured that full rotation = 100% on. It made sense in my head (conceptually). I didn't know it was 2 milliseconds out of 22
(I got that from 2 millisecond on time and 20 millisecond off time as per the links provided).

I made the same assumptions at one time. The reason its done this way is that in the original radio signal, other channels are multiplexed into the space between pulses.

Lots of background here if you search for RC CHannels, PPM or Servo library

rcarduino.blogspot.com

Duane B

012anonymousxyz:
Okay...

So lets see.

if an analog output of 255 is 100% of the time on

And analog output of 0 is 0% of the time onon.

In a 22milisecond time span, to turn it 180 degrees, it must be on 2 milliseconds
That makes the ration 1:11, meaning it must be on 9.1% of the time.
0.091 x 255 = 23.205 = 23

Yes, I did read that one should not use analogWrite() function to control servos but the way I see it, the servo library must also use analogWrite at some point somehow, because in the end, ppm = pwm, because you can vary the time inbetween from 0 to whatever, increasing the width.

It is quite late up here now but I appreciate ALL the help! I will definately try analogWrite(3, 23) tomorrow.

Even if you did figure out the correct duty cycle values to use with analogWrite commands, you do realize that the switching frequency of the analogWrite PWM signal is approximately 490 Hz, and a servo is expecting around a 40Hz switching frequency? analogWrite() and R/C servos are just not electrically compatible, that is why there is a servo library provided that does have the proper PWM frequency and duty cycles all figured out for your use.

Lefty

I wanted to run a servo AND use the timer1 interrupt for other purposes (like polling sensors without using delay() ), but servo.h takes over the timer1 interrupt, so I created an UNO sketch that uses the timer/counter 1 resource to control the servo in a simple manner and leaves the ISR open to add other counters. It jitters a little bit but that's OK for my purpose of just pointing a sonar. I'm using a standard Parallax servo.

(.ino code is attached)

Turret.ino (2.31 KB)