MPU9250 magnetmeter

Hello all,
I have an MPU9250, and I am using this Github post, and I am trying to make a head tracker for FPV. I uploaded i2c_scanner, and the gyro came up as 0x68, and the magnetometer came up as 0x0C. After uploading the head tracker firmware, I opened the GUI, and started the plot. after about a second, the plot locked up. After that the magnetometer did not come up in the i2c_scanner. Does anyone else know what could be causing this? This is the link to the GUI I used. Any help would be appreciated.

Micah

Check the wiring for loose or bad connections.

Improper power or mismatched processor/sensor operating voltages can also lead to failure.

Hello, all the connections are fine, but I cannot read the magnetometer. I suspect that somehow the i2c got disabled on the magnetometer, like this datasheet says. I don't know much about i2c, but could you point me to the right direction as to how to write a reset bit just so I could try it?

In the unlikely event that your suspicion is correct, the data sheet recommends two different reset procedures, on pages 52 and 53.

My question is how to actually write a i2c bit with the reset bit, sorry if I am not making sense.

It is time to learn something about I2C. There are plenty of tutorials on how to read and write specific registers, turned up with the search phrase "arduino i2c tutorial".

I suggest to start with a very simple program that reads one register from the sensor, and work your way up from there. This one includes information for the MPU-6050, which is very similar.

Hello, I came to realize that the problem was something else, but thank you for your time! I have clicked add Karma.

Micah

Thanks. Please consider posting what the actual problem turned out to be, in case someone else on the forum has the same symptoms.

Hello, this is the code that I got to detect the magnetometer, for some reason it needs to have 0x37 written to i2c to make it detect the magnetometer.

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    https://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>

void setup() {
  Wire.begin();

  Serial.begin(115200);
  while (!Serial); // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}

void loop() {
  int nDevices = 0;

  Serial.println("Scanning...");
  Wire.beginTransmission(0x68);
  Wire.write(0x37);
  Wire.write(1<<1);
  Wire.endTransmission();
  Wire.beginTransmission(0x68);
  Wire.write(0x37);
  Wire.endTransmission();
  Wire.requestFrom(0x68,1);
  int value = Wire.read();
  Wire.endTransmission();
  //Serial.println(value,HEX);
  //return;
  for (byte address = 1; address < 127; ++address) {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    delay(10); // Wait 5 seconds for next scan
    Wire.beginTransmission(address);
    byte error = Wire.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.print(address, HEX);
      Serial.println("  !");

      ++nDevices;
    } else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
    } 
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  } else {
    Serial.println("done\n");
  }
  delay(5000); // Wait 5 seconds for next scan
}

I hope it helps!.

Micah

It is not clear to me what that code does.

This part bypasses the I2C interface pins, if the I2C interface is disabled.

  Wire.beginTransmission(0x68);
  Wire.write(0x37);
  Wire.write(1<<1);  //set bit BYPASS_EN in register 55
  Wire.endTransmission();

This part is not a proper I2C transaction, so it is anyone’s guess what it does. Perhaps it undoes the previous command.

  Wire.beginTransmission(0x68);
  Wire.write(0x37);
  Wire.endTransmission();

The MPu9250's, I have used, has its magnetometer on the little board that the MPU9250 is on. Typically, I request a read of the magnetometer from the MPU9250.

Hello and thank you for your replies, I started messing around with the MPU9250 library that the code that I am using uses, and I realized just freezes after a few seconds . :frowning: Here is the code that I have written up currently:

#include <MPU9250.h>
MPU9250 IMU(Wire, 0x68);
int status;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Initializing");
  // 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);
    return;
  } else {
    Serial.println("MPU9250 initialized");
  }
  // setting the accelerometer full scale range to +/-8G
  IMU.setAccelRange(MPU9250::ACCEL_RANGE_8G);
  // setting the gyroscope full scale range to +/-500 deg/s
  IMU.setGyroRange(MPU9250::GYRO_RANGE_500DPS);
  // setting DLPF bandwidth to 20 Hz
  IMU.setDlpfBandwidth(MPU9250::DLPF_BANDWIDTH_20HZ);
  // setting SRD to 19 for a 50 Hz update rate
  IMU.setSrd(19);
}

void loop() {
  IMU.readSensor();
  Serial.println(IMU.getMagX_uT());
  delay(50);
}

Has anyone had similar issues?

After trying out several MPU9250 libraries, I ended up settling on the BloderFlight library, GitHub - bolderflight/mpu9250: MPU-9250 sensor driver..

I now use my own MPU9250 SPI library written using the ESP32 SPI API.

@Idahowalker, the bolderflight library is the one that I am using. Perhaps I should look into SPI? What all does that involve?

Well you'd have had to bought a module that is both I2C and SPI, then a little research on your part.
BolderFlight documentation.
https://github.com/bolderflight/MPU9250/blob/master/README.md#:~:text=SPI%20Object%20Declaratioon%20MPU9250%20(SPIClass%20%26bus%2Cuint8_t%20csPin)%20An,select%20pin%20can%20be%20any%20available%20digital%20pin..

As simple as your code is and my strong belief in the library you are using, you might look at the wiring, check the soldering of the header, try pull up resistors on the I2C SDA and SDO lines.
Add in serial prints to see where its freezing

void loop() {
Serial.println( "!" );
  IMU.readSensor();
Serial.println( "@" );
  Serial.println(IMU.getMagX_uT());
  delay(50);
}[color=#222222]

Just to clarify, I have some other code that I found for calculating the pitch and roll, and when running that, it doesn’t freeze, it just freezes when I am reading the magnetometer. And I am using the arduino nano FIY.

What happens when you run the Bolder I2C Example?

It freezes after a bit. It is like something in the magnetometer is freezing it. Perhaps it is a hardware issue?

Could be?

Since the MPU-9250 has been discontinued, it is very likely that any modules showing up on Amazon, eBay, AliBaba, etc. are made from fake, recycled or rejected parts.

In fact just about any sensor module you buy from those outlets is likely to be counterfeit.

You will be MUCH better off buying a more modern sensor, from a reputable company that supports their products. I recommend the MinIMU-9 series from Pololu.

Currently, the more advanced AltIMU-10 v3 is on sale for half price.