0

我是 Arduino 和 Esp32 编程的新手。我需要将 PCA9685 控制器连接到 ESP-32 cam 才能控制多个伺服电机,但 SCL 和 SDA 引脚被 UART 控制板占用。

我搜索了这个,发现了一些与Wire.hwire.begin()和 TwoWire 相关的东西,但无法实现它。



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

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


#define SERVOMIN  150 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // This is the 'maximum' pulse length count (out of 4096)
#define USMIN  600 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150
#define USMAX  2400 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600
#define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates

// our servo # counter
uint8_t servonum = 0;

void setup() {
 Wire.begin();
  Wire.begin(2,15,100000); // this is to map new SCL and SDA pins to 2,15 
  Serial.begin(9600);
  Serial.println("8 channel Servo test!");

  pwm.begin();
  
  pwm.setOscillatorFrequency(27000000);
  pwm.setPWMFreq(SERVO_FREQ);  // Analog servos run at ~50 Hz updates

  delay(10);
}

// You can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. It's not precise!
void setServoPulse(uint8_t n, double pulse) {
  double pulselength;
  
  pulselength = 1000000;   // 1,000,000 us per second
  pulselength /= SERVO_FREQ;   // Analog servos run at ~60 Hz updates
  Serial.print(pulselength); Serial.println(" us per period"); 
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit"); 
  pulse *= 1000000;  // convert input seconds to us
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
}

void loop() {
   pwm.setPWM(0, 0, 250);
  
}

这是我要运行的测试代码。我从https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/得到了实现的想法

我遇到了一些问题,但代码没有响应-

  1. 当 PCA9685 连接到 esp32-cam 并且我尝试上传代码时,它不起作用并给出致命错误。
  2. 在上传前按下重置按钮时,内置闪光灯会闪烁。这是一个问题,因为在尝试上传没有附加 PCA9685 的代码时,上传的代码没有任何致命错误并且没有“闪烁”。
  3. 我尝试了来自上述同一网站的代码,该代码查找 I2C 设备并打印其地址。即使连接了伺服控制器,它也没有显示任何设备。

PS-我是这个领域的绝对初学者。

4

0 回答 0