搜索代码

PCA9685 16通道12位伺服控制器V1的Arduino代码和视频

PCA9685 16通道12位伺服控制器V1的Arduino代码和视频

在本教程中,我们将探索如何使用NXP半导体的PCA9685 16通道12位伺服控制器。该模块允许您精确控制多达16个伺服电机或通过脉宽调制(PWM)调暗一组LED灯。到本教程结束时,您将拥有一个能够单独或同时控制多个伺服电机的工作设置。

PCA9685 module-0

为进一步澄清教程内容,我鼓励您观看附带的视频(视频时间:00:00),以获得设置和编码过程的视觉演示。

硬件解释

PCA9685模块是一个紧凑的板,可以通过I2C通信控制多个舵机。它具有16个通道,允许您连接最多16个舵机,每个舵机都有自己的控制信号。该模块在5V电源下工作,并设计用于处理PWM信号,这对准确控制舵机的位置至关重要。

该电路板包括专用引脚用于电源(VCC)、接地(GND)和通信(SDA和SCL)。SDA引脚用于数据传输,而SCL引脚是时钟信号,两者分别连接到Arduino的模拟引脚A4和A5。这种配置确保了Arduino与PCA9685模块之间的可靠通信。

数据表详细信息

制造商NXP半导体
零件编号PCA9685
逻辑/IO电压3.3 V 到 5.5 V
供电电压2.3伏到5.5伏
每通道输出电流25 毫安最大
每通道峰值电流最大100毫安
PWM频率指导24赫兹到1.6千赫兹
输入逻辑阈值0.3伏(低)/ 0.7伏(高)
电压降 / RDS(on)/ 饱和度0.5 V最大
热限制-40 °C 到 125 °C
包裹HTSSOP-28
注释 / 变体16通道PWM控制器

  • 确保有足够电流的5V电源(推荐1A)。
  • 不要直接从Arduino供电给舵机,以免造成损坏。
  • 使用正确的 I2C 引脚:SDA 接 A4,SCL 接 A5。
  • 根据您的特定伺服器调整脉冲宽度值。
  • 检查接线以确保极性正确:GND、VCC和信号。
  • 考虑在高电流应用中进行散热。

接线说明

Arduino wiring for PCA9685 to control 16 servo motors
Arduino wiring for PCA9685 to control 16 servo motors

将PCA9685连接到您的Arduino,首先连接电源和接地。将PCA9685上的VCC引脚连接到Arduino上的5V输出。然后将PCA9685上的GND引脚连接到Arduino上的GND。接下来,将PCA9685上的SDA引脚连接到Arduino上的A4引脚,将SCL引脚连接到A5引脚。

对于伺服电机,将信号线连接到 PCA9685 的相应通道(例如,第一个伺服的 CH0),将电源线连接到独立的电源(因为伺服电机可能需要比 Arduino 能提供的更大的电流),并将地线连接到与 PCA9685 共用的接地。确保信号线、电源线和地线正确对齐,以避免损坏您的组件。

代码示例与演练

在代码的设置部分,我们初始化PCA9685模块,使用pwm.begin()并设置PWM频率为pwm.setPWMFreq(60);这设置了伺服电机的通信频率。

void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
}

在循环中,我们通过设置与期望角度相对应的PWM值来控制伺服电机。该函数angleToPulse(int ang)将角度映射到合适的脉宽,这是实现准确舵机定位所必需的。

void loop() {
  for( int angle =0; angle<181; angle +=20){
    delay(500);
    pwm.setPWM(0, 0, angleToPulse(angle) );
  }
}

最后,函数angleToPulse(int ang)将角度转换为脉冲宽度,使用定义的最小和最大脉冲长度。这使您能够根据想要达到的角度轻松控制伺服的位置信息。

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;
}

演示 / 预期内容

一旦所有连接正确且代码上传完毕,您应该会看到伺服电机按每20度的增量在指定角度间移动。如果伺服电机没有按预期工作,请检查接线是否正确,并确保电源供应充足(视频时长12:30)。

视频时间戳

  • 00:00PCA9685简介
  • 02:30- 线路说明
  • 05:00- 代码演示
  • 10:15伺服控制的演示
  • 12:30- 排查常见问题

图像

