cancel
Showing results for 
Search instead for 
Did you mean: 

I am using LSM303D to get accelerometer and magnetometer values but ay and az axis of accelerometer values are wrong.

ATarh.2
Associate

I am using LSM303D to get accelerometer and magnetometer values but i keep getting ay and az axis of accelerometer values wrong. I am able to communicate with the chip because I can see the magnetometer values but something is wrong with accelerometer.

I added the picture of output values. The is as follows:

#include <Wire.h>

//Register addresses for magnetometer

#define OUT_X_L_M  0x08   

#define OUT_X_H_M  0x09   

#define OUT_Y_L_M  0x0A  

#define OUT_Y_H_M  0x0B

#define OUT_Z_L_M  0x0C

#define OUT_Z_H_M  0x0D

// Register addresses for accelerometer

#define OUT_X_L_A  0x28   

#define OUT_X_H_A  0x29   

#define OUT_Y_L_A  0x2A

#define OUT_Y_H_A  0x2B

#define OUT_Z_L_A  0x2C

#define OUT_Z_H_A  0x2D

//Control Settings for LSM303D

#define CTRL0    0x1F

#define CTRL1    0x20

#define CTRL2    0x21

#define CTRL3    0x22

#define CTRL4    0x23

#define CTRL5    0x24

#define CTRL6    0x25

#define CTRL7    0x26

//Register address for gyroscope

#define OUT_X_L   0X28

#define OUT_X_H   0X29

#define OUT_Y_L   0X2A

#define OUT_Y_H   0X2B

#define OUT_Z_L   0X2C

#define OUT_Z_H   0X2D

//Control Settings for L3GD20

#define CTRL_REG1  0X20

#define MPU_HIGH_ADDR  0b0011101  // Device address of LSM303D with SDO/ADR/SA0 pulled HIGH.

#define MPU_LOW_ADDR   0b0011110  // Device address of LSM303D with SDO/ADR/SA0 connected to GND.

#define GYRO_HIGH_ADDR  0b1101011  // Device address of L3GD20 with SDO/ADR/SA0 pulled HIGH.

#define GYRO_LOW_ADDR  0b1101010  // Device address of L3GD20 with SDO/ADR/SA0 connected to GND.

#define BYTE  8

double mag_sen = 0.0000160;   //magnetic sensitivity for +/- 4 gauss in gauss/LSB units

double acc_sen = 0.0000610;   //linear acc sensitivity for +/- 2G in G/LSB units

double gyro_sen = 0.008750;

int16_t mx,my,mz;

int16_t ax,ay,az;

int16_t gx,gy,gz;

void setup(){

  Serial.begin(9600);

  Serial.println("Serial Begin");

  Wire.begin();

  enableDefault();

  delay(250);

}

void loop(){delay(1000);

  

  mx = (int16_t)readRegister(OUT_X_H_M)<<BYTE | readRegister(OUT_X_L_M);

  my = (int16_t)readRegister(OUT_Y_H_M)<<BYTE | readRegister(OUT_Y_L_M);

  mz = (int16_t)readRegister(OUT_Z_H_M)<<BYTE | readRegister(OUT_Z_L_M);

  Serial.print("Mag: ");

  Serial.print(mx*mag_sen,DEC);Serial.print("\t");

  Serial.print(my*mag_sen,DEC);Serial.print("\t");

  Serial.print(mz*mag_sen,DEC);Serial.print("\t");

  ax = (int16_t)readRegister(OUT_X_H_A)<<BYTE | readRegister(OUT_X_L_A);

  ay = (int16_t)readRegister(OUT_Y_H_A)<<BYTE | readRegister(OUT_Y_L_A);

  az = (int16_t)readRegister(OUT_Z_H_A)<<BYTE | readRegister(OUT_Z_L_A);

  Serial.print("Acc: ");

  Serial.print(ax*acc_sen,DEC);Serial.print("\t");

  Serial.print(ay*acc_sen,DEC);Serial.print("\t");

  Serial.print(az*acc_sen,DEC);Serial.print("\t");

  gx = (int16_t)readReg(OUT_X_H)<<BYTE | readReg(OUT_X_L);

  gy = (int16_t)readReg(OUT_Y_H)<<BYTE | readReg(OUT_Y_L);

  gz = (int16_t)readReg(OUT_Z_H)<<BYTE | readReg(OUT_Z_L); 

  Serial.print("Gyro: ");

  Serial.print(gx*gyro_sen,DEC);Serial.print("\t");

  Serial.print(gy*gyro_sen,DEC);Serial.print("\t");

  Serial.print(gz*gyro_sen,DEC);Serial.println();

}

void enableDefault(){

  writeRegister(CTRL0,0x00);

  //Accelerometer settings

  //0x37 = 0b00110111

  //AODR = 0011 (12.5 Hz)

  writeRegister(CTRL1,0x37);

  //Full-scale selection = +/- 2g

  writeRegister(CTRL2,0x00);

  //Magnetometer Settings

  // 0x68  = 0b01101000

  //M_RES = 11 (high resolution), M_ODR = 010 (12.5 Hz)

  writeRegister(CTRL5,0x68);

  //MD = 00 (the device does conversions repeatedly)

  writeRegister(CTRL7,0x00);

  //Gyroscope Settings

  //0x2F = 0b00101111 

  writeReg(CTRL_REG1,0x2F);

}

int8_t readRegister(int8_t reg){

  int8_t buffer;

  Wire.beginTransmission(MPU_HIGH_ADDR);

  Wire.write(reg);

  Wire.endTransmission(0);  //sends restart message so the bus will not be released

  Wire.requestFrom(MPU_HIGH_ADDR,1);

  while(Wire.available() == 0);

  buffer = Wire.read();

  Wire.endTransmission(1);

  return buffer;

}

void writeRegister(int8_t reg, int8_t val ){

  Wire.beginTransmission(MPU_HIGH_ADDR);

  Wire.write(reg);

  Wire.write(val);

  Wire.endTransmission();

}

void writeReg(int8_t reg, int8_t val){

  Wire.beginTransmission(GYRO_HIGH_ADDR);

  Wire.write(reg);

  Wire.write(val);

  Wire.endTransmission();

}

int8_t readReg(int8_t reg){

  int8_t buffer;

  Wire.beginTransmission(GYRO_HIGH_ADDR);

  Wire.write(reg);

  Wire.endTransmission(0);

  Wire.requestFrom(GYRO_HIGH_ADDR,1);

  while(Wire.available() == 0);

  buffer = Wire.read();

  Wire.endTransmission(1);

  return buffer;

}

1 REPLY 1
LauraCx
ST Employee

*Modified by moderation* - Kind reminder to keep the title of your question within 70 characters. Thank you