Problem using MPU6050 and BMP280 sensors together

I am working on a drone project that uses an MPU6050 and BMP280 for an Arduino flight controller. Both are running on I2C protocol; the MPU is being called with the Wire library and the BMP is using the Adafruit_BMP280_Library.

The flight controller runs on a loop of 4 ms so I am trying to make the sensors run fast. Before I added the BMP chip, the MPU6050 was running a cycle in around 600ms. When I set up the BMP chip, the MPU run time jumped to 1700ms. Does anyone know why the time is increasing?

Here is the test code I have been using:

void setup(){
  Serial.begin(57600);
  Serial.println("Starting up.");

  Wire.begin();
  Wire.setClock(400000);

  calibrate_barometer();  
  
  set_gyro_registers();               

  calibrate_gyro();
}
void loop(){
    run_gyro();
}

Setting up the BMP sensor:

void calibrate_barometer() {
  //Setup the bmp280 chip
  //Begin connection with the BMP280 chip
  bmp.begin();//Begin connection with the BMP280 chip
  //Configure the BMP280 sampling settings.  This will only be used to calculate the calibration values.
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     // Operating Mode. 
                  Adafruit_BMP280::SAMPLING_X2,     // Temp. oversampling 
                  Adafruit_BMP280::SAMPLING_X16,    // Pressure oversampling
                  Adafruit_BMP280::FILTER_X16,      // Filtering
                  Adafruit_BMP280::STANDBY_MS_1);   // Standby time set to 0.5 ms
                  
  //Let's take multiple pressure data samples on the ground so that we can determine the average base pressure reading (calibration).
  Serial.println("bmp280 ready");
  for (int cal_int = 0; cal_int < 2000 ; cal_int ++){
    altitude_cal += bmp.readPressure();
  }
  altitude_cal /= 200000;                                                    //Divide by number of cycles * 100 to get to the correct units
  
  Serial.print("Alt cal: ");
  Serial.println(altitude_cal);
  Serial.println(bmp.readAltitude(altitude_cal));
  Serial.println("bmp280 ready");
}

Setting up and running the MPU sensor

void set_gyro_registers(){
  //Setup the MPU-6050
  if(eeprom_data[31] == 1){
    Wire.beginTransmission(gyro_address);                                 //Start communication with the address found during search.
    Wire.write(0x6B);                                                     //We want to write to the PWR_MGMT_1 register (6B hex)
    Wire.write(0x00);                                                     //Set the register bits as 00000000 to activate the gyro
    Wire.endTransmission();                                               //End the transmission with the gyro.

    Wire.beginTransmission(gyro_address);                                 //Start communication with the address found during search.
    Wire.write(0x1B);                                                     //We want to write to the GYRO_CONFIG register (1B hex)
    Wire.write(0x08);                                                     //Set the register bits as 00001000 (500dps full scale)
    Wire.endTransmission();                                               //End the transmission with the gyro

    Wire.beginTransmission(gyro_address);                                 //Start communication with the address found during search.
    Wire.write(0x1C);                                                     //We want to write to the ACCEL_CONFIG register (1A hex)
    Wire.write(0x10);                                                     //Set the register bits as 00010000 (+/- 8g full scale range)
    Wire.endTransmission();                                               //End the transmission with the gyro

    //Let's perform a random register check to see if the values are written correct
    Wire.beginTransmission(gyro_address);                                 //Start communication with the address found during search
    Wire.write(0x1B);                                                     //Start reading @ register 0x1B
    Wire.endTransmission();                                               //End the transmission
    Wire.requestFrom(gyro_address, 1);                                    //Request 1 bytes from the gyro
    while(Wire.available() < 1);                                          //Wait until the 6 bytes are received
    if(Wire.read() != 0x08){                                              //Check if the value is 0x08
      digitalWrite(12,HIGH);                                              //Turn on the warning led
      while(1)delay(10);                                                  //Stay in this loop for ever
    }

    Wire.beginTransmission(gyro_address);                                 //Start communication with the address found during search
    Wire.write(0x1A);                                                     //We want to write to the CONFIG register (1A hex)
    Wire.write(0x03);                                                     //Set the register bits as 00000011 (Set Digital Low Pass Filter to ~43Hz)
    Wire.endTransmission();                                               //End the transmission with the gyro    
  }  

}


// Getting the cal offsets for the sensor
void calibrate_gyro(void) {
  //Let's take multiple gyro data samples so we can determine the average gyro offset (calibration).
  for (cal_int = 0; cal_int < 2000 ; cal_int ++){            
    if(cal_int % 15 == 0)digitalWrite(12, !digitalRead(12)); 
    gyro_signalen();       
    gyro_pitch_cal += gyro_pitch;
    gyro_roll_cal += gyro_roll;
    gyro_yaw_cal += gyro_yaw;  
    angle_pitch_acc_cal += acc_x; 
    angle_roll_acc_cal += acc_y; 
    //We don't want the esc's to be beeping annoyingly. So let's give them a 1000us puls while calibrating the gyro.
    PORTB |= B00001111;    
    delayMicroseconds(1000); 
    PORTB &= B11110000;     
    delay(3);                      
  }
  gyro_pitch_cal /= 2000; 
  gyro_roll_cal /= 2000; 
  gyro_yaw_cal /= 2000;
  angle_pitch_acc_cal /= 2000; 
  angle_roll_acc_cal /= 2000;  
}


