# PCA9685 PWM：舵机

## 硬件连线

| 模块角标 | 龙芯派接口          | 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()
```

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mbyzhang.gitbook.io/loongbian/io/adafruit-blinka-library/sample-programs/pca9685-pwm-servo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
