I tried to control many servo motors with PCA9685 and Arduino.
I need to run many servo motors independently on a multi-legged robot, and Arduino has only a few pins that can output PWM signals, which is not enough.
Let’s give it a try.
I used SG90 servo motors.
#include <Wire.h>
#include <PCA9685.h> //Header file for PCA9685(Made by Akizuki Denshi Co. :https://akizukidenshi.com/catalog/g/g110350/)
PCA9685 pwm = PCA9685(0x40); //Addressing PCA9685 (when address jumper not connected)
#define SERVOMIN 102 //min pulse width
#define SERVOMAX 492 //max pulse width
void setup() {
pwm.begin(); //initial setting (for address 0x40)
pwm.setPWMFreq(50); //frequency of PWM for SG90 (for address 0x40)
}
int n=0;
void loop() {
n=0;
while (n<=180) {
for(int i=0; i<16; i++){
servo_write(i,n);
delay(40);
}
n=n+20 ;
}
}
void servo_write(int ch, int ang){
ang = map(ang, 0, 180, SERVOMIN, SERVOMAX); //Converts angle (0 to 180) to PWM pulse width (102 to 492)
pwm.setPWM(ch, 0, ang);
//delay(1);
}Since I had only 11 SG90s, I connected 11 for the time being, but the same code can be used to connect up to 16 servomotors. We were able to confirm that all servo motors are independent of each other.



コメント