Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4501

Automation, sensing and robotics • How to make BME280 sensor and MPU6050 sensor work in same sketch with Raspberry Pi Pico W

$
0
0
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.

Code:

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:

Code:

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:

Code:

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:

Code:

Starting I2C wires...Scanning custom Wire...Scanning for I2C devices...I2C device found at address 0x68I2C scan completeMPU6050 sensor initialisedScanning default Wire...Scanning for I2C devices...I2C device found at address 0x77I2C scan completeCould not locate BME280 sensor...SensorID was: 0xD0
Output of MPU6050 failing to initialise because the BME280 did so successfully:

Code:

Starting I2C wires...Scanning default Wire...Scanning for I2C devices...I2C device found at address 0x77I2C scan completeBME280 sensor initialisedScanning custom Wire...Scanning for I2C devices...I2C device found at address 0x68I2C scan completeFailed to find MPU6050 chip...

Statistics: Posted by embedded_user — Mon Nov 25, 2024 10:11 am — Replies 0 — Views 8



Viewing all articles
Browse latest Browse all 4501

Trending Articles