ESP32-Controlled 32-Channel Servo Motor Driver with Dual PCA9685 Modules
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary of the Circuit
The circuit is designed to control multiple MG90S micro servo motors using two Adafruit PCA9685 PWM Servo Breakouts, which are interfaced with an ESP32 microcontroller. The ESP32 is responsible for generating PWM signals through the PCA9685 breakout boards to control the position of the servo motors. The circuit is powered by a 5V battery connected to the PCA9685 boards and a 3.3V battery connected to the ESP32. The I2C communication protocol is used between the ESP32 and the PCA9685 boards to send control commands.
Component List
Micro Servo Motors
- MG90S Micro Servo: A compact and lightweight servo motor capable of precise position control. Each servo has three pins: Signal (SIG), Power (VCC), and Ground (GND).
PWM Servo Breakout Boards
- Adafruit PCA9685 PWM Servo Breakout: A 16-channel, 12-bit PWM controller for servo motors, with an I2C interface. Each board has multiple pins, including power (5.0V, GND, PWRIN), control (SDA, SCL, OE), and PWM output channels (PWM0-PWM15).
Microcontroller
- ESP32 (30 pin): A powerful microcontroller with Wi-Fi capabilities, featuring a variety of GPIO pins including digital, analog, and power pins (EN, VP, VN, D34-D2, TX0, RX0, TX2, RX2, 3V3, GND, Vin).
Power Sources
- 5V Battery: Provides power to the Adafruit PCA9685 PWM Servo Breakout boards.
- 3.3V Battery: Powers the ESP32 microcontroller.
Wiring Details
MG90S Micro Servo Motors
Each MG90S servo motor is connected to the Adafruit PCA9685 PWM Servo Breakout board as follows:
- SIG: Connected to one of the PWM channels on the PCA9685 board.
- VCC: Connected to the 5.0V power supply on the PCA9685 board.
- GND: Connected to the ground (GND) on the PCA9685 board.
Adafruit PCA9685 PWM Servo Breakout Boards
The two PCA9685 boards are daisy-chained for I2C communication and share a common power supply:
- 5.0V: Connected to the 5V battery's positive terminal.
- GND: Connected to the 5V battery's negative terminal and the ESP32's GND.
- PWRIN: Connected to the 5V battery's positive terminal (only on one board).
- SDA: Connected to the ESP32's D21 pin.
- SCL: Connected to the ESP32's D22 pin.
- OE: Not connected (output enable, active low).
- PWM0-PWM15: Each connected to the SIG pin of an individual MG90S servo motor.
ESP32 Microcontroller
- 3V3: Connected to the 3.3V battery's positive terminal.
- GND: Connected to the 3.3V battery's negative terminal and the PCA9685 boards' GND.
- D21 (SDA): Connected to the SDA pin on the PCA9685 boards.
- D22 (SCL): Connected to the SCL pin on the PCA9685 boards.
Power Sources
- 5V Battery: Provides power to both PCA9685 boards.
- 3.3V Battery: Powers the ESP32 microcontroller.
Documented Code
The following code is written for the ESP32 microcontroller to control the MG90S servo motors via the Adafruit PCA9685 PWM Servo Breakout boards.
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver board1 = Adafruit_PWMServoDriver(0x40);
Adafruit_PWMServoDriver board2 = Adafruit_PWMServoDriver(0x41);
#define SERVOMIN 125
#define SERVOMAX 575
int servoNumber = 0;
void setup() {
Serial.begin(9600);
Serial.println("32 channel Servo test!");
board1.begin();
board2.begin();
board1.setPWMFreq(60);
board2.setPWMFreq(60);
}
void loop() {
for(int angle = 0; angle < 181; angle += 10) {
for(int i = 0; i < 16; i++) {
board2.setPWM(i, 0, angleToPulse(angle));
board1.setPWM(i, 0, angleToPulse(angle));
}
}
delay(100);
}
int angleToPulse(int ang) {
int pulse = map(ang, 0, 180, SERVOMIN, SERVOMAX);
Serial.print("Angle: "); Serial.print(ang);
Serial.print(" pulse: "); Serial.println(pulse);
return pulse;
}
This code initializes two PCA9685 boards and sets their PWM frequency to 60 Hz, which is suitable for analog servos. It then enters a loop where it increments the angle of the servos from 0 to 180 degrees in steps of 10 degrees. The angleToPulse function converts the angle to a corresponding pulse width modulation value, which is then used to set the PWM for each servo motor on both PCA9685 boards.