You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BMI270辅助I2C通信LIS2MDL磁传感器故障排查求助

问题描述
  • 使用搭载BMI270和LIS2MDL磁传感器的breakout board,LIS2MDL的SDA、SCL引脚连接至BMI270的ASCX、ASDX引脚,作为BMI270的辅助I2C从设备
  • BMI270的I2C地址为0x68,可正常与BMI270通信,但无法与LIS2MDL建立有效连接
  • 读取LIS2MDL的WHO_AM_I寄存器返回0x42(预期值为0x40),读取磁力计数据得到固定值16962且无更新
  • 已尝试使用现成传感器库及自定义I2C代码,均未解决问题
  • 参考过BMI270数据手册第60页的辅助I2C通信章节,仍存在困惑
Arduino代码
// 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
}
连接原理图

传感器连接原理图


内容的提问来源于stack exchange,提问作者Yesuah Balderas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 14:02:09