我有一个使用 BMI270 和 LIS2MDL 磁力计的分线板,lis2MDL SDA 和 SCL 引脚通过 BMI270 的 ASCX 和 ASDX 引脚连接。本质上使其成为 BMI270 的从设备。 BMI270 使用地址 0x68,这也应该是我与 LIS2MDL 通信的相同地址。但是,我在与 LIS2MDL 实际通信时遇到问题,但在与 IMU 通信时没有问题。在本 PDF 的第 60 页上,它介绍了通过 I2C 进行的辅助通信:
https://www.lcsc.com/datasheet/lcsc_datasheet_2207152130_Bosch-Sensortec-BMI270_C2836813.pdf
此时我非常困惑和迷失。下面是我的 Arduino 代码的一部分,最后的打印语句应该是 LIS2MDL 的芯片 ID,但是,我只得到“42”,这不应该是它。这表明我可能没有与传感器通信。此外,当我尝试从传感器获取值时,我得到“16962”但不会更新。
以下是代码:
// I2C addresses
#define BMI270_ADDR 0x68 // I2C address of BMI270
#define LIS2MDL_ADDR 0x1E // Default I2C address of LIS2MDL (on auxiliary bus)
// BMI270 Register Addresses
#define BMI270_AUX_CONFIG 0x4B // Auxiliary I2C configuration register
#define BMI270_AUX_WRITE 0x4C // Write to auxiliary device (LIS2MDL)
#define BMI270_AUX_READ 0x4D // Read from auxiliary device (LIS2MDL)
#define BMI270_CTRL 0x7E // Control register for sensor modes
#define BMI270_AUX_EN 0x44 // Auxiliary I2C enable
// LIS2MDL Register Addresses
#define LIS2MDL_WHO_AM_I 0x4F // WHO_AM_I register to verify sensor identity
#define LIS2MDL_OUTX_L 0x68 // Output registers for magnetometer data
#define SDA_PIN 21 // Define your SDA pin
#define SCL_PIN 26 // Define your SCL pin
// Variables to hold sensor data
int16_t magX, magY, magZ;
void setup() {
Wire.begin(SDA_PIN, SCL_PIN); // Start I2C communication for ESP32
Serial.begin(115200); // Start serial communication for debugging
delay(1000);
// Initialize BMI270 (primary sensor)
initBMI270();
// Enable auxiliary I2C to communicate with LIS2MDL
enableAuxiliaryI2C();
// Check communication with LIS2MDL (WHO_AM_I)
uint8_t whoAmI = readAuxRegister(LIS2MDL_WHO_AM_I);
if (whoAmI == 0x40) { // Expected WHO_AM_I response from LIS2MDL
Serial.println("LIS2MDL communication successful!");
} else {
Serial.print("LIS2MDL communication failed, WHO_AM_I: ");
Serial.println(whoAmI, HEX);
}
}
void loop() {
// Read magnetometer data from LIS2MDL through the BMI270
readMagnetometerData();
Serial.print("Magnetometer X: "); Serial.println(magX);
Serial.print("Magnetometer Y: "); Serial.println(magY);
Serial.print("Magnetometer Z: "); Serial.println(magZ);
delay(1000); // Wait before reading again
}
// Function to initialize the BMI270
void initBMI270() {
// Put BMI270 into configuration mode
writeRegister(BMI270_ADDR, BMI270_CTRL, 0x00); // Placeholder configuration mode
delay(10);
// Additional setup if required (e.g., accelerometer or gyroscope config)
Serial.println("BMI270 initialized.");
}
// Function to enable the auxiliary I2C on the BMI270
void enableAuxiliaryI2C() {
// Enable auxiliary I2C in BMI270 to talk to LIS2MDL
writeRegister(BMI270_ADDR, BMI270_AUX_CONFIG, 0x01); // Enable AUX I2C mode
delay(10); // Allow time for configuration
Serial.println("Auxiliary I2C enabled.");
}
// Function to read magnetometer data from LIS2MDL
void readMagnetometerData() {
uint8_t data[6];
// Read 6 bytes from LIS2MDL (magnetometer output)
for (int i = 0; i < 6; i++) {
data[i] = readAuxRegister(LIS2MDL_OUTX_L + i);
}
// Combine bytes into 16-bit integers (LSB first)
magX = (int16_t)(data[1] << 8 | data[0]);
magY = (int16_t)(data[3] << 8 | data[2]);
magZ = (int16_t)(data[5] << 8 | data[4]);
}
// Helper function to write to BMI270 register
void writeRegister(uint8_t address, uint8_t reg, uint8_t value) {
Wire.beginTransmission(address);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
// Helper function to read from BMI270's auxiliary I2C bus (LIS2MDL)
uint8_t readAuxRegister(uint8_t reg) {
// Set the register to read from in the auxiliary device (LIS2MDL)
writeRegister(BMI270_ADDR, BMI270_AUX_WRITE, reg);
delay(10); // Wait for the register to be set
// Read the result from the auxiliary read register of BMI270
Wire.beginTransmission(BMI270_ADDR);
Wire.write(BMI270_AUX_READ);
Wire.endTransmission(false);
Wire.requestFrom(BMI270_ADDR, 1);
if (Wire.available()) {
return Wire.read();
}
return 0xFF; // Return an invalid value if communication failed
}
我尝试对两个传感器使用预制库的组合,甚至制作了一些自定义 I2C 代码,老实说,我从未这样做过。所以我确实使用 ChatGPT 来寻求帮助。每次,我根本无法与磁力计通信,但能够与陀螺仪通信。
在Arduino示例中,有一个名为i2c_scanner的示例,运行它并检查串行监视器中是否出现两个地址。