Search code examples
stm32accelerometersensors

Why am I getting the wrong sensitivity output for this accelerometer?


I'm using the LSM303AGR Accelerometer (Datasheet) with STM32 I2C Hal Library but I can't figure out the sensitivity.

Here is my code to set configuration registers:

void LSM303AGR_Init() {

uint8_t Data[2] = {0};
Data[0]= 0x20;
Data[1]= 0x57;                                                                                                                                                                      //ODR @100Hz                                                                                                //Accelerometer Control Register 1 and Data
HAL_I2C_Master_Transmit(&hi2c1,0x19<<1,Data,2,50);
Data[0]= 0x23;
Data[1]= 0x20;                                                                                                                                                                      //+/-8G and in normal mode
HAL_I2C_Master_Transmit(&hi2c1,0x19<<1,Data,2,50);}
                                                                                            

The first I2C write transfer is to register 0x20 and is supposed to set sensor to normal mode and output data rate of 100Hz and the second I2C write transfer is supposed to set scale to +/-8G.

Also, here is my code to read XYZ 16bit values and convert to mg (15.63 is the sensitivity as per data sheet):

void LSM303AGR_AccReadXYZ(float* pData) {

HAL_I2C_Master_Transmit(&hi2c1,(0x19<<1)|0x01,&accXYZregAutoRead ,1,50);
HAL_I2C_Master_Receive(&hi2c1,(0x19<<1)|0x01, buffer,6,50); 
for(int i=0; i<3; i++) {
pData[i]=(float)((int16_t)((uint16_t)buffer[2*i+1] << 8) | buffer[2*i]) / 15.63;} //Readings in mg                               
}

I know for a fact that I am writing to these registers and reading from the correct registers through my debugging. However, with the set up above I get output values of about 250mg on a tabletop (for the z-axis of course the others are about zero) using a sensitivity of 15.63 but when I change 15.63 to 3.9 sensitivity (datasheet pg 13) I get about 1000mg on z axis which is correct! Problem is, my registers are set to +/-8G (datasheet pg 49) and normal power mode (datasheet pg 47) and according to datasheet the sensitivity should be should be 15.63 not 3.9!

Any help would be much appreciated!


Solution

  • You are using normal mode, so your data should be 10bit and you should shift right your reads by 6 (values are left justified).