Troubleshooting with i2c devices (emergency)

Hi guys, iam recently working in a project and iam using arduino nano, mpu6050 and bmp280 and for communicating with them I am using i2c bus, first i always like to check weather the sensor is working or not and for that i always use the examples in the arduino, to check mpu6050 i used MPU6050_raw and to check bmp280 i used bmp280test and the both sensors are working perfectly and I just merged the two codes and make it has one and I upload this code and I see the values in the serial monitor and this time also it works perfectly and I got good data from the both sensors

but after few days I again connect the sensors with arduino nano and I again upload same code for mpu6050(MPU6050_raw) that used in the first and in serial monitor it shows :

Initializing I2C devices…
Testing device connections…
MPU6050 connection failed

After that I again upload same code for bmp280(bmp280test) that used in the first and in serial monitor it shows :

BMP280 test
Could not find a valid BMP280 sensor, check wiring!

and this thing is happening again and again

it really surprise me because everything working perfectly but suddenly this things happen and i also tried other arduino nano and same thing happen again

i don’t know what mistake iam doing exactly, so pls pls help me to solve this plssss

CODE FOR MPU6050 :
#include “I2Cdev.h”
#include “MPU6050.h”

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include “Wire.h”
#endif

MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define OUTPUT_READABLE_ACCELGYRO

#define LED_PIN 13
bool blinkState = false;

void setup() {
// join I2C bus (I2Cdev library doesn’t do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif

// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it’s really up to you depending on your project)
Serial.begin(38400);

// initialize device
Serial.println(“Initializing I2C devices…”);
accelgyro.initialize();

// verify connection
Serial.println(“Testing device connections…”);
Serial.println(accelgyro.testConnection() ? “MPU6050 connection successful” : “MPU6050 connection failed”);

// use the code below to change accel/gyro offset values
/*
Serial.println(“Updating internal sensor offsets…”);
// -76 -2359 1688 0 0 0
Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
Serial.print("\n");
accelgyro.setXGyroOffset(220);
accelgyro.setYGyroOffset(76);
accelgyro.setZGyroOffset(-85);
Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
Serial.print("\n");
*/

// configure Arduino LED pin for output
pinMode(LED_PIN, OUTPUT);
}

void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

// these methods (and a few others) are also available
//accelgyro.getAcceleration(&ax, &ay, &az);
//accelgyro.getRotation(&gx, &gy, &gz);

#ifdef OUTPUT_READABLE_ACCELGYRO
// display tab-separated accel/gyro x/y/z values
Serial.print(“a/g:\t”);
Serial.print(ax); Serial.print("\t");
Serial.print(ay); Serial.print("\t");
Serial.print(az); Serial.print("\t");
Serial.print(gx); Serial.print("\t");
Serial.print(gy); Serial.print("\t");
Serial.println(gz);
#endif

#ifdef OUTPUT_BINARY_ACCELGYRO
Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
#endif

// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}

CODE FOR BMP280 :

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

#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);

void setup() {
Serial.begin(9600);
Serial.println(F(“BMP280 test”));

if (!bmp.begin()) {
Serial.println(F(“Could not find a valid BMP280 sensor, check wiring!”));
while (1);
}

/* Default settings from datasheet. /
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_500); /
Standby time. */
}

void loop() {
Serial.print(F(“Temperature = “));
Serial.print(bmp.readTemperature());
Serial.println(” *C”);

Serial.print(F(“Pressure = “));
Serial.print(bmp.readPressure());
Serial.println(” Pa”);

Serial.print(F(“Approx altitude = “));
Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println(” m”);

Serial.println();
delay(2000);
}

sorry for the emojis in the code but the code is exactly as it is in the MPU6050_RAW in examples of arduino ide

Edit your post.

Type [code] before your code.
Type [/code] after your code.

And your emojis will be gone :wink:

ok, thank you, then what about my problem

I suggest that you check your wiring. If you disconnect your sensors and connect them again and it does not work, that seems a clear indication to me that your wiring is faulty.

This can be bad wires, this can be bad contacts.

And please fix your opening post.