Arduino locks up when reading I2C data from MPU6050 accelerometer

Hi all,

My 1284p based Arduino keeps locking up after 5 to 20 minutes when I’m reading data from my MPU6050 accelerometer. I’ve cut my code back to basics as much as possible and it’s still doing it.

Here’s my sketch:
cannot enter due to forum’s 9000 character limit

I’ve attached it, but here’es the part it hangs on:

Serial.print("a");
    fifoCount = mpu.getFIFOCount();
    Serial.print("b");

Using a couple of well placed Serial prints I have narrowed the problem down to the line.

fifoCount = mpu.getFIFOCount();

I know this because when the program freezes, the serial monitor has printed “a” and never the corresponding "B’.

Anyway I have included the .cpp file that contains the getFIFOCount() function as an attachment, but here is the function below to hopefully make it easier to identify.

uint16_t MPU6050::getFIFOCount() {
    I2Cdev::readBytes(devAddr, MPU6050_RA_FIFO_COUNTH, 2, buffer);
    return (((uint16_t)buffer[0]) << 8) | buffer[1];
}

Can anyone help me solve this crashing issue? I’ve been at it for days, searched many forums and tried so many different things, but none stop it crashing.

And I don’t know if it helps, but the MPU6050 is on a GY521 breakout board, supplied with 5v, which the board’s regulator converts to 3.3v. The SDA and SCL lines at 3.3v. my 1284p is 3.3v volts.

MPU6050.cpp (124 KB)

MPU6050_dig_int.ino (13.7 KB)

Nobody?

dave_sausages:
Nobody?

You might have a hardware problem. The standard Wire library will hang in an infinite loop if the I2c bus is busy.

When it locks up measure the voltage on SDA and SCL. An inactive bus should read Vcc. in your case 3.3V

Chuck

So if the bus is monetarily active will the wire library hang infinitely? Or will it only hang for the time the bus is busy?

Where did you get the I2Cdev library?

uint16_t MPU6050::getFIFOCount() {

// I presume it is hanging here
    I2Cdev::readBytes(devAddr, MPU6050_RA_FIFO_COUNTH, 2, buffer);

    return (((uint16_t)buffer[0]) << 8) | buffer[1];
}

dave_sausages:
So if the bus is monetarily active will the wire library hang infinitely? Or will it only hang for the time the bus is busy?

As long as the Bus is LOW, or the Arduino has seen a START without a matching STOP.

Chuck.

SurferTim:
Where did you get the I2Cdev library?

I got it from here: (I've just diff compared and it is the same).

And I've just noticed this in the .cpp

 #elif ARDUINO > 100
            /*#warning Using current Arduino IDE with Wire library is functionally limiting.
            #warning Arduino IDE v1.6.5+ with I2CDEV_BUILTIN_FASTWIRE implementation is recommended.
            #warning This I2Cdev implementation does not support:
            #warning - Timeout detection (some Wire requests block forever)*/
        #endif

Forgive me, but how do I implement fastwire?