树莓派下安装PCA9685的python驱动程序
目录
树莓派下安装PCA9685的python驱动程序
树莓派下安装PCA9685的python驱动程序
Adafruit Python PCA9685
Python code to use the PCA9685 PWM servo/LED controller with a Raspberry Pi or BeagleBone black.
#使用树莓派或者BeagleBone black 的Python 源码 驱动PCA9685 输出PWM波控制舵机(伺服电机)/LED灯
Installation#安装配置
To install the library from source (recommended) run the following commands on a Raspberry Pi or other Debian-based OS system:
#从源码库安装,在树莓派或者其他Debian-based系统上运行以下指令
sudo apt-get install git build-essential python-dev
cd ~
git clone
cd Adafruit_Python_PCA9685
sudo python setup.py install
#完成 以下部分为不带例子程序安装方法
Alternatively you can install from pip with:
sudo pip install adafruit-pca9685
Note that the pip install method won’t install the example code.
#以下为python源程序
#输入通道与角度。即可选通并使该通道的舵机转动到相应的角度
from __future__ import division #导入 __future__ 文件的 division 功能函数(模块、变量名....) #新的板库函数 //=
import time
# Import the PCA9685 module.
import Adafruit_PCA9685 #导入Adafruit_PCA9685模块
# Uncomment to enable debug output.
#import logging
#logging.basicConfig(level=logging.DEBUG) #调试打印日志输出
# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685() #把Adafruit_PCA9685.PCA9685()引用地址赋给PWM标签
# Alternatively specify a different address and/or bus:
#pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)
#2^12精度 角度转换成数值 #angle输入的角度值(0--180) #pulsewidth高电平占空时间(0.5ms--2.5ms) #/1000将us转换为ms #20ms时基脉冲(50HZ)
#pulse_width=((angle*11)+500)/1000; //将角度转化为500(0.5)<-->2480(2.5)的脉宽值(高电平时间) angle=180时 pulse_width=2480us(2.5ms)
#date/4096=pulse_width/20 ->有上pulse_width的计算结果得date=4096*( ((angle*11)+500)/1000 )/20 -->int date=4096((angle*11)+500)/20000;
def set_servo_angle(channel, angle): #输入角度转换成12^精度的数值
date=4096*((angle*11)+500)/20000 #进行四舍五入运算 date=int(4096*((angle*11)+500)/(20000)+0.5)
pwm.set_pwm(channel, 0, date)
# Set frequency to 50hz, good for servos.
pwm.set_pwm_freq(50)
print('Moving servo on channel x, press Ctrl-C to quit...')
while True:
# Move servo on channel O between extremes.
channel=int(input('pleas input channel:'))
angle=int(input('pleas input angle:'))
set_servo_angle(channel, angle)
#time.sleep(1)