Controlling more than 3 ServoMotors with PCA9685

Hey there,

my first question here. I'm trying to simply control several servo motors with the pwm-controller PCA9685(Controlled via smbus). Everything works as exspected with 1, 2 and 3 motors. My test is that i choose a random number for every motor in the allowed radius and then move it in this position. If i choose 4 motors, the first moves are successful but after time every motor seems to be "confused" and starts moving random. I'm using an external power supply (USB supply 5V max. 2A - i opened it and used the power cables). At first it looked like an issue with the current but now that i upgraded from a 0.15A to a 2A Power supply and this really should be enough. My source code is as follows:

#!/usr/bin/env python

import smbus2
import math
import time
import random

bus = smbus2.SMBus(1)
prescalevalue = 25000000.0
prescalevalue /= 4096.0
prescalevalue /= float(50) #50hz
prescalevalue -= 1.0
prescalevalue = int(prescalevalue)
mode1 = bus.read_byte_data(0x40, 0x00)
bus.write_byte_data(0x40, 0x00, (mode1 | (1>>4))) # set sleep bit to 1
bus.write_byte_data(0x40, 0xFE, prescalevalue)
bus.write_byte_data(0x40, 0x00, (mode1 & ~(1>>4))) # set sleep bit to 0
time.sleep(0.5) # more than enough time (doc: 0.005 sec)

def set_pwm(motor, on, off):
    bus.write_byte_data(0x40, 0x06 + 4 * motor, on & 0xFF) # ON_L
    bus.write_byte_data(0x40, 0x07 + 4 * motor, on >> 8) # ON_H
    bus.write_byte_data(0x40, 0x08 + 4 * motor, off & 0xFF) # OFF_L
    bus.write_byte_data(0x40, 0x09 + 4 * motor, off >> 8) # OFF_H

min = int(1.0/20.0 * 4096.0) + 1 # correct accoring to doc but needs to be tested
max = int(2.0/20.0 * 4096.0) + 1 # same

# connected motors on 0 - 5
# for i in range(3) works fine, with 4 afters several rounds it starts spinning completely random
while True:
    for i in range(4):
        value = random.randint(min,max)
        set_pwm(i, 0, value)

Motors: Micro Servo SG90

I hope someone of you can help me... It is probably a really stupid mistake i made... Thank you all, Arz0c

Hi, Welcome to the forum.

The problem is your code is in python, and this forum is a Arduino IDE forum. There may be some members that can help you.

4 servos, will need 4A supply to cover the current spikes when you ask a servo to move, if you have any force on the servo arms you may find that the servo uses more current.

Use a DMM and measure your servo supply when your fault occurs.

Tom.... :)

Thank you very much for the fast answer. Sorry, i wasn't aware that this forum is for Arduino IDE only. I just thought this would be a good place to start searching for a solution :) The problem is that i don't have a DMM and it seems these are pretty expensive... But as you pointed out the 2A still seem to be too less current so I guess I'll search for another way to power these. Ultimately I try to use even 6 Motors so there is a pretty large current needed. Do you have any idea how I could realize this?

Hi, Check on ebay for a cheap DMM, you should be able to get a decent one for $20.00 Aus or less.

If you find one ask here if you are unsure about its suitability.

Some forum membere here do use python, so give the world a chance to turn at least once and see what happens.

Tom.. :)