cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with LSM303DLM compass

avcs_silwal
Associate
Posted on September 13, 2012 at 07:51

I am trying to calculate the heading of the 3 axis compass and stuck with this issue that I can't solve. when I read the compass output register viz.

0x03 (x_msb),  0x04 (x_lsb),  

0x05 (z_msb),  0x06 (z_lsb),  

0x07 (y_msb) and  0x08 (y_lsb).  

the  x,y and z MSB values are not updated at all where as y and z LSB are updated as expected. I configured the 

CRA_REG_M (00h) to  0x10 for 30Hz ODR,  

MR_REG_M (02h) to 0x00 for continuous mode and 

CRB_REG_M (01h) to every possible value of (GN2,GN1,GN0) for all sensor range

but had same result. I can't understand what the problem is, The I2C software seems to be fine as it works perfectly for accelerometer and gyro!!!!.

did anyone come across this issue????

sample output:

Mxh=-16 Mxl=0 Myh=-1 Myl=35 Mzh=-1 Mzl=20

Mxh=-16 Mxl=0 Myh=-1 Myl=35 Mzh=-1 Mzl=20

Mxh=-16 Mxl=0 Myh=-1 Myl=35 Mzh=-1 Mzl=20

Mxh=-16 Mxl=0 Myh=-1 Myl=35 Mzh=-1 Mzl=20

Mxh=-16 Mxl=0 Myh=-1 Myl=35 Mzh=-1 Mzl=20

Mxh=-16 Mxl=0 Myh=-1 Myl=35 Mzh=-1 Mzl=20

Mxh=-16 Mxl=0 Myh=-1 Myl=35 Mzh=-1 Mzl=23

Mxh=-16 Mxl=0 Myh=-1 Myl=35 Mzh=-1 Mzl=23

please note the Mxh, Myh and Mzh won't buzz however I rotate or move the compass.

#lsm303dlm
3 REPLIES 3
avcs_silwal
Associate
Posted on September 13, 2012 at 11:27

I have finally solved the problem. but it took me some agonizing hours to figure out the problem. The problem is that the default sensitivity of the compass or the magnetometer is easily overran by surrounding magnetic fields, (may be from power supply or other devices). This causes the output registers to overflow and contain -4096 as output. THIS WAS NOT MENTIONED IN THE LSM303DLM datasheet!!!.

However the solution is simple, just select the least sensitivity. I selected the value 0xE0 for 0x02 register and it worked 🙂

dannym
Associate II
Posted on September 17, 2012 at 05:40

Ah you found the same thing I did:

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32F%20MEMS%20%20iNEMO/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32F MEMS iNEMO/LSM303DLHC max mag range is wrong&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580003E26E7DD54228C428E8F9FB5EE9C5185&currentviews=47]LSM303DLHC max mag range is wrong

I share your aggravation with this!

There's actually 4 things wrong here I was seeing in this part:

One, yes, the ''out-of-range'' value is not specified.

Two, the ''out-of-range'' value weirdly did this thing where it flipped sign near the out-of-range value, but only with the Z-axis.

Three, the point at which it goes out-of-range is not only unspecified, but makes little sense- it was not at the max/min of the number space, but a far lower value- I've never even seen this before in any part I can think of.

Four- this is big, I suspect you have the same problem but haven't realized it yet- the total range is not only much less than specified in the datasheet, but it's uselessly low for Earth-based applications as it will saturate even within the Earth's magnetic field, when the part's axis starts to line up with the Earth's magnetic vector.

The measurement's units all made sense, sqrt(x^2+y^2+z^2)~=0.480gauss, ambient for my area.  But with the scale set to 1.3gauss, the max values I could read were:

Xrange=+/-0.736 gauss

Yrange=+/-0.439gauss

Zrange=+/-2.080 gauss(!)

The Y will break when aligned with Earth's magnetic field.  This is a WICKED problem, because your code will work just fine and give good output until you hit a small range of orientations where it aligns enough that it suddenly jumps to -4096!

I recommend you set this up to read live on your screen and bring a magnet near it, slowly, to see what YOUR part can read as a max and min axis value before you get the ''out-of-range'' error.  From what I could tell, the magnetic field amplitude was indeed correct, but the limits of the axes are all wrong.  If you've got the same problem I saw on my part- that an axis can't read even the Earth's magnetic field- then you can't use that scale.

In fact, PLEASE do this test for me!  I'd suspected I merely got a bad part, but the product itself may have a major design flaw.  Your problem sounds the same as mine.

christophschiefer9
Associate
Posted on October 11, 2012 at 16:37

Yeah, I have the same problem with magnetic measurement as you!

The interesting thing: the effect is device dependent. I work with a couple of the LSM303DLM and some of them have this saturation problem, jumping to -4096. I'm not happy with and try now to change the measurement range to +/- 1.9 gauss at those sensors with problems.

I hope there will be a hint for fixing it or improvement of the sensor for the future. 😉