I have made init methods for both sensors inside of their own classes, and then I tried to combine them in the same sketch. I created a custom I2C object wire to be used with the MPU6050 to stop the two sensors wires from interferring with each other but I cannot get them to both work at the same time.
void setup() {
Serial.begin(115200);
while (!Serial);
// Initialise I2C wires
Serial.println("Starting I2C wires...");
// Wire.begin();
// delay(1000);
// Serial.println("Scanning default Wire...");
// i2c_scanner(Wire, "Wire (Default I2C)");
// bme280.init();
mpu6050.get_wire().begin();
delay(1000);
Serial.println("Scanning custom Wire...");
i2c_scanner(mpu6050.get_wire(), "Wire (Custom I2C)");
mpu6050.init();
Wire.begin();
delay(1000);
Serial.println("Scanning default Wire...");
i2c_scanner(Wire, "Wire (Default I2C)");
bme280.init();
}
If I call mpu6050.init()
then the BME sensor will not be located and vice versa. The I2C scanner is able to detect them both but calling the init method for either sensor causes the other one to fail. Both init methods just use begin()
and pass in the address and wire object. The BME init method:
void BME280::init() {
unsigned status;
// Wire.begin();
status = bme.begin(0x77, &Wire); // In case of sensor error, try address 0x77 (uint8_t)
if (!status) {
Serial.println("Could not locate BME280 sensor...");
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
while (1) delay(10);
}
Serial.println("BME280 sensor initialised");
}
The MPU init method:
void MPU6050::init() {
// WireMPU.begin();
if (!mpu.begin(0x68, &WireMPU, 0)) {
Serial.println("Failed to find MPU6050 chip...");
while (1) delay(10);
}
mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.println("MPU6050 sensor initialised");
delay(100);
}
Output of the BME280 failing to initialise because the MPU6050 did so successfully:
Starting I2C wires...
Scanning custom Wire...
Scanning for I2C devices...
I2C device found at address 0x68
I2C scan complete
MPU6050 sensor initialised
Scanning default Wire...
Scanning for I2C devices...
I2C device found at address 0x77
I2C scan complete
Could not locate BME280 sensor...
SensorID was: 0xD0
Output of MPU6050 failing to initialise because the BME280 did so successfully:
Starting I2C wires...
Scanning default Wire...
Scanning for I2C devices...
I2C device found at address 0x77
I2C scan complete
BME280 sensor initialised
Scanning custom Wire...
Scanning for I2C devices...
I2C device found at address 0x68
I2C scan complete
Failed to find MPU6050 chip...