MPU 9150 compass data reading -1

Hi everyone
I am getting a -1 for all the compass data. Please help.

Chandra :slight_smile:

Here is the code I am using

void read_mag_data(int cmps[3])
{

MPU9150_writeSensor(MPU9150_INT_PIN_CFG, 0x02); //Set Bypass to Magnetometer
MPU9150_I2C_ADDRESS = MPU9150_CMPS_ADDR; //change Adress to Compass
MPU9150_writeSensor(0x0A, 0x00); //PowerDownMode
MPU9150_writeSensor(0x0A, 0x0F); //SelfTest
MPU9150_writeSensor(0x0A, 0x00); //PowerDownMode
delay(10);
MPU9150_writeSensor(0x0A,0x01); // Enable Magnetometer
delay(10);
cmps[0]=MPU9150_readSensor(MPU9150_CMPS_XOUT_L,MPU9150_CMPS_XOUT_H);
cmps[1]=MPU9150_readSensor(MPU9150_CMPS_YOUT_L,MPU9150_CMPS_YOUT_H);
cmps[2]=MPU9150_readSensor(MPU9150_CMPS_ZOUT_L,MPU9150_CMPS_ZOUT_H);

MPU9150_I2C_ADDRESS=MPU9150_ADDR_DEFAULT;
}

int MPU9150_readSensor(int addrL, int addrH){
Wire.beginTransmission(MPU9150_I2C_ADDRESS);
Wire.write(addrL);
Wire.endTransmission(false);

Wire.requestFrom(MPU9150_I2C_ADDRESS, 1, true);
byte L = Wire.read();

Wire.beginTransmission(MPU9150_I2C_ADDRESS);
Wire.write(addrH);
Wire.endTransmission(false);

Wire.requestFrom(MPU9150_I2C_ADDRESS, 1, true);
byte H = Wire.read();

return (H<<8)+L;
}

The first ting that I would do is run the I2C Scanner (by Nick Gammon) to make sure of the device address and that it’s communicating with the bus.

#include <Wire.h>

void setup() {
  Serial.begin (115200);

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

Hi
I am getting the reading now. Just some small coding mistakes.
Thanks anyways.

Chandra :slight_smile: