MPU9250/GY-91 Magnetometer is not working

I've tried all libraries recommended on arduino.cc but non of them make a magnetometer work.

Help us help you.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Please post a schematic.

Please post an image of your project.

Please describe the problem better then you just did.

Sorry, the thing is that non of codes make it work as I've said. No errors, just not working. Like the one I've found here

#include "Wire.h"


const int MPU_ADDR = 0x68; // 0x say that we are working with hex numbers and 68 is the acc & gyro sensor adress in hex (from data sheet)
const int MAG_ADDR = 0x0C; // 0x say that we are working with hex numbers and 0C is the magnetometer sensor adress in hex (from data sheet)

int8_t Device_ID;
int8_t mag_xL, mag_xH, mag_yL, mag_yH, mag_zL, mag_zH;

int16_t mag_x, mag_y, mag_z;
int16_t acc_x, acc_y, acc_z;
int16_t gyr_x, gyr_y, gyr_z;
int16_t temp;

int8_t Msens_x,Msens_y, Msens_z;

int8_t control_1;
int8_t status_1;
int8_t status_2;
float Yaw;
float asax, asay, asaz;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  
  // Acc & Gyro Registers********************************************
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x6A);   // USER CONTROL  
  Wire.write(0x00);   // 0x00 is reset value
  Wire.endTransmission(true);

  Wire.beginTransmission(MPU_ADDR);   
  Wire.write(0x37);   //  IMU INT PIN CONFIG    
  Wire.write(0x02);   //  0x02 activate bypass in order to communicate with magnetometer
  Wire.endTransmission(true);
  delay(200);

  // Magnetometer Registers*****************************************
  Wire.beginTransmission(MAG_ADDR); 
  Wire.write(0x0B);   //  CONTROL 2
  Wire.write(0b01);   //  0 NORMAL OR 1 RESET
  Wire.endTransmission(true);
  delay(200);

  Wire.beginTransmission(MAG_ADDR);   //SLEEP MODE
  Wire.write(0x0A);   //  CONTROL 1
  Wire.write(0b00010000);   // 1 for 16 bit or 0 for 14 bit output, 0000 SLEEP MODE
  Wire.endTransmission(true);
  delay(200);

  
  Wire.beginTransmission(MAG_ADDR);   //ROM WRITE MODE
  Wire.write(0x0A);   //  CONTROL 1
  Wire.write(0b00011111); // 1 for 16 bit or 0 for 14 bit output, 1111 FUSE ROM ACCESS MODE
  Wire.endTransmission(true);
  delay(200);

  Wire.beginTransmission(MAG_ADDR);   //GET MAGNETIC SENSITIVITY DATA FOR CONVERTING RAW DATA
  Wire.write(0x10);     //  ASAX  
  Wire.endTransmission(false);
  Wire.requestFrom(MAG_ADDR, 3 , true);  //GET SENSITIVITY ADJUSMENT VALUES STARTS AT ASAX
  Msens_x = Wire.read();    //GET X SENSITIVITY ADJUSMENT VALUE
  Msens_y = Wire.read();    //GET Y SENSITIVITY ADJUSMENT VALUE
  Msens_z = Wire.read();    //GET Z SENSITIVITY ADJUSMENT VALUE
  Serial.println(Msens_x);
  Serial.println(Msens_y);
  Serial.println(Msens_z);
  Wire.endTransmission(true);
  asax = (((Msens_x-128))/256.0f)+1.0f;
  asay = (((Msens_y-128))/256.0f)+1.0f;
  asaz = (((Msens_z-128))/256.0f)+1.0f;
  Serial.print("Mx Sensitivity: ");  Serial.println(asax);
  Serial.print("My Sensitivity: ");  Serial.println(asay);
  Serial.print("Mz Sensitivity: ");  Serial.println(asaz); 
  delay(200);
  
  Wire.beginTransmission(MAG_ADDR);   //SLEEP MODE
  Wire.write(0x0A);   //  CONTROL 1
  Wire.write(0b00010000);  // 1 for 16 bit or 0 for 14 bit output, 0000 SLEEP MODE
  Wire.endTransmission(true);
  delay(200);
    
  Wire.beginTransmission(MAG_ADDR);   //CONT MODE 2
  Wire.write(0x0A);
  Wire.write(0b00010110); // 1 for 16 bit or 0 for 14 bit output, 0110 FOR CONT MODE 2 (X Hz?) 
  Wire.endTransmission(true);
  delay(200);
}

