pythonraspberry-piservo

Servo Issues while using PWM with Raspberry Pi


My setup is simply the servo JX-PDI-6221MG being directly activated with raspberry pi's PWM through GPIO library. The servo is brand new, I followed this tutorial and set duty cycle values between 0 and 100. The angles it makes have no pattern, it seems to go one way from 20 to 50 and them from 60 to 90 the other.

I decide to loop through all possible duty cycles (code is below) and I confirm that the variations aren't smooth and it doesn't look precise. After trying to set fix values again it suddenly starts to spin in multiple directions, even without I changing anything. Not even GPIO.cleanup() or killing python related processes made it stop.

Is my Raspberry Pi getting jitter and being very imprecise or was I a victim of falsification?

Code:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
pwm=GPIO.PWM(11,330) #which is the same as shown in datasheet
pwm.start(0)

for i in range(0,100):
     pwm.ChangeDutyCycle(i)
     time.sleep(0.02)

for i in range(0,100):
     i = 100 - i
     pwm.ChangeDutyCycle(i)
     time.sleep(0.02)`

Solution

  • AFAIK, only GPIO 18 can be used by GPIO library for hardware PWM. try changing tht GPIO.OUT to that.

    Also try using pigpio library which allows ANY GPIO pin to do hardware timed PWM.