Showing results for 
Search instead for 
Did you mean: 

LSM303AGR magnetometer will not pass self test

Associate II


I have been battling with getting an LSM303AGR to pass the self test. The accelerometer is passing the self test and the values look reasonable (Z-axis ~1g), but the magnetometer will not pass. I have 2 STEVAL-MKI172V1 boards, both are showing the same symptoms. I am testing with a Nucleo-U5A5ZJ-Q with I2C at fast mode (400KHz).

Initially, I wrote my own self test based on the procedures in the datasheet (Version 11.0, Aug 2022). I was able to get the accelerometer to pass, but not the magnetometer. After debugging ad infinitum, I decided to try using the test provided from the STMems_Standard_C_driver repo contained in lsm303agr_self_test.c. I found a few posts here and this was what everyone was directed to.

It is worth noting there are several bugs in this test: using uninitialized data, using previous data from the accelerometer test for the mag test without clearing, etc. Once I had these bugs sorted, I reran the tests to get the exact same outcome I was getting from my own self test. Here is an example that is typical of what I have seen, this is after conversion and averaging:
Name : maes_st_off_mag
Details:{-0.50999999, -1.83000004, 0}  // {x,y,z}

Name : maes_st_on_mag
Details:{0.839999974, -0.899999976, 1.20000005}  // {x,y,z}


What I am seeing is a very small difference between measurements with/out the self test enabled. I have checked all I2C comms with a Saleae logic analyzer and can rule out any comms issues. I've checked for data corruption, timing issues, and all the other commonplace issues and can confidently rule them out. There are no data overruns and no apparent saturation, and no magnets in the vicinity.

I have attached the exported data from the logic analyzer for both tests as well as the modified self test file. The first reads have overruns as they are after the delay period: this is expected and the values are discarded. The rest of the values all work out to be pretty close to 0 for both with and without self test enabled.

As an aside, the sensors were ordered separately and have been handled with the appropriate ESD precautions. I'm at least a little bit disappointed these dev kit sensors do not come in an ESD bag, just bare in a carboard box. I'd be happy if this is just a few bad sensors, but after the same behavior from 2 different sensors ordered a few months apart, it's hard to say one way or another.

Associate II

@Federica Bossi , are you able to help with this?

Associate II

I've ordered another 2 STEVAL-MKI172V1 boards and I just tried switching everything over to SPI and I have gotten a pass. Out of 4 STEVAL-MKI172V1 boards (all brand new), 2 are passing and 2 are failing the magnetometer self test. I am beginning to think this could just be bad QA or poor storage conditions as these boards are stored in bare cardboard with no ESD or humidity protection.

Federica Bossi
ST Employee

Hi @ocean_rse .

Where did you buy them?

Did you try to use the code from the Github repository making only the necessary changes to adjust it to you testing set up? What results did you get in this case?

Additionally, did you try replicating the test in a completely different environment? Maybe there is an interference that you are not aware of.

And lastly, did you try using the sensors to measure a known magnetic field? In this way you could assess if the problem is with the self-test procedure or with the sensors themselves.

In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

Hi @Federica Bossi,

The sensors were bought from Mouser and Digikey. Never had any issues with them as suppliers in the past. Unfortunately, I did not keep track of which sensors were from which supplier, so I don't know if both the failed units came from the same supplier. The testing code was exactly as you described: the testing code from the ST Github repo with only necessary changes for specific hardware and ensuring a clean slate for measurement data.

As for interference, that is certainly a valid approach. I made sure to go through my workspace and move anything magnetic far away during testing, but it is easy to miss things. I have access to magnetic test equipment at work and plan to check it out. That said, I ran some tests just setting up and polling the sensors for magnetic axes data and I noticed that on the sensors that did not pass, they also did not read any disruption from ferrous material and weak magnets passing by, so I am more convinced the problem is with the magnetometer failing completely as opposed to the self test not exciting the sensor. These failed sensors just keep reading at or a few counts away from zero.