Labs / Bipes + MicroPython / 8 Actuators (Outputs / Actuators)

8.11 Want to build a robotic arm gimbal? Precisely control multiple servos! ——I2C Bus 16-Channel PWM Servo Driver Module

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents

Young makers, today let's unlock a "super commander that can control 16 servo motors at the same time" 🤖!

Previously, controlling a single servo motor required occupying one pin on the development board. However, this 16-channel PWM servo driver module based on the PCA9685 chip only needs two I2C wires to drive 16 servo motors simultaneously, enabling the robot arms and car steering to move flexibly — it’s incredibly powerful ✨!

1. Module Introduction

Want to move multiple joints of your robot at the same time but worry that your development board doesn't have enough pins? This module is your ultimate tool for controlling multiple servo motors!

It is an I2C to multi-channel PWM output module with the PCA9685 16-channel PWM driver chip as its core: the development board only needs to send commands via the I2C interface (two wires: SDA and SCL), and the chip can output 16 independent PWM signals to precisely control the angle of each servo, perfectly solving the problem of insufficient pins on the development board.

Compared with direct driving via the pins of the development board, it has the following features:

In graphical programming, these blocks are commonly used:

In practical projects, it is often used for the following purposes:

Quick Science: Why Use the PCA9685 to Drive Multiple Servos?

Servos require precise PWM signals to control their angles, but the number of PWM pins on a development board is limited. The PCA9685 acts like a "signal distributor" — it converts the I2C commands from the development board into 16 independent PWM channels, allowing you to control the maximum number of servos with the minimum number of pins. It is an essential module for robotics projects!

2. Wiring Instructions

To make multiple servos "follow instructions", it is necessary to separate the wiring of the signal loop, the power supply loop and the servo connection respectively:

I2C 16 2800mAh

2.1 Signal Loop (Development Board ↔ Servo Driver Module)

This part transmits the control commands for the development board, requiring only 2 I2C wires. We connect the 16-channel PWM servo driver module via the I2C bus to the I2C interface I2C0 of the development board, and the corresponding relationship is as follows:

connection object

Development Board Pins

Module Pins

Description

Development Board and Module (I2C Communication)

Pin 4

SDA

I2C data transmission line, corresponding to I2C0 's SDA

Development Board and Module (I2C Communication)

Pin 5

SCL

I2C clock signal line, corresponding to I2C0 's SCL

2.2 Power Supply Circuit (Power Supply ↔ Servo Drive Module)

This part supplies operating current to the modules and servos (servos require a relatively large current, so it is recommended to use an external 5V power supply for independent power supply).

power supply

Module Pins

Description

5V power supply positive electrode

VIN

The positive power supply terminal of the module and the servo

5V Power Supply Negative Terminal (GND)

GND

The power supply negative terminals of the module and the servo need to be connected to the GND of the development board

2.3 Servo Connection (Servo Driver Module ↔ Servo)

Connect two 180° servos to the output channels of the module:

servo

Module Channel

Description

Servo 1

IO1 (Channel 0)

Corresponds to Servo ID 0 in the building blocks

Servo 2

IO2 (Channel 1)

Corresponds to Servo ID 1 in the building blocks

📌 Note

3. Install the PCA9685 library function

Before using graphical building blocks, you need to install the driver library first:

PCA9685 pca9685 I2C

4. Example: Demonstration of Cooperative Rotation of Dual Servos

Experiment Objective: Make the servo connected to IO1 (Channel 0) rotate to 45°, the servo connected to IO2 (Channel 1) rotate to 90°, reset both to 0° simultaneously after a 2-second pause, so as to intuitively experience the effect of multi-servo coordinated control.

Everyday Life Insight: A robot arm can bend multiple joints at the same time, which is achieved through the coordinated control of multiple servo motors. Now let's build a prototype of a "double-joint robot arm"!

The graphical program is as follows:

PCA9685 I2C 6 7 500 2500 0 1 45 90

Building Block Assembly Steps:

  1. Initialize the servo drive
    • Drag in Init PCA9685 Servo Driver block;
    • Set I2C to 0 (corresponding to the I2C0 interface);
    • SDA selects Pin 6 / GPIO4, SCL selects Pin 7 / GPIO5, which matches the wiring.
    • Set the Min Pulse Width to 500, and set the Max Pulse Width to 2500 (this is the pulse range adapted for 180° servos).
  2. Control the rotation of the servo
    • Drag in Position Servo block;
    • Set Servo ID (0-15) to 0 (corresponding to the servo of IO1), Angle (0-180) to 45;
    • Drag in another Position Servo block;
    • Set the Servo ID to 1 (corresponding to the servo of IO2), and set the Angle to 90.
  3. Set dwell time
    • Drag in the delay block and set it to 2 seconds → Make the servo stay at the target angle for 2 seconds.
  4. Steering gear reset
    • Drag in Position Servo block, set Servo ID 0 's Angle to 0;
    • Drag in another Position Servo block, and set Servo ID 1 's Angle to 0.

Experimental Effect:

C2178.MP4 72.0 MB

Graphical blocks will be automatically converted into MicroPython code, and the running results will be displayed:

I2C pca9685_12c 50Hz 45 50 0

Directly importable building blocks:

Servo.xml 4 KB

Tips

  1. If the servo does not rotate / rotates abnormally:
    • Check the signal wiring: verify whether the SDA and SCL pins are connected correctly, and whether there is any I2C address conflict;
    • Check power supply: Ensure that the 5V power supply for the module and the servo is sufficient (the servo draws high current, so an external power supply is recommended);
    • Check the wiring of the servo motor: connect the red wire to VIN, the brown wire to GND, and the orange wire to the signal pin; do not reverse the connections.
  2. Advanced Gameplay:
    • Add more servos to realize the multi-joint linkage of the robot arm;
    • Pair it with a sensor (such as an infrared distance sensor) to enable the servo to automatically adjust its angle based on the sensor data.

✨ Interactive Quiz

  1. Think about it: Why can the PCA9685 control 16 servos simultaneously? Without using this module, what is the maximum number of servos that can be controlled directly with the pins of a development board?
  2. Try it: Change the servo angles to 60° and 120°, and see how the rotation effect changes. Then adjust the delay time to make the movement smoother.

Summary

  1. The 16-channel PWM servo driver module based on the PCA9685 over the I2C bus is a core module for multi-servo control that enables a development board to control up to 16 servos with just 2 wires.
  2. It enables precise and coordinated control of multiple servos via I2C communication and PWM output, making it an essential tool for robot and robotic arm projects.
  3. The basic usage follows the sequence of "initialization → angle control → delay → reset", while the advanced usages include multi-servo linkage and intelligent control;
  4. Pay attention to the power supply stability of the servo; an external power supply enables the servo to rotate with greater force and more stability.