Strange values from LSM303 Magnetometer I am having problems with values i get from Pololu MinIMU-9 boards's magnetometer. I am using Arduino-micro and the arduino library from Pololu. The sensor on the board is 3 axis accelero- and magnetometer LSM303.
The problem is - the highest value always seems to be in vertical axis, not horisontal, as i would expect.

For example, here is the data i got when i put sensor flat on the table and made 360 clockwise turn in horizontal plane:
(values are X Y Z)

-6 102 -516
-11 103 -515
-11 103 -515
-21 102 -514
-31 109 -515
-40 114 -512
-57 116 -509
-57 116 -509
-77 111 -507
-93 109 -506
-104 106 -507
-104 106 -507
-114 99 -508
-126 88 -504
-131 74 -507
-142 52 -507
-142 52 -507
-151 31 -506
-150 4 -509
-147 -27 -513
-147 -27 -513
-133 -60 -513
-119 -80 -519
-112 -87 -517
-102 -97 -521
-102 -97 -521
-89 -101 -522
-77 -101 -524
-50 -104 -527
-50 -104 -527
-43 -105 -529
-14 -98 -532
2 -92 -535
23 -92 -535
23 -92 -535
36 -81 -534
66 -54 -534
76 -32 -531
76 -32 -531
76 -21 -532
78 -7 -531
85 10 -528
91 39 -524
91 39 -524
94 50 -524
91 63 -524
76 75 -525
76 75 -525
63 82 -526
47 93 -525
44 98 -522
44 98 -522
38 104 -521
26 110 -521
26 112 -518
21 116 -519
21 116 -519
18 116 -518
12 120 -517
11 120 -515
11 120 -515
8 120 -517
5 122 -515
-5 123 -514


Turning the sensor "upside down" and then rotating it in horisontal plane again gives similar results, only Z axis has positive 500+ value.

Shouldn't the magnetometer always point in the direction of the magnetic field - in X or Y direction?
I have tried this in different rooms and buildings and got the same results.
Rotating the sensor on other axes yields the same results - vertical axis has the largest (and constant) value.

For reference, here is the code i used to get the values.

#include &lt;Wire.h&gt;
#include &lt;LSM303.h&gt;
LSM303 compass;

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  compass.init();
  compass.enableDefault();
}

void loop(){
  compass.readMag();
  Serial.print(compass.m.x);
  Serial.print(" ");
  Serial.print(compass.m.y);
  Serial.print(" ");
  Serial.print(compass.m.z);
  Serial.print("\n");
  delay(100);
}
The earth's magnetic field is not parallel with the ground. In some locations it can be pointing more down than across. 

NOAA has a magnetic field calculator where you can enter you latitude and longitude. For example, I am located at approx  19deg S 147degE and the field is 

North Comp    East Comp    Vertical Comp    Total magnitude
32,434.4 nT   4,280.6 nT   -36,035.1 nT     48,670.7 nT 


So the downwards strength is more than the other two combined for my location. I'm guessing the calculator will also show a very strong down component for your location.

Some other things to be aware of are that the LSM303DLHC magnetometer can have quite large offsets, up to the equivalent of 2 earth magnetic fields, and therefore requires calibration. Also hard and soft iron distortions can affect the readings.

Edit: Please see my answer here for methods to calibrate the sensor.