如何修复 Arduino I2C Wire error: call of overloaded 'begin(const int&, const int&, int)' is ambiguous

问题:

你尝试使用以下命令为 I2C 调用 Wire.begin()

i2c_begin_example.ino
Wire.begin(Pin_I2C_SDA, Pin_I2C_SCL, 400000);

但你看到类似这样的错误消息

i2c_compile_error.txt
src/NyI2C.cpp:139:47: error: call of overloaded 'begin(const int&, const int&, int)' is ambiguous
src/MyI2C.cpp: In function 'void MyInitI2C()':
src/NyI2C.cpp:139:47: error: call of overloaded 'begin(const int&, const int&, int)' is ambiguous
     Wire.begin(Pin_I2C_SDA,Pin_I2C_SCL, 400000);
                                               ^
In file included from include/MyIO.hpp:2:0,
                     from src/MyI2C.cpp:2:
/home/uli/.platformio/packages/framework-arduinoespressif32/libraries/Wire/src/Wire.h:79:10: note: candidate: bool TwoWire::begin(int sda=-1, int scl=-1, uint32_t frequency=0)
     bool begin(int sda=-1, int scl=-1, uint32_t frequency=0); // returns true, if successful init of i2c bus
           ^
/home/uli/.platformio/packages/framework-arduinoespressif32/libraries/Wire/src/Wire.h:80:10: note: candidate: bool TwoWire::begin(uint8_t, int, int, uint32_t)
     bool begin(uint8_t slaveAddr, int sda=-1, int scl=-1, uint32_t frequency=0);

解决方案

这发生在特定版本的 Arduino 框架中。对我来说,它特别发生在升级到 arduino-esp32 版本 2.0.1 时。

你需要显式将第三个参数(400000)转换为 uint32_t,以告诉编译器你要调用两个函数中的哪一个:

i2c_begin_cast_example.ino
Wire.begin(Pin_I2C_SDA, Pin_I2C_SCL, (uint32_t)400000);

Check out similar posts by category: Arduino, C/C++, Embedded, PlatformIO