This tutorial is part of: Controlling 16 or 32 Servo motor with PCA9685
These collection of tutorial with video help you control 32 or more servo motors using Arduino UNO, Nano, Mini or ESP32 . All codes provided
Control 16 Servo Motors Using a PCA9685 Module and Arduino V2 Sketch #1: One-by-One
In this tutorial, we'll learn how to control up to 16 servo motors using a PCA9685 module and an Arduino. The PCA9685 is a 16-channel, 12-bit PWM controller that allows for precise control of servo motors. By following this guide, you'll be able to individually control each servo motor and set them to specific angles, achieving a variety of robotic movements.
We'll start by discussing the hardware components you'll need for this project, followed by detailed wiring instructions. After that, we'll go through the code step-by-step, highlighting key identifiers and their roles in controlling the servo motors. For clearer understanding, you may want to refer to the accompanying video (in video at 00:00).
Hardware Explained
The primary component in this project is the PCA9685 module, which is responsible for generating PWM signals to control the servos. Each servo is connected to one of the 16 channels on the PCA9685, allowing for independent control. The module communicates with the Arduino using I2C protocol, requiring only two wires: SDA and SCL.
In addition to the PCA9685, you'll need an Arduino board, 16 servo motors, and an external power supply. The external power supply is crucial because the Arduino alone may not provide enough current to power all servos simultaneously. Each servo typically operates at 5V, so ensure your power supply matches this requirement.
Datasheet Details
| Manufacturer | NXP Semiconductors |
|---|---|
| Part number | PCA9685 |
| Logic/IO voltage | 2.3 - 5.5 V |
| Supply voltage | 5 V |
| Output current (per channel) | 25 mA |
| PWM frequency guidance | 60 Hz |
| Input logic thresholds | 0.3VCC (low) / 0.7VCC (high) |
| Voltage drop / RDS(on) / saturation | – |
| Thermal limits | – |
| Package | TSSOP-28 / VQFN-28 |
| Notes / variants | – |
- Connect an external 5V power supply for the servos.
- Use I2C for communication, connecting SDA to A4 and SCL to A5 on the Arduino.
- Ensure all grounds are common between the Arduino and the PCA9685.
- Adjust pulse widths according to the specific servos used.
- Maintain proper cooling for the PCA9685 if powering many servos simultaneously.
Wiring Instructions
To wire the PCA9685 module to the Arduino and servo motors, start by connecting the external 5V power supply to the V+ terminal of the PCA9685. Connect the ground of the power supply to the ground terminal of the PCA9685 as well as to the Arduino's ground.
Next, connect the SDA and SCL pins from the PCA9685 to the corresponding pins on the Arduino (SDA to A4 and SCL to A5). Each servo motor will have three wires: ground (usually black or brown), VCC (typically red), and signal (often yellow or white). Connect the ground wire of each servo to the ground terminal of the PCA9685, the VCC wire to the V+ terminal, and the signal wire to the respective channels (0-15) on the PCA9685. Make sure to connect the signal wire in the correct order for each servo.
Code Examples & Walkthrough
We'll now go through the code that controls the servo motors one by one. The code begins by importing the necessary libraries for I2C communication and the PCA9685 module. The following excerpt initializes the PCA9685 object:
#include
#include
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); // Initialize PCA9685
In the setup function, we initialize the serial monitor and set the PWM frequency for the servos:
void setup() {
Serial.begin(9600); // Start serial communication
pwm.begin(); // Initialize PCA9685
pwm.setPWMFreq(60); // Set frequency to 60 Hz for servos
}
The main loop contains two nested loops: the outer loop iterates through each of the 16 servos, while the inner loop gradually changes the angle from 0 to 180 degrees:
void loop() {
for(int i=0; i<16; i++) {
for(int angle = 0; angle<181; angle += 10) {
delay(50); // Wait for servo to move
pwm.setPWM(i, 0, angleToPulse(angle)); // Set servo position
}
}
delay(1000); // Wait before repeating
}
This structure allows each servo to move to its designated angle in increments of 10 degrees, providing smooth transitions. The function angleToPulse(int ang) converts angle values into pulse widths appropriate for the servos:
int angleToPulse(int ang) {
int pulse = map(ang, 0, 180, SERVOMIN, SERVOMAX); // Map angle to pulse width
return pulse; // Return pulse width
}
This function is essential for translating the desired angle into a PWM signal that the PCA9685 can understand and transmit to the servos. For further details, remember that the full code loads below the article.
Demonstration / What to Expect
Once everything is wired correctly and the code is uploaded, you should see each servo moving to its respective angles one after the other. If you encounter any issues, double-check your connections and ensure that you have a stable power supply for the servos. If the servos are not responding as expected, verify the PWM signals being sent through the PCA9685.
Video Timestamps
- 00:00 Details of the module with chip PCA9685
- 06:14 Adding library needed for the PCA9685
- 07:14 Loading the example code
- 07:35 Code explained
- 11:31 Simplified Arduino code for PCA9685
- 12:00 Finding minimum and maximum value for your servo
- 18:27 Mapping pulse angle to pulse width
- 20:05 Creating separate method for mapping
- 20:55 Using for loop to test all angles for mapping
This tutorial is part of: Controlling 16 or 32 Servo motor with PCA9685
- Arduino Code and Video for PCA9685 16-Channel 12-Bit Servo Controller V1
- Controlling 16 Servo Motors Using a PCA9685 Module and Arduino V2 Sketch: Individual Servo Control
- Controlling 16 Servo Motors Using a PCA9685 Module and Arduino V2 Sketch #3: All Servos Together
- Controlling a 32 Servo Motor Using a PCA9685 Module and Arduino V3 Sketch #1: All Servos Together
- Controlling a 32 Servo Motor Using a PCA9685 Module and an ESP32 V4
- Control 32 Servos over Wi-Fi Using ESP32 and PCA9685 via Desktop or Mobile Phone V5
This code has not been parsed yet. Please return to the admin panel to parse it.
This code has not been parsed yet. Please return to the admin panel to parse it.
Resources & references
No resources yet.
Files📁
No files available.