How does the Servo library does it's thing?

I have a query about the servo library just out of curiosity .

If I would have to write code for servo's from scratch I would propably set the pulses for all motors at the same time in a for loop and turn them off one by one by calculating the time when the next pulse is to fall and load that in the timer's registers. Every time I would write a new position to a motor I would have to recalculate the order of which pulses would fall.

Today I had a conflict with a timer, which led me to downloading the ServoTimer2 library. I was peaking around in the souece but it seems to work differently than I expected. This library can also control 8 servo's opposed to 12. I guess that is related to the fact that an 8 bit timer is used.

From what I get, a pulse is set, a time for the interrupt is loaded for when the pulse ends and then the next pulse is set. So the pulses are not happening on the same moment, correct?

Does the regular Servo library works in the same manner? One of the reasons I thought that the pulses were simultaneously was because that library could handle 12 motors. If all motors would be on 180 degrees all sequential pulses would take 24ms which would result in a frequency of 41,7Hz which is lower than 50Hz.

I do currently not have a oscilloscope or logic analizer to test it.

So could one explain to me briefly how the servo library works?

Kind regards,

Bas

The introduction:
Servo Motor Basics with Arduino | Arduino Documentation | Arduino Documentation

Also, some great YouTube videos on the subject: check w/ Google:
Arduino + Servo site:youtube.com - Google Search

This the link to the Arduino servo library.

arduino-libraries/Servo: Servo Library for Arduino (github.com)

The servo.h link, Servo/ServoTimers.h at master · arduino-libraries/Servo (github.com)

The .cpp link Servo/Servo.cpp at master · arduino-libraries/Servo (github.com)

The library is commented and the code is right there so you can figure out,

Yes. It starts one pulse, sets the timer to interrupt when the pulse is over, and then goes on to the next pulse. If that all gets done in less than 20000 microseconds (50 Hz) there is a delay so that the next cycle starts 20000 microseconds after the current cycle started. If it runs out of time, there is a very short delay before the cycle starts over.

Tnx,

So the frequency of 50Hz is maintained untill the moment you set all 12 servo's to 180 degrees (2ms pulse)?

I should get me a new scope.

Kind regards,

Bas

No excuse not to have a logic analyser.

google

 8 bit 24mhz logic analyzer

see

and cheaper very much coming out of the same factory copies for less.

It works very well. I use mine more than I thought I would. I think I got it on ebay for $12 or so.

a7

No excuse not to have a logic analyser.
True dat.

found a few on alie for < $7,- , purchasing it right now :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.