树莓派连接驱动板控制舵机

By Heanny
2022-07-28
2078 read

硬件

  1. 16路PWM Servo 舵机驱动板模块PCA9685
  2. 树莓派
  3. MG90S 9g舵机

连接

  • GND -> RPi GND
  • SCL -> RPi SCL1
  • SDA -> RPi SDA1
  • VCC -> RPi 3.3V
  • V+ -> RPi 5V

VCC 引脚只是为芯片供电,如果要连接舵机或者 LED 灯,就使用 V+引脚供电,V+引脚支持 3.3~6V 的电源(芯片的安全电压时 5V)。 建议通过电源接线柱外接电源供电。
大多数的舵机设计电压都是在 5~6V,尤其在多个舵机同时运行时,跟需要有大功率的电源供电。

demo

# coding:utf-8
# 导入 __future__ 文件的 division 是为了精确除法。
from __future__ import division
import time

# 导入Adafruit_PCA9685模块,需要安装
import Adafruit_PCA9685

# 使用默认地址(0x40)初始化PCA9685。
# 把Adafruit_PCA9685.PCA9685()引用地址赋给PWM标签
pwm = Adafruit_PCA9685.PCA9685()

# 或者指定不同的地址和/或总线:
# pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)

def set_servo_angle(channel, angle):  # 输入角度转换成12^精度的数值
    # + 0.5是进行四舍五入运算。
    date = int(4096 * ((angle * 11) + 500) / 20000 + 0.5)
    pwm.set_pwm(channel, 0, date)

# 将频率设置为50赫兹,适合伺服系统。
pwm.set_pwm_freq(50)

print('Moving servo on channel x, press Ctrl-C to quit...')
while True:
    # 选择需要移动的伺服电机的通道与角度
    channel = int(input('pleas input channel:'))
    angle = int(input('pleas input angle:'))
    set_servo_angle(channel, angle)
    time.sleep(1)
树莓派红外控制空调
[rustdesk]远程控制中继服务器

Comments

暂无评论,还不快来坐沙发...

Leave a Reply