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:
- Raspberry Pi (mine is the 3B+)
- PCA9685 driver
- Servos (the driver supports up to 16)
- 4 female to female jumper cables.
- External 5v power for the driver
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
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
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
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.angle = 175 sleep(1) kit.servo.angle = 45 sleep(1)
We can specify the servo in the
I really hope this post helped you out and feel free to share this with someone that might need it, see you next time!.
Did you find this article valuable?
Support Gary Vladimir Núñez López by becoming a sponsor. Any amount is appreciated!