void run_gyro(){
  //Read the MPU-6050
  test_timer = micros();
  Wire.beginTransmission(gyro_address);      //Start communication with the gyro.
  Wire.write(0x3B);                          //Start reading @ register 43h and auto increment with every read.
  Wire.endTransmission();                    //End the transmission.
  Wire.requestFrom(gyro_address,14);         //Request 14 bytes from the gyro.
  
  
  while(Wire.available() < 14);              //Wait until the 14 bytes are received.
  acc_x = Wire.read()<<8|Wire.read();        //Add the low and high byte to the acc_x variable.
  acc_y = Wire.read()<<8|Wire.read();        //Add the low and high byte to the acc_y variable.
  acc_z = Wire.read()<<8|Wire.read();        //Add the low and high byte to the acc_z variable.
  temperature = Wire.read()<<8|Wire.read();  //Add the low and high byte to the temperature variable.
  gyro_pitch = Wire.read()<<8|Wire.read();   //Read high and low part of the angular data.
  gyro_roll = Wire.read()<<8|Wire.read();    //Read high and low part of the angular data.
  gyro_yaw = Wire.read()<<8|Wire.read();     //Read high and low part of the angular data.

  acc_z *= -1;                          //Invert acc_z
  gyro_yaw *= -1;                       //Invert gyro_yaw
  

  //Compensate for gyro offsets once the calibration has been completed.
  if(cal_int == 2000){
    gyro_pitch -= gyro_pitch_cal;       //Only compensate after the calibration.
    gyro_roll -= gyro_roll_cal;         //Only compensate after the calibration.
    gyro_yaw -= gyro_yaw_cal;           //Only compensate after the calibration.
  }

  Serial.println(micros() - test_timer);
}

No, that's an excerpt. Post complete code!

Very bad idea! This is an endless loop in case the previous command failed for whatever reason.

"set up" means what? Adding the chip to the circuit or also adding some code?
"run time" is what? One loop() run?

Post the schematics of that flight controller! If you're using breakout boards for the BMP280 and/or MPU6050, post links to these board's schematics!

After wiring up the BMP and MPU and running the I2C scan, what did the I2C scan report? Didi it find 2 devices or just one or none?

Here is the full code.

#include <Wire.h>  
#include <EEPROM.h>  
#include <Adafruit_BMP280.h>

double gyro_pitch, gyro_roll, gyro_yaw;
double gyro_pitch_cal, gyro_roll_cal, gyro_yaw_cal;
int16_t angle_pitch_acc_cal, angle_roll_acc_cal;
long acc_x, acc_y, acc_z, acc_total_vector;
int temperature;
byte eeprom_data[36];
int cal_int, start, gyro_address, bmp_address;

Adafruit_BMP280 bmp;

void setup(){
  Serial.begin(57600);
  Serial.println("Starting up.");

  Wire.begin();
  Wire.setClock(400000);

  calibrate_barometer();  
  
  set_gyro_registers();               

  calibrate_gyro();
}

void loop(){
    run_gyro();
}

void calibrate_barometer() {
  //Setup the bmp280 chip
  //Begin connection with the BMP280 chip
  bmp.begin();//Begin connection with the BMP280 chip
  //Configure the BMP280 sampling settings.  This will only be used to calculate the calibration values.
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     // Operating Mode. 
                  Adafruit_BMP280::SAMPLING_X2,     // Temp. oversampling 
                  Adafruit_BMP280::SAMPLING_X16,    // Pressure oversampling
                  Adafruit_BMP280::FILTER_X16,      // Filtering
                  Adafruit_BMP280::STANDBY_MS_1);   // Standby time set to 0.5 ms
                  
  //Let's take multiple pressure data samples on the ground so that we can determine the average base pressure reading (calibration).
  Serial.println("bmp280 ready");
  for (int cal_int = 0; cal_int < 2000 ; cal_int ++){
    altitude_cal += bmp.readPressure();
  }
  altitude_cal /= 200000;                                                    //Divide by number of cycles * 100 to get to the correct units
  
  Serial.print("Alt cal: ");
  Serial.println(altitude_cal);
  Serial.println(bmp.readAltitude(altitude_cal));
  Serial.println("bmp280 ready");
}

