cancel
Showing results for 
Search instead for 
Did you mean: 

LSM303AGR reading weird values

Theresa Lichtenberger
Associate II
Posted on March 09, 2018 at 00:18

Hey! I am using the accelerometer LSM303AGR to read some acceleration values on external interrupt. If the threshold is overcome, and interrupt will be triggered and in the ISR, I am reading the FIFO of the accelerometer (I am using STM32F405 btw). The values seem to be correct half of the time as shown below. As you can see in the third interrupt and fifo reading, the zAxis exceeded the threshold and then, out of nowhere, the axis-values are getting weird (and wrong!). They won't return back to normal and therefore, no more interrupts are triggered. If I stop, reset and run it again, no interrupts can be triggered because the values are still weird and wrong. The only thing that helps is to cut the power supply for a second and plug it in again. But even that does not help sometimes. 

readINT1_SRC: 86

FIFO samples: xAxis exceeds threshold

x: 3189.199951 y: -375.200012 z: 750.400024

x: 3564.399902 y: -187.600006 z: 1125.599976

x: 3189.199951 y: -187.600006 z: 1125.599976

x: 2251.199951 y: 0.000000 z: 1125.599976

x: 0.000000 y: -187.600006 z: 1125.599976

x: -2063.600098 y: 375.200012 z: 750.400024

x: -2814.000000 y: 562.799988 z: 750.400024

x: -3752.000000 y: 375.200012 z: 938.000000

x: -2626.399902 y: 562.799988 z: 938.000000

x: -1313.199951 y: 562.799988 z: 938.000000

x: -562.799988 y: 0.000000 z: 938.000000

x: -562.799988 y: 187.600006 z: 938.000000

x: -562.799988 y: 0.000000 z: 938.000000

x: -187.600006 y: 0.000000 z: 938.000000

x: 187.600006 y: 0.000000 z: 750.400024

x: 187.600006 y: 0.000000 z: 750.400024

x: 187.600006 y: 0.000000 z: 750.400024

x: 0.000000 y: 0.000000 z: 938.000000

x: 0.000000 y: 0.000000 z: 938.000000

x: 0.000000 y: -187.600006 z: 938.000000

x: 0.000000 y: 0.000000 z: 938.000000

x: 0.000000 y: 0.000000 z: 938.000000

x: 0.000000 y: 0.000000 z: 938.000000

x: 187.600006 y: 0.000000 z: 938.000000

x: 187.600006 y: 0.000000 z: 938.000000

x: 0.000000 y: 0.000000 z: 938.000000

x: 0.000000 y: 0.000000 z: 938.000000

x: 187.600006 y: 0.000000 z: 938.000000

x: 0.000000 y: 0.000000 z: 938.000000

x: 0.000000 y: 0.000000 z: 938.000000

x: 0.000000 y: 0.000000 z: 938.000000

x: 0.000000 y: 0.000000 z: 938.000000

readINT1_SRC: 101

FIFO samples: zAxis exceeds threshold

x: 1125.599976 y: 562.799988 z: 3564.399902

x: 187.600006 y: 938.000000 z: 4690.000000

x: -750.400024 y: 750.400024 z: 5252.799805

x: -1688.400024 y: 375.200012 z: 0.000000

x: 469.000000 y: 0.000000 z: 562.799988

x: -187.600006 y: 0.000000 z: 140.699997

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

x: 0.000000 y: 140.699997 z: -187.600006

readINT1_SRC: 86

FIFO samples: xAxis exceeds threshold

x: -3189.199951 y: 562.799988 z: 938.000000

x: 0.000000 y: -938.000000 z: 0.000000

x: -2720.199951 y: 375.200012 z: 1125.599976

x: 0.000000 y: -938.000000 z: 0.000000

x: 0.000000 y: -938.000000 z: 0.000000

x: -375.200012 y: -4127.200195 z: 2438.800049

x: -375.200012 y: -4314.799805 z: 2532.600098

x: -375.200012 y: -4314.799805 z: 2532.600098

x: -375.200012 y: -4221.000000 z: 2532.600098

x: -375.200012 y: -4221.000000 z: 2532.600098

x: -375.200012 y: -4127.200195 z: 2438.800049

x: -375.200012 y: -4314.799805 z: 2532.600098

x: -375.200012 y: -4314.799805 z: 2532.600098

x: -375.200012 y: -4314.799805 z: 2532.600098

x: -375.200012 y: -4127.200195 z: 2532.600098

x: -375.200012 y: -4221.000000 z: 2532.600098

x: -375.200012 y: -4314.799805 z: 2438.800049

x: -375.200012 y: -4314.799805 z: 2532.600098

x: -469.000000 y: -4127.200195 z: 2532.600098

x: -375.200012 y: -4314.799805 z: 2438.800049

x: -375.200012 y: -4221.000000 z: 2532.600098

x: -281.399994 y: -4221.000000 z: 2532.600098

x: -375.200012 y: -4221.000000 z: 2532.600098

x: -375.200012 y: -4221.000000 z: 2532.600098

x: -375.200012 y: -4221.000000 z: 2532.600098

x: -469.000000 y: -4221.000000 z: 2532.600098

x: -469.000000 y: -4221.000000 z: 2438.800049

x: -375.200012 y: -4221.000000 z: 2532.600098

x: -375.200012 y: -4221.000000 z: 2438.800049

x: -375.200012 y: -4127.200195 z: 2438.800049

x: -281.399994 y: -4314.799805 z: 2438.800049

x: -375.200012 y: -4221.000000 z: 2532.600098

I have attached my code to this post. So you can see the code I have written. Maybe this will help for finding a solution.

I appreciate every advice and help you can give me, thank you in advance!

#accelerometer #external-interrupt #motion-sensor #lsm303agr #sensor-tile
2 REPLIES 2
Miroslav BATEK
ST Employee
Posted on March 12, 2018 at 17:07

I don't know what is the root cause of this issue, but I see a few problems in your code.

I might help to fix it.

  • SPI1_writeReg(0x2E, 0x0A); this is not Stream-to-FIFO mode but the the FIFO is in bypass mode, the FIFO threshold is set to 10 sample, but it is not used as the FIFO is bypassed
  • In the interrupt routine you are checking data ready, why? You should check number of sample in the FIFO (in case FIFO is used). You should also limit the number of reading from FIFO to number of samples really stored in FIFO, not to real whole FIFO as you are doing now.
  • It is not good practice to read data and send them through UART in interrupt routine, you should set a flag and make it in the main loop 
Posted on March 19, 2018 at 15:57

Batek.Miroslav

‌ Hey Miroslav, thank you for your answer. I have also noticed the mistake of writing 0x0A to the 0x2E register, but if I write 0xC0 (which would correctly be the Stream-to-FIFO mode), the values of the accelerometer are also more than wrong and seem to be stuck. Two your second point: Why do I have to check for the actual number of samples stored in the FIFO? I thought if a interrupt takes place, the mode will switch to FIFO-mode and the FIFO will be filled until its full. So in the end, I will always have 32 valuesstored in the FIFO?

Thank you in advance for your help!