I am trying to control precisely (without glitches) several servo-motors from a computer through GPIO. Final position, speed and ideally acceleration need to be controlled.

Currently I generate the PWM signal by software, but software seem not precise enough (due mostly to kernel context switches, the PWM signal is some times several micro-seconds late, producing some "shaking"). I know several way to improve, for instance implementing the signal from a driver (kernel) context, but the problem will inherently stay.


How to generate a PWM signal, based on software indications. Which solution is recommended?

  1. Building the logic myself: That seem an overkill: 2 clocks, plenty of logical gates.. Even if probably achievable, the final result would take too much space.
  2. Using an intermediate pic, dedicated to provide precise PWM, the protocol between the computer and the pic to be defined. Could be a solution, I am not sure however of the result or if there are better ways.
  3. Looking over internet shops by PWM, servo controller, ... I did not found any chip which integrate this feature. Probably I miss an important concept.
  4. other?
  • \$\begingroup\$ Find an MCU with enough PWM pins to suit your requirements and add some communication (UART? SPI? I2C?) to configure it. You may actually find some existing I2C IO expanders with PWM outputs, not sure. \$\endgroup\$ – Tom Carpenter Apr 22 '16 at 15:23
  • \$\begingroup\$ In fact yeah, type "i2c pwm" into google. First result. Couldn't have looked very hard for point 3. \$\endgroup\$ – Tom Carpenter Apr 22 '16 at 15:23
  • \$\begingroup\$ @TomCarpenter: Thanks! Well, remove i2c and you will get 10 pages of pwm to analog and software-pulse pwm pages. It look stupid, but without experience, all topics seem quite obscure. \$\endgroup\$ – Adrian Maire Apr 22 '16 at 15:42
  • \$\begingroup\$ Supposing MCU = microcontroller I expect option 2 to be the solution. Correct? Looking at "i2c pwm", I am probably blind, or maybe Google is specially well train to found electronic for your account/ip. In any case, Not a single Chip for I2C->PWM in several pages of search. Do you have any chip reference which I could start with? \$\endgroup\$ – Adrian Maire Apr 22 '16 at 16:56
  • \$\begingroup\$ First link: adafruit.com/product/815 \$\endgroup\$ – Tom Carpenter Apr 22 '16 at 16:58

One solution is to use small CPUs with hardware PWM. Once you run out of hardware, add another CPU. You need slow enough PWM and enough bits to handle the 1-2ms pulses with sufficient resolution- not all micros will have this.

Its also possible to do firmware PWM with a small-ish CPU-- let's say you want <1usec jitter and resolution, you could update several servos one at a time within a (say) 20ms frame. You would have to worry about not overflowing whatever buffering is available in the micro (say by interleaving communication).

And finally, you could do a hybrid by steering hardware timer pulses from a single timer through a demultiplexer to individual servos. This might be the easiest of all because the micro would have lots of time to deal with communications (assuming it is interrupt driven).


Best practical solution is to buy of-the-shelf servo and control it from your software by communication lile UART or ethernet. It's because the people who make servo do it better than you and cheaper then you. And they actually have it done, while you are just starting.

Still, let me explain what is inside a typical servo. There is a microcontroller with built-in pwm peripheral, that is set on startup to certain frequency and the duty cycle is updated each cycle according to control loops outputs. Normally one MCU can control two motors, sometimes three or even more.

The servo firmware normally has current, velocity and position loops, and depending on vendor advanced control algorithms (error correction, ecam, input shaping, you name it).

More advanced architecture has one master controller with cool cpu inside and separate power stages connected by communication to the master.

A decent dual axis servo costs around $500. I am sure, time you are going to waste building it by yourself costs more.

If you need hints on such servo, i suggest for Galil (simple, but almost high end), Adtek QS9 (chinese high end, very simple and practical), then you have beckhof, yaskawa, elmo- good, but more expensive and complicated.


You are getting jitter out of a software implementation because generally computer kernels are not realtime. As such, anything may get prioritized before your PWM switch. You may reduce the problem by elevating you controller's priority, but the problem still won't go away, since drivers and other kernel modules may still have priority.

To really fix the problem in the computer side would require using a realtime kernel. Linux has the RTAI project. MATLAB has its own simple OS to run systems on. But generally my opinion is that it's usually simpler to just have a MCU (microcontroller) do the hard work in bare metal. A simple Arduino will be cheap/easy/simple enough. If you want more PWM channels you may try an ARM board (there are some ARM-based Arduinos now too).

If you really need the controller to be on the PC side, then either you have to go realtime or you must design you controller to allow jitter when sending information to the MCU (if using USB you may get delay jitter), but then the PWM waveform will still be unaffected.

  • 1
    \$\begingroup\$ No real time kernel guarantees pwm switching of 10khz or so. It requires hardwares. Maybe very simple, but additional. And of course for powering the motor and reading it's feedback you need even more hardware. \$\endgroup\$ – Gregory Kornblum Apr 22 '16 at 17:18
  • \$\begingroup\$ @GregoryKornblum Surely a 2GHz computer can do a 10KHz PWM. But, granted, with a full OS, even RTAI, it must be difficult to do so. However, the OP is trying to control a servo system. Unless those are industrial (usually such a question would come from someone controlling RC hobby servos) they won't 10kHz, only around 200Hz plus something to get better resolution. Anyway, that's exactly why I'd rather use an MCU. \$\endgroup\$ – Ronan Paixão Apr 22 '16 at 17:53
  • \$\begingroup\$ 200hz is not practical. Motor coils have time constant shorter than 5msec. MCU would be fine for someone who does such projects daily. Otherwise it's waste of time. \$\endgroup\$ – Gregory Kornblum Apr 22 '16 at 17:56
  • \$\begingroup\$ By the way, if you use an external MCU, you can communicate with it any way you want, be it using I2C, SPI, UART, USB (if supported) or anything else. And depending on the number of channels needed an Arduino-like may be cheaper than an I2C to PWM converter. \$\endgroup\$ – Ronan Paixão Apr 22 '16 at 18:00
  • \$\begingroup\$ Yes! Eventually you will have industrial grade servo, but after two years and without right tools for motor tuning. \$\endgroup\$ – Gregory Kornblum Apr 22 '16 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.