Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

LSM303D - Device identification register not correct and ACC/MAG values not changing even though I2C is working


#1

Hi… I am using the LSM303D part on 10DOF sensor board and i am communicating it with esp8266. I am not able to read the correct Device identification register(WHO_AM_I = 0x49) and after writing all the control registers correctly the output doesnot change. I am also reading all the registers to see if i am able to write into registers properly. Attached excel sheet has the registers.

LSM303S_registers.xlsx (14.5 KB)

My code:

#include <Wire.h>
 
byte Address = 0B00011110;

byte OUT_X_L_M          = 0B00001000;
byte OUT_X_H_M          = 0B00001001;
byte OUT_Y_L_M          = 0B00001010;
byte OUT_Y_H_M          = 0B00001011;
byte OUT_Z_L_M          = 0B00001100;
byte OUT_Z_H_M          = 0B00001101;
byte WHO_AM_I           = 0B00001111;
byte OUT_X_L_A          = 0B00101000;
byte OUT_X_H_A          = 0B00101001;
byte OUT_Y_L_A          = 0B00101010;
byte OUT_Y_H_A          = 0B00101011;
byte OUT_Z_L_A          = 0B00101100;
byte OUT_Z_H_A          = 0B00101101;

void setup()
{
  Wire.begin();
 
  Serial.begin(9600);
  while (!Serial);       
        
  Serial.println("\nI2C Scanner");

  delay(5000);

    byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(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.
    Wire.beginTransmission(address);
    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.print("  !; WHO_AM_I register: ");
      Wire.beginTransmission(address);
      Wire.write(0x0F);                                                          //Start reading @ register 0x1B
      Wire.endTransmission();                                                    //End the transmission
      Wire.requestFrom(address, 1);                                         //Request 1 bytes from the gyro
      while(Wire.available() < 1);                                               //Wait until the byte is received
      Serial.println(Wire.read(),HEX);

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknow 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");


      for(byte i=1; i<64;i++) {
      Wire.beginTransmission(0x1E);
      Wire.write(i);                                                          //Start reading @ register 0x1B
      Wire.endTransmission(0);                                                    //End the transmission
      Wire.requestFrom(0x1E,1);                                    //Request 1 bytes from the gyro
      while(Wire.available() < 1)
      Serial.println("xxx");
      Serial.print("0x");
      Serial.print(i,HEX);                //Wait until the byte is received
      Serial.print(": 0x");
      Serial.println(Wire.read(),HEX);
      }
     
      Wire.beginTransmission(0x1E);
      Wire.write(0x1F);                                                          //Start reading @ register 0x1B
      Wire.write(0x00);
      Wire.endTransmission();

      Wire.beginTransmission(0x1E);
      Wire.write(0x20);                                                          //Start reading @ register 0x1B
      Wire.write(0x37);
      Wire.endTransmission();

      Wire.beginTransmission(0x1E);
      Wire.write(0x21);                                                          //Start reading @ register 0x1B
      Wire.write(0x00);
      Wire.endTransmission();

      Wire.beginTransmission(0x1E);
      Wire.write(0x22);                                                          //Start reading @ register 0x1B
      Wire.write(0x00);
      Wire.endTransmission();

      Wire.beginTransmission(0x1E);
      Wire.write(0x23);                                                          //Start reading @ register 0x1B
      Wire.write(0x00);
      Wire.endTransmission();

      Wire.beginTransmission(0x1E);
      Wire.write(0x24);                                                          //Start reading @ register 0x1B
      Wire.write(0x90);
      Wire.endTransmission();

      Wire.beginTransmission(0x1E);
      Wire.write(0x25);                                                          //Start reading @ register 0x1B
      Wire.write(0x40);
      Wire.endTransmission();

      Wire.beginTransmission(0x1E);
      Wire.write(0x26);                                                          //Start reading @ register 0x1B
      Wire.write(0x00);
      Wire.endTransmission();

      Wire.beginTransmission(0x1E);
      Wire.write(0x2E);                                                          //Start reading @ register 0x1B
      Wire.write(0x00);
      Wire.endTransmission();

      Serial.println("--------------------------------------------------------------------------");
delay(1000);

      for(byte i=1; i<64;i++) {
        Wire.beginTransmission(0x1E);
        Wire.write(i);                                                          //Start reading @ register 0x1B
        Wire.endTransmission(0);                                                    //End the transmission
      Wire.requestFrom(0x1E,1);                                    //Request 1 bytes from the gyro
      while(Wire.available() < 1)
      Serial.println("xxx");
      Serial.print("0x");
      Serial.print(i,HEX);                //Wait until the byte is received
      Serial.print(": 0x");
      Serial.println(Wire.read(),HEX);
      }
  
}
 
void loop()
{

byte Who = ReadRegister(WHO_AM_I);
byte X_L_M = ReadRegister(OUT_X_L_M);
byte X_H_M = ReadRegister(OUT_X_H_M);
byte Y_L_M = ReadRegister(OUT_Y_L_M);
byte Y_H_M = ReadRegister(OUT_Y_H_M);
byte Z_L_M = ReadRegister(OUT_Z_L_M);
byte Z_H_M = ReadRegister(OUT_Z_H_M);

byte X_L_A = ReadRegister(OUT_X_L_A);
byte X_H_A = ReadRegister(OUT_X_H_A);
byte Y_L_A = ReadRegister(OUT_Y_L_A);
byte Y_H_A = ReadRegister(OUT_Y_H_A);
byte Z_L_A = ReadRegister(OUT_Z_L_A);
byte Z_H_A = ReadRegister(OUT_Z_H_A);


int X_AXIS_M = X_H_M <<8 | X_L_M;
int Y_AXIS_M = Y_H_M <<8 | Y_L_M;
int Z_AXIS_M = Z_H_M <<8 | Z_L_M;

int X_AXIS_A = X_H_A <<8 | X_L_A;
int Y_AXIS_A = Y_H_A <<8 | Y_L_A;
int Z_AXIS_A = Z_H_A <<8 | Z_L_A;

Serial.print("X-Axis_M: ");
Serial.print(X_AXIS_M, HEX);
Serial.println(" Gauss");
Serial.print("Y-Axis_M: ");
Serial.print(Y_AXIS_M, HEX);
Serial.println(" Gauss");
Serial.print("Z-Axis_M: ");
Serial.print(Z_AXIS_M, HEX);
Serial.println(" Gauss");

Serial.print("X-Axis_A: ");
Serial.print(X_AXIS_A, HEX);
Serial.println(" G");
Serial.print("Y-Axis_A: ");
Serial.print(Y_AXIS_A, HEX);
Serial.println(" G");
Serial.print("Z-Axis_A: ");
Serial.print(Z_AXIS_A, HEX);
Serial.println(" G");

delay(2000);
}

byte ReadRegister(int Register){
byte result = 0;
Wire.beginTransmission(Address); //slave ID start talking
//ask for info in register
Wire.write(Register);
//complete the send
Wire.endTransmission(0);
//Request 1 byte

Wire.requestFrom(Address , 1);
//wait for info
while( Wire.available() == 0)
Serial.println("xxxxxxxxxx");
result = Wire.read();  
//get info
Wire.endTransmission();
return(result);  
}

void WriteRegister(byte Register, byte Value){
  Wire.beginTransmission(Address );
  Wire.write(Register);
  Wire.write(Value);
  Wire.endTransmission();
}

Please help!!


#2

Also when i do a I2C scan as shown in the code, I get two addresses 0x19 and 0x1E.


#3

Hello.

Those addresses are consistent with the sensors on an LSM303DLHC. Can you confirm which board you have? Also, can you try running the Serial.ino sketch from our LSM303 Arduino library and see if it works with your ESP8266?

-Jon


#4

Thanks Jonathan for your reply.
Seems like i was seeing the wrong datasheet for the part. :slight_frown:
i might have accidently written into some of the reserved registers. Do you think it has damaged the chip?
how can i confirm that everything works correctly?

BTW the LSM303 library works fine for reading the accelerometer values.


#5

Hello.

If the example sketch is working, then your LSM303DLHC is probably fine. If you suspect you are getting strange behavior, feel free to post a sample of the raw data you are getting here, and I would be happy to take a look.

-Jon


#6

Hi Jon

I have also run some code for calculating pitch,roll,heading from the acc/mag data. The values look good even without calibrating. I am still worried whether they will cause any instablility issues later in my drone.
I am adding the raw data for the Serial sketch in LSM303 Library. Please look at the data and see if they are correct or anything strange you find in the data.

A:    -16   3408 -16960    M:    261    544    189
A:      0   3408 -17024    M:    261    544    189
A:    -16   3408 -16960    M:    255    540    183
A:    -64   3392 -16912    M:    262    539    191
A:    -96   3408 -16928    M:    257    538    188
A:    -16   3344 -16944    M:    259    541    189
A:      0   3376 -16864    M:    259    541    189
A:    -80   3424 -17024    M:    256    542    188
A:     32   3328 -17024    M:    260    538    189
A:    -16   3376 -16944    M:    258    542    186
A:    -32   3424 -17008    M:    258    542    186
A:     16   3408 -17008    M:    262    539    186
A:     32   3456 -16976    M:    260    542    190
A:    -16   3424 -17008    M:    260    537    189
A:      0   3408 -16944    M:    260    545    190
A:    -32   3376 -16912    M:    260    545    190
A:    -32   3440 -17024    M:    259    538    186
A:    -32   3424 -17024    M:    258    544    189
A:    -64   3392 -16976    M:    257    536    186
A:    -48   3472 -17104    M:    259    546    187
A:    -16   3456 -17040    M:    259    546    187
A:    -32   3376 -17024    M:    256    535    187
A:    -80   3344 -16976    M:    256    547    192
A:    -48   3376 -16960    M:    260    537    183
A:    -48   3408 -16960    M:    260    546    192
A:    -96   3376 -16960    M:    260    546    192
A:    -48   3360 -17056    M:    259    534    184
A:     64   3360 -17008    M:    258    547    192
A:    -80   3424 -16848    M:    257    535    185
A:    -48   3408 -16880    M:    257    535    185
A:    -96   3376 -17008    M:    260    545    192
A:    -48   3360 -16976    M:    256    535    182
A:    -80   3344 -17024    M:    262    547    192
A:    -48   3248 -16976    M:    256    533    181
A:     16   3328 -16848    M:    256    533    181
A:    -96   3440 -16960    M:    262    547    192
A:    -32   3408 -17008    M:    257    531    184
A:    -48   3280 -17024    M:    263    550    192
A:   -112   3360 -16992    M:    255    532    180
A:     16   3376 -16944    M:    255    532    180
A:    -80   3424 -16912    M:    264    552    195
A:    -48   3344 -16976    M:    255    529    181
A:    -96   3408 -16912    M:    261    548    194
A:      0   3456 -16960    M:    261    548    194
A:    -48   3376 -16944    M:    255    530    182
A:    -64   3456 -16944    M:    260    546    193
A:     64   3376 -17056    M:    259    559    180
A:    -64   3392 -17008    M:    264    545    192
A:    -16   3376 -16992    M:    264    545    192
A:    -48   3328 -16992    M:    256    534    182
A:    -64   3344 -16976    M:    260    543    190
A:    -64   3408 -16912    M:    258    536    187
A:      0   3424 -16976    M:    262    541    189
A:   -112   3280 -16912    M:    262    541    189
A:    -16   3440 -16992    M:    258    539    185
A:    -80   3456 -16832    M:    261    540    190
A:     16   3264 -16928    M:    259    539    183
A:    -64   3504 -16992    M:    259    539    183
A:   -144   3344 -16928    M:    261    540    188
A:    -32   3472 -16928    M:    258    543    188
A:    -16   3376 -16992    M:    261    538    188
A:   -112   3296 -16976    M:    257    543    189
A:    -64   3424 -16896    M:    257    543    189
A:    -48   3264 -17040    M:    260    537    185
A:    -80   3472 -16992    M:    257    544    187
A:    -80   3312 -16928    M:    258    539    171
A:     16   3392 -17104    M:    256    542    188
A:    112   3360 -17072    M:    256    542    188
A:      0   3376 -16992    M:    257    538    185
A:    -16   3408 -16912    M:    259    544    190
A:    -32   3440 -16960    M:    257    536    186
A:     16   3280 -16960    M:    257    536    186
A:    -48   3392 -17008    M:    262    544    191
A:    -32   3264 -16896    M:    256    536    183
A:     16   3424 -17024    M:    260    546    192
A:    -32   3360 -17040    M:    259    531    186
A:    -80   3456 -17072    M:    259    531    186
A:   -112   3328 -16896    M:    259    545    191
A:    -16   3328 -16992    M:    255    536    182
A:    -48   3312 -17072    M:    261    545    193
A:     16   3360 -16912    M:    253    531    186
A:    -16   3328 -17072    M:    253    531    186
A:      0   3376 -16896    M:    261    549    190
A:    -32   3424 -16928    M:    259    532    183
A:    -48   3408 -17024    M:    263    549    195
A:    -64   3328 -16960    M:    255    530    182
A:    -80   3360 -16960    M:    255    530    182
A:    -32   3392 -16944    M:    260    547    194
A:    -80   3376 -16992    M:    256    531    182
A:    -80   3392 -16928    M:    262    553    192
A:    -32   3296 -16928    M:    262    553    192
A:    -32   3248 -16880    M:    255    531    180
A:     16   3376 -16992    M:    263    548    195
A:    -80   3360 -16960    M:    254    531    179
A:    -16   3360 -17040    M:    260    549    190
A:    -48   3376 -16896    M:    260    549    190
A:     32   3440 -16976    M:    256    533    182
A:    -48   3472 -16976    M:    261    546    194
A:      0   3296 -16992    M:    256    537    182
A:    -64   3456 -16912    M:    262    544    192
A:    -80   3408 -16960    M:    262    544    192
A:    -48   3360 -16832    M:    255    537    185
A:    -96   3424 -16960    M:    263    544    192
A:    -16   3408 -16976    M:    260    536    184
A:   -128   3360 -16864    M:    260    536    184
A:   -112   3360 -16880    M:    263    542    189
A:   -128   3328 -17104    M:    254    538    185
A:   -144   3392 -16944    M:    260    540    191
A:    -64   3360 -16928    M:    256    542    187
A:    -16   3424 -16992    M:    256    542    187
A:    -96   3344 -16912    M:    257    538    187
A:    -64   3408 -16928    M:    257    540    187
A:    -32   3440 -16912    M:    260    542    188
A:      0   3328 -16960    M:    260    540    185
A:    -80   3360 -17024    M:    260    540    185
A:     16   3408 -16976    M:    257    538    187
A:     80   3328 -16992    M:    256    542    188
A:    -32   3424 -16912    M:    259    537    187
A:   -128   3424 -16960    M:    259    537    187
A:    -16   3344 -16976    M:    256    543    186
A:    -96   3424 -16944    M:    261    535    185
A:    -32   3344 -16896    M:    257    542    188
A:     32   3408 -16976    M:    260    536    183
A:    -16   3280 -17024    M:    260    536    183
A:     16   3424 -16960    M:    258    542    191
A:    -16   3344 -17040    M:    256    537    185
A:     48   3424 -17024    M:    261    544    190
A:    -48   3408 -16864    M:    262    564    185
A:    -16   3392 -16976    M:    262    564    185
A:   -112   3280 -16912    M:    261    545    191
A:    -96   3296 -16912    M:    258    538    183
A:    -64   3408 -16896    M:    262    543    192
A:     16   3376 -16992    M:    258    534    186

Thanks :slight_smile:
Ali


#7

Your data does not look abnormal to me.

-Jon


#8

Ok. Thanks for your help Jon.
God bless you :slight_smile: