0
\$\begingroup\$

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...
\$\endgroup\$
1
  • \$\begingroup\$ This question relates more to Arduino.SE than EE.SE. Obviously your init code tries once, gets status, and loops with the old status without retrying. Also in many cases the I2C transactions are handled in the background, so in that case, if you have started an I2C operation and wait for it to finish, you cannot start another operation. \$\endgroup\$
    – Justme
    Commented Nov 25 at 10:31

1 Answer 1

0
\$\begingroup\$

Both I2C buses must have proper pull-up resistors on the SCL and SDA lines. Improper pull-ups can lead to signal integrity issues.

I2C address 0x68 is commonly used by the MPU6050. 0x77 is standard for BME280. Confirm that these addresses are not being reused or mistaken in your code. You can use I2C scanner code to check the I2C addresses. If you want to design MPU6050 module, you can see here:

https://www.pcbway.com/project/shareproject/MPU6050_Sensor_Module_b67e6f8d.html

\$\endgroup\$
1
  • \$\begingroup\$ In my post I displayed the output of an I2C scanner, both sensors are detected at the correct unique addresses. Even with this I can only have 1 of 2 sensors initialised, and whatever sensor is initialised last fails. \$\endgroup\$ Commented Nov 27 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.