Electronic – arduino – Checking the understanding of how hobby servo motors operate

arduinohobbyistpwmservo

Can someone tell me if my understanding is correct please.

The control signal to a hobby servo motor that is connected to an arduino operates by :

In every 1 s there are fifty windows each of length 20ms.
Thus the frequency is 50 Hz right ?

Moving on,

If the servo shaft is at its center and I want it to go 90 degrees left, in each of the 20ms of the 1s, the PWM pin will generate a PWM signal that has 1 ms of pulse width until the pulse has traveled for 1s and then for the next second generate something like 1.25ms?

or will it start at 1 ms and during that 1 s period and change from 1ms to a different value 1.25 ms and 1.5ms as it comes closer and closer to the desired angle( decreases in angle ) ?

Best Answer

The hobby servo needs a signal like this:

In a 20ms window, (50 times a second) a pulse with a width between approximately 1ms and 2ms (may vary, depending on servo) with a middle point of 1.5ms. So in the 20ms window, there is almost 90% of the time being "silence" with no signal level. The pulse is only high for a very short (relatively) period.

The Arduino pins are default output to either 500Hz and 3.9Khz. You cannot directly use the pin in PWM mode (with analogWrite), because it will be receiving pulses too fast, and will bug out/malfunction/won't do anything useful.

The Servo library available for inclusion to an Arduino sketch uses the built-in timers of the ATMEGA328P (or other chip, depends on which arduino you are using) and some fancy software and interrupts to get the proper 50Hz timing, and uses an ordinary digital output pin to send the required pulse.

You can do this yourself in a simple loop, if you only control 1 servo and have a simple program, by simply setting a digital pin HIGH and then delaying (using DelayMicroeconds) for between 1000 and 2000 microseconds depending on what position you want to move to, then setting the pin LOW and delaying for (20,000 - the time you delayed for the servo).

You can send the same signal constantly and the servo will stay at that point, or you can send it until it reaches there and stop sending a signal. The servo should stay at that point, but I do not think it will remember the point (you can move it with your hand and it will stay at the new point, until a new signal is sent again).

It's better not to try and use the PWM hardware outputs for the Arduino to control a servo, even if you do manage to pre-scale the timers down to the required 50Hz (I think this would be hard to get, but I believe you can do it!) as the PWM output registers will need to be set with 8 bit resolution in a tiny range of 10-20%, which only gives you 25 total positions available to the servo (that is pretty bad, 25 positions for a whole +-90 degree movement range!). So go with the Servo Library, basically :)