Control 16 servos with Raspberry Pi + PCA9685 Driver

👋 Front-End Developer with a flair for competitive programming & robotics. Expert in 3D CAD design, YouTube educator. Innovating from Oaxaca, México.
Search for a command to run...

👋 Front-End Developer with a flair for competitive programming & robotics. Expert in 3D CAD design, YouTube educator. Innovating from Oaxaca, México.
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?
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.
hey hii did you manage to run the servo using pca and raspberry pi
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
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.
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!
Great article!, I like the way you explained it, keep the good work :)
Hello everyone 👋, welcome back to another article, today are going to dive into a very cool graph algorithm called "Tarjan", we are going to see what it is, how it works, what it's used for, and we are going to upsolve two very cool competitive prog...

Introduction Hello everyone! 😄 Welcome back to my blog! Today, we're diving into the fascinating world of greedy algorithms in competitive programming. If you've ever wondered how to spot a problem screaming for a greedy solution or how to tackle th...

Introduction Hey there, fellow coders! Today, we're going to navigate through the exciting world of dynamic programming, tackling a captivating competitive programming problem along the way. We'll be stepping into the shoes of game strategists, playi...

Introduction Hello everyone, in this post we'll be mastering the 2D prefix sum array and 2D segment Tree while we solve a very interesting competitive programming problem to better understand these structures and their capabilities. Forest Queries Pr...

Introduction Hello everyone, in this post we will look in detail at the solution to two similar, very classic DP problems: 'Roads' and 'Organizing Tiles', as well as their implementation in C++, altho the solution can be implemented in the language o...

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.
5v-6v recommended power and you can connect as many servos as you like
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

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
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].

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