void set_gyro_registers(){
  //Setup the MPU-6050
  if(eeprom_data[31] == 1){
    Wire.beginTransmission(gyro_address);                                 //Start communication with the address found during search.
    Wire.write(0x6B);                                                     //We want to write to the PWR_MGMT_1 register (6B hex)
    Wire.write(0x00);                                                     //Set the register bits as 00000000 to activate the gyro
    Wire.endTransmission();                                               //End the transmission with the gyro.

    Wire.beginTransmission(gyro_address);                                 //Start communication with the address found during search.
    Wire.write(0x1B);                                                     //We want to write to the GYRO_CONFIG register (1B hex)
    Wire.write(0x08);                                                     //Set the register bits as 00001000 (500dps full scale)
    Wire.endTransmission();                                               //End the transmission with the gyro

    Wire.beginTransmission(gyro_address);                                 //Start communication with the address found during search.
    Wire.write(0x1C);                                                     //We want to write to the ACCEL_CONFIG register (1A hex)
    Wire.write(0x10);                                                     //Set the register bits as 00010000 (+/- 8g full scale range)
    Wire.endTransmission();                                               //End the transmission with the gyro

    //Let's perform a random register check to see if the values are written correct
    Wire.beginTransmission(gyro_address);                                 //Start communication with the address found during search
    Wire.write(0x1B);                                                     //Start reading @ register 0x1B
    Wire.endTransmission();                                               //End the transmission
    Wire.requestFrom(gyro_address, 1);                                    //Request 1 bytes from the gyro
    while(Wire.available() < 1);                                          //Wait until the 6 bytes are received
    if(Wire.read() != 0x08){                                              //Check if the value is 0x08
      digitalWrite(12,HIGH);                                              //Turn on the warning led
      while(1)delay(10);                                                  //Stay in this loop for ever
    }

    Wire.beginTransmission(gyro_address);                                 //Start communication with the address found during search
    Wire.write(0x1A);                                                     //We want to write to the CONFIG register (1A hex)
    Wire.write(0x03);                                                     //Set the register bits as 00000011 (Set Digital Low Pass Filter to ~43Hz)
    Wire.endTransmission();                                               //End the transmission with the gyro    
  }  

}


// Getting the cal offsets for the sensor
void calibrate_gyro(void) {
  //Let's take multiple gyro data samples so we can determine the average gyro offset (calibration).
  for (cal_int = 0; cal_int < 2000 ; cal_int ++){            
    if(cal_int % 15 == 0)digitalWrite(12, !digitalRead(12)); 
    run_gyro();       
    gyro_pitch_cal += gyro_pitch;
    gyro_roll_cal += gyro_roll;
    gyro_yaw_cal += gyro_yaw;  
    angle_pitch_acc_cal += acc_x; 
    angle_roll_acc_cal += acc_y; 
    //We don't want the esc's to be beeping annoyingly. So let's give them a 1000us puls while calibrating the gyro.
    PORTB |= B00001111;    
    delayMicroseconds(1000); 
    PORTB &= B11110000;     
    delay(3);                      
  }
  gyro_pitch_cal /= 2000; 
  gyro_roll_cal /= 2000; 
  gyro_yaw_cal /= 2000;
  angle_pitch_acc_cal /= 2000; 
  angle_roll_acc_cal /= 2000;  
}


void run_gyro(){
  //Read the MPU-6050
  test_timer = micros();
  Wire.beginTransmission(gyro_address);      //Start communication with the gyro.
  Wire.write(0x3B);                          //Start reading @ register 43h and auto increment with every read.
  Wire.endTransmission();                    //End the transmission.
  Wire.requestFrom(gyro_address,14);         //Request 14 bytes from the gyro.
  
  
  while(Wire.available() < 14);              //Wait until the 14 bytes are received.
  acc_x = Wire.read()<<8|Wire.read();        //Add the low and high byte to the acc_x variable.
  acc_y = Wire.read()<<8|Wire.read();        //Add the low and high byte to the acc_y variable.
  acc_z = Wire.read()<<8|Wire.read();        //Add the low and high byte to the acc_z variable.
  temperature = Wire.read()<<8|Wire.read();  //Add the low and high byte to the temperature variable.
  gyro_pitch = Wire.read()<<8|Wire.read();   //Read high and low part of the angular data.
  gyro_roll = Wire.read()<<8|Wire.read();    //Read high and low part of the angular data.
  gyro_yaw = Wire.read()<<8|Wire.read();     //Read high and low part of the angular data.

  acc_z *= -1;                          //Invert acc_z
  gyro_yaw *= -1;                       //Invert gyro_yaw
  

  //Compensate for gyro offsets once the calibration has been completed.
  if(cal_int == 2000){
    gyro_pitch -= gyro_pitch_cal;       //Only compensate after the calibration.
    gyro_roll -= gyro_roll_cal;         //Only compensate after the calibration.
    gyro_yaw -= gyro_yaw_cal;           //Only compensate after the calibration.
  }

  Serial.println(micros() - test_timer);
}

By "set up" I meant that I called the bmp.begin() line to initiate the sensor and adding the chip to the circuit.

I think I've figured out the issue. I moved the Wire.setClock() line to call after calibrate_barometer(). I think when the BMP was initiated it reset the clock speed. After the adjustment the cycle speeds are back to normal.