16-Channel 12-bit PWM/Servo Driver

To control the robot’s servo motors I use Adafruit 16-Channel 12-bit PWM/Servo Driver – I2C interface board.

I connect it to my Raspberry Pi with I2C interface and control with a simple python code:

from Adafruit_PWM_Servo_Driver import PWM

class Head:
    # define min and max values to control the servos
    MinS = 0
    MaxS = 5000

    #constructor
    def __init__(self):

        # init PWM
        self.pwm = PWM(0x40, debug=True)
        self.pwm.setPWMFreq(60)

        # setup my servo allowed values interval
        self.intervals = [[410,595],[260,530],[170,630],[220,620]]

    # set a servo:
    # the arguments:
    #    id - servo motor id, [0..3] in my case
    #    s - control value [MinS..MaxS]

    def SetServo(self, id, s):

        if s < self.MinS:
            s = self.MinS
        else:
            if s > self.MaxS:
                s = self.MaxS

        minServo = self.intervals[id][0]
        maxServo = self.intervals[id][1]

        value = int((s - self.MinS) * (maxServo - minServo ) / (self.MaxS - self.MinS) + minServo)

        self.pwm.setPWM(id, 0, value)
        return value

3 comments

Leave a Reply

Your email address will not be published. Required fields are marked *