Search code examples
pythonraspberry-pii2cservo

Connect Adafruit_PCA9685- I2C interface in Raspberry pi Python


How to Connect Adafruit_PCA9685- I2C using Raspberry Pi and Python?


Solution

  • You could be able to individually 16 servo motor run at a time using Raspberry pi and python.I used reconnect_io_func() function which will check is connected with raspberry pi, if not then will continue try to connect until successful connect.Then will be run servo motor value

    from __future__ import division
    import Adafruit_PCA9685
    
    
    def reconnect_io_func():
        try:
            pwm = Adafruit_PCA9685.PCA9685()
            return pwm
        except Exception as error:
            if "Remote I/O error" in (error):
                reconnect_io = True
                while reconnect_io:
                    try:
                        # print("while Error: "+str(error))
                        pwm = Adafruit_PCA9685.PCA9685()
                        # print(pwm)
                        reconnect_io = False
                        return pwm
                    except Exception as error:
                        # print((error))
                        reconnect_io = True
    
    
    servo_min = 150  # Min pulse length out of 4096
    servo_max = 600  # Max pulse length out of 4096
    pwm = reconnect_io_func()
    pwm.set_pwm_freq(60)
    
    if __name__ == '__main__':
        pwm.set_pwm(0, 0, 420)
        pwm.set_pwm(1, 0, 420)
        pwm.set_pwm(2, 0, 307)
        pwm.set_pwm(3, 0, 420)
        pwm.set_pwm(4, 0, 307)
        pwm.set_pwm(5, 0, 307)
        pwm.set_pwm(6, 0, 307)
        pwm.set_pwm(7, 0, 307)
        pwm.set_pwm(8, 0, 0)
        # print (" i am here")