MPU6050 I2C lockup when serial used.

Hello All,

I’m having some real head scratching issues using the MPU6050 sensor via I2C and Hardware Serial1 with an XBee connected to pins 0/1.

I’m using the Jeff Rowberg code for the MPU but not using DMP. This sketch will run fine continously for long periods as long as it doesn’t receive data from serial1. If I get my PC to send to the XBee at a data rate of around 16Hz, the sketch will intermittently lockup after a relatively short period of time - 10-30 secs.

This is running on a Genuino Zero and a Leonardo. I can only assume some interrupt problem between I2C and hardware serial.

I’ve come across what seems like very similar problems with people using an RFDuino MPU6050 and GPS connected to hardware serial where the GPS will cause the lockup.

Has anyone solved this in software?

Here’s my sketch - you’ll note I’ve not even opened the hardware serial port for this to happen - makes no difference whether it’s open or not:

#include <MPU6050.h>

MPU6050 accelgyro;

#define LED_PIN 13 
bool blinkState = false;

unsigned long Hz100Loop = 0;
unsigned long Hz10Loop = 0;

void loop10Hz()
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);


void loop100Hz()
    int16_t ax,ay,az,gx,gy,gz;

    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);


void setup() {



    if (accelgyro.testConnection())
        Serial.println("test ok");

    pinMode(LED_PIN, OUTPUT);


// the loop function runs over and over again until power down or reset
void loop() {

    int delta100Hz = millis()-Hz100Loop;
    int delta10Hz = millis()-Hz10Loop;
    if (delta100Hz>=10) 

    if (delta10Hz>=100) 



Sounds like a buffer overflow problem in serial1, in which case serial1 is storing incoming characters regardless of whether you open the port. That would be a bug in the Arduino core.

It seems like that. But if I don’t read from the IMU, it doesn’t happen.

A further update to this shows that with the MPU library debugging turned on, I2CDev locks up at the point of reading 14 bytes from the MPU in the readBytes function. This is consistent repeatable behaviour. There is a blocking call made to which never seems to return.

So the problem is manifesting itself in the Arduino Wire library which ultimately seems to end up in calls to RingBuffer for available() and read_char()

I'm using Arduino Library 1.6.7.