Skip to main content

Command Palette

Search for a command to run...

Control 16 servos with Raspberry Pi + PCA9685 Driver

Updated
2 min read
Control 16 servos with Raspberry Pi + PCA9685 Driver

Hello everyone, I recently wanted to build a cool robot, and to do so, I needed to control 8 servos using the PCA9685 driver and thought I shared my experience with anyone interested in this topic.

To do this you will need:

  1. Raspberry Pi (mine is the 3B+)
  2. PCA9685 driver
  3. Servos (the driver supports up to 16)
  4. 4 female to female jumper cables.
  5. External 5v power for the driver
  6. Python3

Connection

connection.png 5v-6v recommended power and you can connect as many servos as you like

coding with python3

Turn on your Raspberry Pi and open a new terminal, then run the following commands:

sudo apt-get install git build-essential python-dev
git clone https://github.com/adafruit/Adafruit_Python_PCA9685.git
cd Adafruit_Python_PCA9685
sudo python setup.py install
cd examples

Inside the examples folder, you should find the simplest.py example code, to run it use the command

python3 simplest.py

However, if we execute this program we get an error

No such file or directory: '/dev/i2c-1'

To fix this we need to open the raspberry pi software configuration

sudo raspi-config

2021-10-06-194915_1024x768_scrot.png

Use the down arrow to select interfacing options and press enter, then select P5 I2C and enable the interface ==> ok ==> finish.

Now, if we execute the simplest.py file we shouldn't get any errors and our servo should start moving, Nice!, however the code it's unnecessarily complex for what I need. I would like to call a function that passes the target degree as a parameter and nothing else, this way the code would be easier to read and with fewer bugs, to accomplish this I'm going to use the Adafruit Servo Kit library for python

https://github.com/adafruit/Adafruit_CircuitPython_ServoKit

To use this library, open a new terminal on your Raspberry Pi and execute the following command: pip3 install adafruit-circuitpython-servokit

Usage example

Here we can see a full example:

from time import sleep
from adafruit_servokit import ServoKit

# Set channels to the number of servo channels on your kit.
# 8 for FeatherWing, 16 for Shield/HAT/Bonnet.
kit = ServoKit(channels=8)

kit.servo[0].angle = 175
sleep(1)
kit.servo[0].angle = 45
sleep(1)

We can specify the servo in thekit.servo[0-15].

Result

resultServos.jpg

I really hope this post helped you out and feel free to share this with someone that might need it, see you next time!.

I

I followed the steps, and the programme is able to execute without error, but no response from the servo. The external power for the PCA9685 is a 9V battery. I've checked the motor, and it's healthy. Any idea why the servo is not responding?

G

Hmmm, I'm not sure what the problem is, but the battery I used was a 5V not a 9V, maybe that's the problem?, also I recommend you to check the wiring, and try with the second library.

A

hey hii did you manage to run the servo using pca and raspberry pi

P
Pascal4y ago

Very nice tutorial, thanks! I followed your steps above and it has thrown this exception on run:

/bin/python /home/pi/rover/Adafruit_Python_PCA9685/examples/simpletest.py

Traceback (most recent call last):
  File "/home/pi/rover/Adafruit_Python_PCA9685/examples/simpletest.py", line 56, in <module>
    kit.servo[7].angle = 175
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_servokit.py", line 147, in __getitem__
    servo = adafruit_motor.servo.Servo(self.kit._pca.channels[servo_channel])
  File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 89, in __init__
    super().__init__(pwm_out, min_pulse=min_pulse, max_pulse=max_pulse)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 29, in __init__
    self.set_pulse_width_range(min_pulse, max_pulse)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 33, in set_pulse_width_range
    self._min_duty = int((min_pulse * self._pwm_out.frequency) / 1000000 * 0xFFFF)
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_pca9685.py", line 56, in frequency
    return self._pca.frequency
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_pca9685.py", line 134, in frequency
    return self.reference_clock_speed / 4096 / self.prescale_reg
ZeroDivisionError: float division by zero

Could you help me? Thanks a lot in advance Metrowizard

G

Hi!, I'm glad you found the article useful, the error shown above has nothing to do with the adafruit library or hardware, the problem it's clear in the last line:

zeroDivisionError: float division by zero

that means that the code it's performing a division by zero which is not possible.

Have you changed the simplest.py file? in your code, I'm seeing lines that are not found in the original one.

Edit: I've checked the library and it says that it's' deprecated, maybe that has to do with the problem, either way, I recommend you skip that test and work with the second library I showed in the post, it's simpler and works better.

Hope this helps, let me know how you do.

P
Pascal4y ago

Gary Vladimir Núñez López

Hi! Thanks for your answer. I just repeated the tutorial with the second library (--> Adafruit_CircuitPython_ServoKit).

The same kind of error:

Traceback (most recent call last):
  File "/home/pi/rover/Main.py", line 35, in <module>
    test.test()
  File "/home/pi/rover/Controllers/ServoController.py", line 12, in test
    kit.servo[7].angle = 175
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_servokit.py", line 147, in __getitem__
    servo = adafruit_motor.servo.Servo(self.kit._pca.channels[servo_channel])
  File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 89, in __init__
    super().__init__(pwm_out, min_pulse=min_pulse, max_pulse=max_pulse)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 29, in __init__
    self.set_pulse_width_range(min_pulse, max_pulse)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 33, in set_pulse_width_range
    self._min_duty = int((min_pulse * self._pwm_out.frequency) / 1000000 * 0xFFFF)
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_pca9685.py", line 56, in frequency
    return self._pca.frequency
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_pca9685.py", line 134, in frequency
    return self.reference_clock_speed / 4096 / self.prescale_reg
ZeroDivisionError: float division by zero

I expect you have nothing different running than the stock Raspian? What version of python do you use? Python3.10? I have Python3.7 on the Raspberry Pi, and I do not know how to upgrade it to 3.10...

Your help would be VERY appreciated!

A

Really nice project, I apreciate this!!

7
A

Great article!, I like the way you explained it, keep the good work :)

2

More from this blog

Gary Vladimir Núñez López Blog

19 posts

Passionate Front-End Software developer and 3D designer with a Robotics and Competitive programming background.