0

Greet everyone, I am using JavaScript with pigpio to develop my learning project.

How come my servo motor keeps shaking when I drive it by program PWM? codes are posted as follow:

    var servo1GPIO = new Gpio(20, { mode: Gpio.OUTPUT });

    var func = () => {
        servo1GPIO.digitalWrite(1);
        setTimeout(() => {
            servo1GPIO.digitalWrite(0);
        }, 2);
    };

    setInterval(func, 20);

The above code demonstrate how my servo motor was drove with a 2ms duty cycle, but keep shakes...

However, if I use the function servoWrite(pulseWidth) built in by pigpio, it works perfectly. May I know what is the different between these two approaches?

2

The length of the pulse is used by the servo hardware to set the angle of the servo horn. If the length of the pulses differ then the servo will be continually shifting angle. Typically a 10 µs difference in length of pulse will result in a 1 degree difference in angle.

With software timed pulses the accuracy is not very precise and can easily change by several microseconds from pulse to pulse resulting in visible jitter.

With hardware timed pulses this is much less likely as the pulses are more accurately timed.

The servoWrite method uses hardware timed PWM.

| improve this answer | |
  • Is hardware timed PWM the same with hardware PWM? Seems it is only available on certain pin only. How come servoWrite() can be used on every pin? – mannok Mar 29 at 10:50
  • No, pigpio can provide hardware timed PWM on all GPIO. That's what servoWrite uses. – joan Mar 29 at 11:08

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.