MPU-6050 freezing

Hi there,

I'm building a quadcopter using the MPU-6050, but when I use the MPU-6050 in combination with my other code for some reason the arduino? freezes up. The serial monitor doesn't print any more new values, this usually happens after 15000 iterations of the loop, but when I only use the MPU-6050 without any other code in the sketch it doesnt freeze at all. So I was wondering if any of you could help me. I make use of Jeff Rowbergs library

I've already tried the following suggestions:
-Making sure the wires are connected well/connecting it straight to the arduino
-Lowering the frequency in which it reads the data
-Using freeIMU

I'm also not sure at what part of the code it hangs but it has to be happening in the part where I read the data from theMPU-6050 since if i don't include that it doesnt freeze.

void mpuData() {
  mpuInterrupt = false;
    mpuIntStatus = mpu.getIntStatus();
    fifoCount = mpu.getFIFOCount();
    
    if((mpuIntStatus & 0x10) || fifoCount >= 1024){ 
      
      mpu.resetFIFO(); 
    
    }else if(mpuIntStatus & 0x02){
    
      while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  
      mpu.getFIFOBytes(fifoBuffer, packetSize);
      
      fifoCount -= packetSize;
    
      mpu.dmpGetQuaternion(&q, fifoBuffer);
      mpu.dmpGetGravity(&gravity, &q);
      mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
    
    }
}

If there's anything you need to know or if i have forgotten something please tell

I can think of more than 10 things that could go wrong.
1 ) using 5V to the MPU-6050. Either with the VCC or else the SDA or SCL.
2 ) using too long wires.
3 ) other I2C device with the same 0x68 address.
4 ) using a mix of the optimized I2C fast functions with normal Arduino Wire library functions for other I2C devices.
5 ) out of memory
6 ) electrical noise, for example from the power supply.
7 ) using an older version of the Arduino IDE.
8 ) using a copy of Jeff Rowbers library, instead of the original one.
9 ) it doesn't freeze at all, but the Serial monitor stops working
10 ) not using a normal Arduino board.

It there something that might apply to your situation ?

1: The VCC is connected to the 3.3V so that shouldn't be an issue
4: Do you mind explaining this one?
5: How can I check for this?
6: Doubt it since the arduino is powered by USB
9: The motors don't respond either anymore so I doubt this is the case I'll check again though

These are all that I think could be relevant, do you have an idea which is the most likely to cause it?

1:
You have to check this one once more.
When you have a Mega 2560 board, the I2C bus is a 5V I2C bus and the SDA and SCL may not be connected to a MPU-6050. For the Arduino Uno or Nano or Leonardo it is no problem.
When you have a MPU-6050 module with a voltage regulator on that board, you better use 5V for VCC instead of 3.3V.

4:
The library by Jeff Rowberg is able to use the Arduino Wire library or it's own extra fast functions. They change the hardware settings of the I2C. That might cause trouble with other I2C devices.

5:
Use the function at the very bottom: Arduino Playground - AvailableMemory

6:
Usb powered ? and motors connected ? The motors could cause a voltage drop, making the Arduino or a sensor to halt. Or the motors could cause a lot of electrical noise peaks. Does it work without the motors ?
This is something that happens a lot, motors causing voltage peaks that stop the Arduino. You should look into this.

I think it is 30 percent chance it is the motors, 20 percent chance it is the MPU-6050 voltage. But I'm 80% wrong when I start guessing :o

I hope you're right about the motors or the voltage :stuck_out_tongue:

4: how does this cause trouble and how do I "fix" it?
6: The arduino is currently powered by a USB cable from my PC and the motors by a LiPo accu, but the accu doesn't do anything with the arduino it just powers the motors so I doubt that that's the issue.

4 : select the Arduino Wire functions.
It is in the I2Cdev.h, I think the Arduino Wire is used by default.

#define I2CDEV_IMPLEMENTATION I2CDEV_ARDUINO_WIRE 
//#define I2CDEV_IMPLEMENTATION I2CDEV_BUILTIN_FASTWIRE

6 : Are the grounds connected ? Which motor driver do you use ? The motor driver should indeed prevent nasty things going to the Arduino board. But it could still throw electrical noise into the I2C bus.

I'm still not sure if you connected the MPU-6050 in the right way and which MPU-6050 module you have.

4: Thanks, but one next question. Should I still include wire.h if I use fastwire? Since if i don't the connection doesn't work. (Sorry if this is a dumb question I'm rather new)

6: All the grounds are connected and the MPU-6050 should be connected right, I followed the instructions very clearly. How do you mean which module, the site at which I bought it didn't say anything about that it just said MPU-6050 3 axes gyroscope 6DOF Acceleration module. I thought there was only "one" MPU-6050. By driver I think you mean the ESC? We use the Turnigy Multistar 20 Amp Multi-rotor Brushless ESC 2-4S.

And thank you for helping me I really appreciate it.

4 : I think you don't have to include Wire.h, that is taken care for in the same I2Cdev.h:

 #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 
    #include <Wire.h>

The MPU-6050: Connect its GND to the Arduino GND. There are a few different modules. Can you make a photo your module, or give a link to it ?

The ESC : With a "motor driver" I mean a module or board that makes the motors turn. Your 'ESC' can also be called a "motor driver".
This one ? Radio Control Planes, Drones, Cars, FPV, Quadcopters and more - Hobbyking
How do you connect that to an Arduino ?
I think it is like this: Connect the Arduino GND only to the GND of signal wires (the three wires: orange red brown). Do not connect the motor ground to the Arduino and do not connect the battery ground to the Arduino.
I'm not sure if that ESC is ment to be used with an Arduino board. Do you have example code to make it possible that an Arduino can use that ESC ?

Ok so apperantly it doesn't freeze if we exclude the setup of the motors could this indicate bad wiring?

Yes, but it could also be something else.
You don't call the motor initialization functions, but have them still connected ? And that is okay ?
Well, at least we are going in a certain direction. But it could be software (out of memory, or code error) or hardware (wiring, electrical noise, something broken) so it is still hard to tell what the cause is.

It's working properly now! I've redone the wiring and it doesn't freeze anymore!
I appreciate the help you've given me to solve this, really thanks.

Greetings,

Peter_n:
When you have a Mega 2560 board, the I2C bus is a 5V I2C bus and the SDA and SCL may not be connected to a MPU-6050. For the Arduino Uno or Nano or Leonardo it is no problem.
When you have a MPU-6050 module with a voltage regulator on that board, you better use 5V for VCC instead of 3.3V.

5 years late here, but just trying to understand the above. I can see the above mentioning MEGA's I2C bus is 5V. And it says the MPU6050 CANNOT be connected to the MEGA.

But it is saying the MPU6050 CAN be connected to the UNO.

I assumed that if an MPU6050 works with a MEGA, then it would also work with an UNO. And vice versa.