Control 32 Servo motor using PCA9685 Module and ESP32 V4

Project Video
Project Details

In this video we learn how to control 32 Sermo motors using PCA9685 and ESP32. This is version 4 video.

Project Code

 * Original library sourse:
 * This is the Arduino code PCA6985 16 channel servo controller
 * to control 32 Servo Motors with ESP32 board 
 * This is V3 Video on PCA9685:
 * Watch video for this code: 
 * Related Videos
V5 video of PCA9685 32 Servo with ESP32 with WiFi
V4 video of PCA9685 32 Servo with ESP32 (no WiFi):
V3 video of PCA9685 how to control 32 Servo motors
V2 Video of PCA9685 3 different ways to control Servo motors:
V1 Video introduction to PCA9685 to control 16 Servo
 * Written/updated by Ahmad Shamshiri for Robojax Video channel
 * Date: Dec 15, 2019, in Ajax, Ontario, Canada

 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * this code has been downloaded from
 * Get this code and other Arduino codes from
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on

Get early access to my videos via Patreon and have  your name mentioned at end of very 
videos I publish on YouTube here: (watch until end of this video to list of my Patrons)

or make donation using PayPal

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .


// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver board1 = Adafruit_PWMServoDriver(0x40);
Adafruit_PWMServoDriver board2 = Adafruit_PWMServoDriver(0x41);

// Depending on your servo make, the pulse width min and max may vary, you 
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
// Watch video V1 to understand the two lines below:
#define SERVOMIN  125 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  575 // this is the 'maximum' pulse length count (out of 4096)

int servoNumber = 0;

void setup() {
  Serial.println("32 channel Servo test!");

  board1.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

// the code inside loop() has been updated by Robojax
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) );
// robojax PCA9865 16 channel Servo control

 * angleToPulse(int ang)
 * gets angle in degree and returns the pulse width
 * also prints the value on seial monitor
 * written by Ahmad Nejrabi for Robojax,
int angleToPulse(int ang){
   int pulse = map(ang,0, 180, SERVOMIN,SERVOMAX);// map angle of 0 to 180 to Servo min and Servo max 
   Serial.print("Angle: ");Serial.print(ang);
   Serial.print(" pulse: ");Serial.println(pulse);
   return pulse;

If you found this Arduino course helpful, please support me so I can continue creating content like this. support me via PayPal