# lsm303

Hi!

I have LSM303 devices but some strange data i got from it when it pointing to the north and south.

I use LSM303 Arduino library but with some modification according i2c reading because I use this sensor on the Atmel dev board.
Ok, when I use Arduino library the call to LSM303::heading(void) (it passes 0, -1, 0 vector to LSM::heading(vector from) method) returns 110 deg when chip X axis points to the north.
When I point chips X axis to the west, it show 18 deg. I’ve changed from vector of the heading method to (1, 0, 0) and now it is returns 18 deg when chip is pointing straight to the North by its X axis. When I turns chip to the 90 deg for East or West, it show correct 90 and 270 degs accordingly. The same trouble with South diretion, returns 158 deg instean of 180.

I decided to use different equations that I googled and from app-note (AN3192):

``````  float pitch = atan2(a.x, a.z); // around Y axis in radians
float roll = atan2(a.y, a.z); // around X axis in radians``````

The pitch and roll shows right degrees when i try to tilt the board. It is ok.

Here is my heading equation from app-note with tilt compensations:

``  float h1 = atan2( (mx * sin(roll) * sin(pitch) + my * cos(roll) - mz * sin(roll) * cos(pitch)), (mx * cos(pitch) + mz * sin(pitch)) ); // in radians``

When I point chip by its X axis to the North, now it returns 23-26 degs. When i point it to the West or East it returns (around) +/- 90 degs. When I point it to the South. it returnts -156!!!
Then, I tried to use the simple equation without tilt compensation:

``  float h2 = atan2(my, mx); // in radians``

And it returns the same results as the equation with tilt compensation.

Can any body help me to understand the problem. I have no idea now…

Have you calibrated the magnetometer? Are there magnetic field sources (other than the earth) in the vicinity? There are a number of posts on this topic, so search this forum and elsewhere for lots of information.