void loop() {
  Wire.beginTransmission(MAG_ADDR);
  Wire.write(0x00);
  Wire.endTransmission(false);
  Wire.requestFrom(MAG_ADDR, 1 , true);   
  Device_ID = Wire.read();
  Serial.print("Device_ID: "); Serial.println(Device_ID,DEC);  
  Wire.endTransmission(true);
 

  Wire.beginTransmission(MAG_ADDR);
  Wire.write(0x0A);
  Wire.endTransmission(false);
  Wire.requestFrom(MAG_ADDR, 1 , true);  
  control_1 = Wire.read();  // check DRDY bit if ready to read
  Serial.print("control_1: "); Serial.println(control_1,BIN);  
  Wire.endTransmission(true);

  Wire.beginTransmission(MAG_ADDR);
  Wire.write(0x02);
  Wire.endTransmission(false);
  Wire.requestFrom(MAG_ADDR, 1 , true);   
  status_1 = Wire.read();
  Serial.print("Status 1: "); Serial.println(status_1,BIN);  
  Wire.endTransmission(true);

  if(status_1 == 0b11) {
    Wire.beginTransmission(MAG_ADDR);
    Wire.write(0x03);
    Wire.endTransmission(false);
    Wire.requestFrom(MAG_ADDR, 7 , true);
   
    mag_xL = Wire.read();
    mag_xH = Wire.read();
    mag_x = (mag_xH << 8) | mag_xL;

    Serial.print("Available bytes left after reading mag x values: "); Serial.println(Wire.available(),DEC);    
    
    mag_yL = Wire.read();
    mag_yH = Wire.read();
    mag_y = (mag_yH << 8) | mag_yL;    
    
    Serial.print("LOW BITS Mag_Y: "); Serial.println(mag_yL,BIN);  
    Serial.print("HIGH BITS Mag_Y: "); Serial.println(mag_yH,BIN);
    Serial.print("FULL BITS Mag_Y: "); Serial.println(mag_y,BIN);    
    
    mag_zL = Wire.read();
    mag_zH = Wire.read();
    mag_z = (mag_zH << 8) | mag_zL;

    
    status_2 = Wire.read();   // check if there is a magnetic overflow 
    Serial.print("Status 2: "); Serial.println(status_2,BIN);  
    
  //if(status_2 != 0x08)
    
    
    
    Wire.endTransmission(true);

    Serial.print("  | mX = "); Serial.print(mag_x*asax*0.15); Serial.print(" [uT]");
    Serial.print("  | mY = "); Serial.print(mag_y*asay*0.15); Serial.print(" [uT]");
    Serial.print("  | mZ = "); Serial.print(mag_z*asaz*0.15); Serial.println(" [uT]");
  }
   
  
  //  Yaw = (atan2(mag_y,mag_x)*(180/3.141));
  //Serial.println(Yaw);  
  

  Serial.println("-------------------------------END OF THE LOOP ---------------------------");
  delay(500);

}

gives me:

Device_ID: -1
control_1: 11111111111111111111111111111111
Status 1: 11111111111111111111111111111111

in serial monitor

I2C scanner

What did the I2C scanner report?

It says
Scanning...
I2C device found at address 0x68 !
I2C device found at address 0x76 !
done

Cool so the device is connected, what happens if you use a MPU9250 library?

Which one? There are many of them

Accel and Gyro work fine but not the mag

When using a library the mag readings are not ok and when using your code the mag readings are not ok. Try another mpu.

Define "not working".

Magnetometers do not provide accurate data out of the box. They need to be calibrated.

Not connecting. Saying not presented. Not printing any info from it. Seems like it's a fake module, MPU6050 marked as MPU9250.

Very likely to be fake.

Both the MPU6050 and MPU9250 are obsolete and have not been manufactured for years.

I bought a couple of "MPU9250" modules last summer and they both turned out to be MPU6050's.

so, which one can you recommend instead?

Any modern 9DOF module from a reputable supplier, like Sparkfun, Adafruit, Pololu, etc. will outperform the obsolete ones, and you get product support and usually, money-back guarantee.

yeah) but mine is 4 times more expensive than a regular mpu6050))) Just returning it back and buying mpu6050) I've decided not to use a magnetometer for now.

Most likely the MPU6050 is an imitation or clone.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.