MPU9250 Troubles

Hi,
I am using the MPU9250 sensor (magnetometer, gyro, and accelerometer) with I2C. I have been having a few on/off problems which I was wondering if anyone would have any ideas on how to correct. The first problem is that the device is only picked up on I2C about half the time. I have to constantly re-download sketches and unplug/re-plug in the arduino to fix this. Could this problem be due to wiring or is there another possible reason? Secondly, my magnetometer only reads reasonable data for the first roughly five loops. After that, it shows only zeros as datapoints. Does anyone have any idea why?

I am using this library from github: GitHub - bolderflight/invensense-imu: Arduino and CMake library for communicating with the InvenSense MPU-6500, MPU-9250 and MPU-9255 nine-axis IMUs.
Here is my code:

#include "MPU9250.h"

MPU9250 IMU(Wire,0x68);
int status;

void setup() {
// serial to display data
Serial.begin(9600);
while(!Serial) {}

// start communication with IMU
status = IMU.begin();
if (status < 0) {
Serial.println("IMU initialization unsuccessful");
Serial.println("Check IMU wiring or try cycling power");
Serial.print("Status: ");
Serial.println(status);
while(1) {}
}
}

void loop() {
// read the sensor
IMU.readSensor();
// display the data
Serial.print("Accelerometer (X, Y, Z): ");
Serial.print(IMU.getAccelX_mss());
Serial.print(" ");
Serial.print(IMU.getAccelY_mss());
Serial.print(" ");
Serial.println(IMU.getAccelZ_mss());

Serial.print("Gyro (X, Y, Z): ");
Serial.print(IMU.getGyroX_rads());
Serial.print(" ");
Serial.print(IMU.getGyroY_rads());
Serial.print(" ");
Serial.println(IMU.getGyroZ_rads());

Serial.print("Magnetometer (X, Y, Z): ");
Serial.print(IMU.getMagX_uT());
Serial.print(" ");
Serial.print(IMU.getMagY_uT());
Serial.print(" ");
Serial.print(IMU.getMagZ_uT());
int dir = atan2(IMU.getMagY_uT(), IMU.getMagX_uT()) * ((180)/ (PI));
Serial.print(" Direction Heading: ");
Serial.println(dir);
Serial.println();
Serial.println();
Serial.println();
delay(1000);
}

Things that can go wrong:

  • A 5V Arduino board with a 3.3V sensor.
  • No pullup resistors on the I2C bus.
  • Long wires for the I2C bus.
  • Short wires, but SDA and SCL next to each other in a flat ribbon cable.
  • Own design pcb board without decoupling capacitors.
  • Arduino Due.
  • Counterfeit sensor.

Which Arduino board do you use ?
Can you give a link to the MPU-9250 module that you use ? (preferably a link to where you bought it).
Can you show a photo of the wiring ?

Please put a sketch between code-tags. You get code-tags with the </> button.

Without a a schematic I can only take a SWAG but it has the earmarks of a bad pull up resistor(s) on the I2C. Download one of the many I2C scanners and see if you get consistent results. If not disconnect everything and start with only one I2C device connected. Get it to work properly, validate the resistors, then add the second etc. You should have it up in a few minutes unless you have a faulty component which is not to likely. If the results are consistently good you probably have a software problem. Be sure your power is good, an Arduino is not a power supply.

+1 for reply #1.

Work your way through the list and tell us what works.

Hi All,
Thanks so much for your help and responses! To address your questions and concerns:

  1. The page I bought the sensor on seems to indicate that the MPU 9250 can take both 3.3V and 5V (I have also experimented with both)

  2. Here is the link I bought it: Amazon.com

  3. The wires are not that long and I am using an Arduino/Elegoo Uno- a picture of the wiring is attached (I have VCC directly connected to 5V, GND directly connected to GND, SDA directly connected to A4, and SCL directly connected to SCL).

  4. From looking down your lists, I think my problem is probably pull-up resistors, but I am not quite sure how to integrate them. How would I wire up the circuit with pull-up resistors?

Once again, thank you all so much, and any further help would be greatly appreciated!

The MPU-9250 can be powered by 5V because the module has an on board regulator. However, it is a 3.3V device and the inputs can never be exposed to 5 V.

The best option is to connect the MPU-9250 directly to a 3.3V Arduino.

The second best option is to use a two channel 5V to 3.3V level shifter to connect SDA and SCL to A4 and A5 on a 5V Arduino.

The third best (one that works ONLY with I2C connections) is to use pullup resistors (2.2K to 4.7K) connected from SDA and SCL to 3.3 V.

Hint on posting links: this is all you need.

All the rest is tracking your personal activities.