Control servo motor using PCA9685

日本語のページ

You can control servo motors using the PCA9685, which can output PWM signals.

PCA9685

PCA9685-related blocks are located in the "Motor" - "PCA9685" group.

Blocks of "Motor" - "PCA9685" group

Wiring

The PCA9685 connects to the microcontroller via I2C.
The pin numbers used for I2C connection vary depending on the microcontroller.
For more information, see the "I2C Connection" page.

For example, with a plain ESP32, connect SDA to pin 21 and SCL to pin 22.
Also, connect VCC and GND to the microcontroller's 3V3 (or 5V) and GND pins.

Connect the servo motor to the 3-pin terminals for PWM/V+/GND (0-15).
Also, connect an external power supply (5-6V) to the V+ and GND terminal blocks.

When using an ESP32 as the microcontroller and connecting a servo motor to pin 0 of the PCA9685, the wiring should look like the diagram below.

Wiring the ESP32 and PCA9685

Initialization

For microcontrollers with fixed I2C pin numbers, such as the original ESP32, use the "PCA9685 Initialization" block.

Initialize PCA9685

Also, for microcontrollers with variable I2C pin numbers, such as the ESP32-S3, use a block to specify the pin number during initialization.

Initialize PCA9685 by specifying I2C pin number

Rotating a Servo Motor

You can use the rotation block to rotate a servo motor from 0 to 180 degrees.
Specify pin numbers 0 through 15 and the desired rotation angle.

Servo Motor Rotation

Specifying the Pulse Width

Depending on the servo motor, it may not rotate sufficiently from 0 to 180 degrees.
In that case, try adjusting the pulse width corresponding to 0 and 180 degrees using the pulse width specification block.
By default, the pulse widths corresponding to 0 and 180 degrees are 500 and 2550, respectively.

Pulse Width Setting

Example

This example shows how to rotate two servo motors connected to a PCA9685 over a range of 0 to 180 degrees.
The microcontroller has a variable I2C interface, and SDA and SCL are connected to pins 4 and 5, respectively.
This program can be downloaded from here.

Example of running the PCA9685