cancel
Showing results for 
Search instead for 
Did you mean: 

Reading data from LSM303AGR

Ester Verlato
Associate
Posted on June 22, 2017 at 15:50

Hi, I'm new here and I am new at working on mems.

I have a LPCXpresso1549 evaluation board and a Steval-MKI172v1 mems that implements a lsm303agr chip. 

I'm working on mbed platform and I need to read values from accelerometer but I tried a lot and cannot read anything.

I connect pins for SCL and SDA, 3V3 to the VDD pin, the GND, and two digital pins to the CS and CS_A (chip select on lsm303agr). 

I don't know what address my lsm303 has and I can't understand what address I need to write on registers

:(

My code is:

&sharpinclude 'mbed.h'

&sharpdefine ACC 0x00

&sharpdefine CTRL_REG1_A 0x20

&sharpdefine CTRL_REG2_A 0x21

&sharpdefine CTRL_REG3_A 0x22

&sharpdefine CTRL_REG4_A 0x23

&sharpdefine CTRL_REG5_A 0x24

&sharpdefine OUT_X_L_A 0x28

&sharpdefine OUT_X_H_A 0x29

&sharpdefine OUT_Y_L_A 0x2A

&sharpdefine OUT_Y_H_A 0x2B

&sharpdefine OUT_Z_L_A 0x2C

&sharpdefine OUT_Z_H_A 0x2D

typedef struct Plane {

      float x, y, z;

} Plane;

Plane a;

//Plane a_max;

//Plane a_min;

Serial pc(USBTX, USBRX); // tx, rx

I2C i2c(P0_23, P0_22);

DigitalOut chip_sel_a(P0_27);

DigitalOut chip_sel_m(P0_28);

void LSM303_write (int data, int reg_address) {

   char out[2] = { reg_address, data };

   i2c.write( ACC, out, 2 );

}

int main() {

//setup iniziale

chip_sel_a = 1;

chip_sel_m = 0;

LSM303_write(0x57, CTRL_REG1_A);

LSM303_write(0x81, CTRL_REG4_A); 

LSM303_write(0x00, CTRL_REG2_A);

LSM303_write(0x00, CTRL_REG3_A);

LSM303_write(0x00, CTRL_REG5_A);

   while(1) {

      char data[1] = { OUT_X_L_A | (1<<7)}; //mette un 1 in testa all'indirizzo per fare read multipla

      char out[6] = {0,0,0,0,0,0};

      i2c.write( ACC, data,1);

      i2c.read( ACC, out, 6);

      a.x = short( (((short)out[1]) << 8) | out[0] );

      a.y = short( (((short)out[3]) << 8) | out[2] );

      a.z = short( (((short)out[5]) << 8) | out[4] );

      pc.printf('asse x: %f\n', a.x);

      pc.printf('asse y: %f\n', a.y);

      pc.printf('asse z: %f\n\n', a.z);

      wait(2);

      }

   }   

Could someone help me?  

:(

#sensor #steval-mki172v1 #lpc1549 #lpcxpresso #mems #lsm303agr
2 REPLIES 2
S.Ma
Principal
Posted on June 22, 2017 at 19:19

If there is a VDDIO this needs to be the voltage source of the serial interface. 

Read carefully the datasheet and if using the I2C, make sure you can read the WHO_AM_I_REG @0x0F properly for each function (Accelero and Magneto).

Check with an oscilloscope otherwise to make SURE the I2C is looking good and there is a pull-up on each line.

Or sweep all the I2C slave addresses until an ACK is detected...

Miroslav BATEK
ST Employee
Posted on September 04, 2017 at 14:30

If you are using I2C bus, you have to connect pull-up resistors on the SDA nad SCL lines. CS pins needs to be connected to VDDIO. VDDIO also connected to power supply.

The I2C addresses of the sensors are following.

0690X0000060879QAA.png