The PCA9685 controller for Arduino and other platforms is a widely used solution when you need to control multiple devices that operate with PWM signals. Although it was initially conceived to control LEDs, its versatility has allowed it to also be a frequent choice for servo control. This chip is extremely popular in robotics and automation projects for its ability to control multiple devices with precision and simplicity.
This article will not only show you how to use the PCA9685 in combination with Arduino and other microcontrollers, but it will also detail each of the technical aspects you need to know in order to get the most out of this component. We will explain everything from how to connect it to how to handle code libraries so that you can control your motors and servos with total ease.
What is PCA9685 and what is it used for?
El PCA9685 is a PWM (Pulse Width Modulation) controller designed to manage up to 16 outputs, ideal for controlling LED lights and servo motors. It communicates via the I2C bus, which means that it only needs two pins to connect to a microcontroller such as Arduino or Raspberry Pi. Through the use of specific addresses, you can even connect up to 62 of these modules to the same I2C bus, managing to control around 992 PWM outputs. This makes it a very powerful option for projects that require managing many devices in a coordinated manner.
The most widespread use of PCA9685 This is in projects where a PWM signal is needed. A clear example is the control of servos, which are managed using PWM signals. In addition, the controller has a precision of 12 bits, allowing it to generate very fine signals, with adjustable frequency up to a maximum of 1600 Hz.
Features and benefits of the PCA9685
One of the main advantages of PCA9685 This is because it takes the load off the microcontroller in terms of constantly generating PWM signals. This is especially useful in projects that involve controlling many devices, as it allows the main processor to focus on other tasks.
- 16 independent channels: Each of the 16 channels can output an independent PWM signal, allowing you to control devices such as servos, motors, and LED lights.
- I2C control: The PCA9685 uses an I2C interface to communicate with the main controller (Arduino, Raspberry Pi, etc.), and only requires two wires for communication (SDA and SCL).
- Multiple modules on a single bus: Up to 62 PCA9685 modules can be connected to the same I2C bus, managing to control up to 992 PWM outputs.
- Adjustable frequency: Supports frequencies up to 1600 Hz, although for typical servo control a frequency of 50-60 Hz is used.
Connection between Arduino and PCA9685
The connection between the PCA9685 module and an Arduino is simple, and is done using the I2C pins (SCL and SDA) and the power pins. The following table specifies the typical connections for various Arduino models:
PCA9685 pin | Arduino Uno/Elder brother | Arduino Mega | Arduino Leonardo |
---|---|---|---|
GND | GND | GND | GND |
5V | 5V | 5V | 5V |
SCL | A5 | 21 | 3 |
SDA | A4 | 20 | 2 |
In this configuration, the pins A4 and A5 or their equivalents on the corresponding platform, connect to the SDA (data) and SCL (clock) pins of the PCA9685 module. Also, it is important to have a suitable external power supply for the servo motors, especially if you are connecting multiple servos, since the Arduino does not provide enough current to drive them properly.
It is recommended to use a 5V source to power the servos, and make sure to properly connect the power pins. If you use more than 16 servos, it is also recommended to solder a 1000uF capacitor on the board to stabilize the power supply.
Configuration in the code
In order to manage the PWM outputs of the PCA9685, a library developed by Adafruit is used. You can download it from your GitHub pageHere we show you a basic example to configure the module and move a servo:
#include <Wire.h> #include <Adafruit_PWMServoDriver.h> Adafruit_PWMServoDriver servos = Adafruit_PWMServoDriver(); void setup() { servos.begin(); servos.setPWMFreq(60); // Configura la frecuencia PWM a 60Hz } void loop() { servos.setPWM(0, 0, 172); // Mueve el servo del canal 0 a la posición 0 grados delay(1000); servos.setPWM(0, 0, 565); // Mueve el servo a la posición 180 grados delay(1000); }
This simple code performs a sweep motion on the servo connected to channel 0, taking it from 0 degrees to 180 degrees slowly. You can use the function setPWM() to control each of the PCA9685 outputs independently.
Moving multiple servos at once
One of the great advantages of PCA9685 is that it allows you to control multiple servos simultaneously. Here's how you can move multiple servo motors to different positions within the same code cycle:
void loop() { setServo(0, 30); setServo(2, 90); setServo(4, 180); delay(1000); } void setServo(uint8_t n_servo, int angulo) { int duty = map(angulo, 0, 180, 172, 565); servos.setPWM(n_servo, 0, duty); }
In this case, we define a function called setServo which receives the servo number and its angle as parameters, calculates the appropriate pulse width and moves it to the desired position. This way you can easily control multiple channels.
How to adjust the limits for different servos
Not all servos have the same range of values for angles from 0° to 180°. In some cases, you will need to adjust these values yourself. Here's how you can make these adjustments for different servos:
unsigned int pos0[16]= {172, 256, 246, 246, 246, 172, 246, 200}; unsigned int pos180[16]= {565, 492, 492, 492, 492, 565, 492, 550}; void setServo(uint8_t n_servo, int angulo) { int duty = map(angulo, 0, 180, pos0[n_servo], pos180[n_servo]); servos.setPWM(n_servo, 0, duty); }
This code allows you to set the minimum and maximum values for each servo connected to the PCA9685, which will be extremely useful if you use servos with different signal ranges.
With all this in mind, you now have everything you need to configure, connect, and code your first project using the PCA9685. Whether you're working on a robot with multiple degrees of freedom, or need to control many devices in parallel, this module will allow you to do so efficiently and accurately.
Whether you're working with servos or PWM outputs, the PCA9685 makes it extremely easy to control up to 16 channels using just two pins on your microcontroller. With a little experience and good code, you can create very powerful applications without overloading the main processor.