cancel
Showing results for 
Search instead for 
Did you mean: 

LSM303C gives zero acceleration for z-axis when at rest

NVell
Associate II

Hi,

I am trying to read Z-axis acceleration from LSM303C at rest. The device returns 0x41 for who am I register (same as datasheet). But it gives zero values for z-axis acceleration at rest. Attached is my code. Please let me know if there is an issue with the code and why it is not giving 1g for z-axis at rest. Thanks.

U8 temp_char, imuaxl, imuaxh, imuayl, imuayh, imuazl, imuazh;

U8 array_whoami[1] = {0x0F};

U8 array_control1[1] = {0x20};

U8 array_control1_value[1] = {0x9F};

U8 array_control4[1] = {0x23};

U8 array_control4_value[1] = {0x30};

SMB_DATA_OUT = array_whoami;

TARGET = 0x3A;  // Target the Slave for next

SMB_Write();

SMB_Read();

SMB_DATA_OUT = array_control1_value;

TARGET = 0x3A;  // Target the Slave for next

Byte_Write(0x20,0x9F);

Byte_Read(0x20);

SMB_DATA_OUT = array_control4_value;

TARGET = 0x3A;  // Target the Slave for next

Byte_Write(0x23,0x30);

Byte_Read(0x23);

   Byte_Read(0x2C);

imuazl = SMB0DAT;

Byte_Read(0x2D);

imuazh = SMB0DAT;

accZdata = (imuazh << 😎 | imuazl;

accZdata_result = accZdata >> 4;

  negative = (accZdata_result & (1 << 15)) != 0;

  if (negative)

   nativeInt = accZdata_result | ~((1 << 16) - 1);

  else

   nativeInt = accZdata_result;

2 REPLIES 2
NVell
Associate II

Can you also give me a code to convert the register values to 'g' value? I'm currently using a code I found on the internet but not sure if it is right.

Thanks.

Eleon BORLINI
ST Employee

Hi, but when you read the z axis data you get a value near 0 or exactly 0? The sensor is correctly oriented (I mean, you aren't able to detect 1g on no one axis), right?

About the code, did you check in the github repository for ST sensors? There isn't the , which is conceptually the same (https://github.com/STMicroelectronics/STMems_Standard_C_drivers/tree/master/lsm303agr_STdC/driver). Here is the code for the data conversion:

float_t lsm303c_from_fs_2g_hr_to_mg(int16_t lsb)
{
  return ((float_t)lsb / 16.0f ) * 0.061f;
}
 
float_t lsm303c_from_fs_4g_hr_to_mg(int16_t lsb)
{
  return ((float_t)lsb / 16.0f ) * 0.122f;
}
 
float_t lsm303c_from_fs_8g_hr_to_mg(int16_t lsb)
{
  return ((float_t)lsb / 16.0f ) * 0.244f;

regards