PCA9685 module-0
PCA9685 module-0
PCA9685 module-1
PCA9685 module-1
PCA9685 module-2
PCA9685 module-2
PCA9685 module-3
PCA9685 module-3
PCA9685 module
PCA9685 module
Arduino wiring for PCA9685 to control 16 servo motors
Arduino wiring for PCA9685 to control 16 servo motors
36-PCA9685 video V1: Arduino Code to control 16 servo motor, Basic
语言: C++
/*
 * Original source: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
 * This is the Arduino code for the PCA9685 16-channel servo controller.
 * Watch the video for details and demo: http://youtu.be/y8X9X10Tn1k
 *  get code and wiring diagram from http://robojax.com/RTJ27
 
 * Watch the video for this code: 
 * 
 * Related Videos:
V5 video of PCA9685 32 Servo with ESP32 with WiFi: https://youtu.be/bvqfv-FrrLM
V4 video of PCA9685 32 Servo with ESP32 (no WiFi): https://youtu.be/JFdXB8Za5Os
V3 video of PCA9685: how to control 32 servo motors: https://youtu.be/6P21wG7N6t4
V2 Video of PCA9685: 3 different ways to control servo motors: https://youtu.be/bal2STaoQ1M
V1 Video introduction to PCA9685 to control 16 servos: https://youtu.be/y8X9X10Tn1k

 * Written by Ahmad Shamshiri for Robojax Video channel: www.Robojax.com
 * Date: December 16, 2017, in Ajax, Ontario, Canada
 * Permission granted to share this code, given that this
 * note is kept with the code.
 * Disclaimer: This code is "AS IS" and for educational purposes only.
 * This code has been downloaded from https://robojax.com
 * 
 */
/*************************************************** 
  This is an example for our Adafruit 16-channel PWM & Servo driver.
  Servo test - this will drive 16 servos, one after the other.

  Pick one up today in the Adafruit shop!
  ------> http://www.adafruit.com/products/815

  These displays use I2C to communicate; 2 pins are required to  
  interface. For Arduino UNOs, that's SCL -> Analog 5, SDA -> Analog 4.

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution.
 ****************************************************/

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = 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!
#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)

// our servo # counter
uint8_t servonum = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");

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

  //yield();
}

// the code inside loop() has been updated by Robojax
void loop() {


  for( int angle =0; angle<181; angle +=20){
    delay(500);
    pwm.setPWM(0, 0, angleToPulse(angle) );
  }

  delay(1000);
 
}

/*
 * angleToPulse(int ang)
 * gets angle in degrees and returns the pulse width.
 * Also prints the value on the serial monitor.
 * Written by Ahmad Shamshiri for Robojax, Robojax.com
 */
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;
}
37-PCA9685 video V1: Arduino Code with mapping PWM
语言: C++
/*
 * Original source: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
 * This is the Arduino code for the PCA9685 16-channel servo controller.
we have simple code with mapping PWM for simplicity explained in the video at 18:27

 * Watch the video for details and demo: http://youtu.be/y8X9X10Tn1k
 get this code and wiring from for this video:  http://robojax.com/RJT27

 * Written by Ahmad Nejrabi for Robojax Video channel: www.Robojax.com
 * Date: December 15, 2017, in Ajax, Ontario, Canada
 * Permission granted to share this code, given that this
 * note is kept with the code.
 * Disclaimer: This code is "AS IS" and for educational purposes only.
 * 
 */
/*************************************************** 
  This is an example for our Adafruit 16-channel PWM & Servo driver.
  Servo test - this will drive 16 servos, one after the other.

  Pick one up today in the Adafruit shop!
  ------> http://www.adafruit.com/products/815

  These displays use I2C to communicate; 2 pins are required to  
  interface. For Arduino UNOs, that's SCL -> Analog 5, SDA -> Analog 4.

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = 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!
#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)

// our servo # counter
uint8_t servonum = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");

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

  //yield();
}

// the code inside loop() has been updated by Robojax
void loop() {

    pwm.setPWM(0, 0, 125 );
	delay(500);
    pwm.setPWM(0, 0, 255 );
	delay(500);
    pwm.setPWM(0, 0, 450 );
	delay(500);
    pwm.setPWM(0, 0, 575 );
	delay(500);	

 
}

|||您可能需要的东西

资源与参考

尚无可用资源。

文件📁

Arduino 库(zip 格式)