> For the complete documentation index, see [llms.txt](https://mbyzhang.gitbook.io/loongbian/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mbyzhang.gitbook.io/loongbian/io/adafruit-blinka-library/sample-programs/pca9685-pwm-servo.md).

# PCA9685 PWM：舵机

本样例使用了一个连接到板载 I2C 接口的 PCA9685 模块作为 PWM 驱动器，来驱动舵机。Python 程序将会控制舵机来回扫动。

## 硬件连线

| 模块角标 | 龙芯派接口          | Python 引脚常量名 |
| ---- | -------------- | ------------ |
| V+   | P2: P5V        |              |
| VCC  | P1: P3V3       |              |
| SDA  | P27: IIC0\_SDA | IIC0\_SDA    |
| SCL  | P28: IIC0\_SCL | IIC0\_SCL    |
| GND  | P9: GND        |              |

另外，您需要将舵机连接到 PCA9685 的通道 0 上。

## 软件依赖

该样例依赖 Adafruit CircuitPython PCA9685 和 Adafruit CircuitPython Motor 驱动库，可使用如下命令安装。

```
$ pip3 install -U adafruit-circuitpython-pca9685 adafruit-circuitpython-motor
```

## 代码

```python
from board import IIC0_SCL, IIC0_SDA
import busio

from adafruit_pca9685 import PCA9685
from adafruit_motor import servo

i2c = busio.I2C(IIC0_SCL, IIC0_SDA)

pca = PCA9685(i2c)
pca.frequency = 50

servo0 = servo.Servo(pca.channels[0])

while True:
    for i in range(180):
        servo0.angle = i
    for i in range(180):
        servo0.angle = 180 - i

pca.deinit()
```

程序运行后，舵机将会来回扫动。
