Why is magnetometer stronger on one side than the other?

Can you convert your data into some consistent set of units? It is difficult to tell what would be normal in in units of 4*(gauss*6842+C)^2 While sitting on my table, my phone's magnetometer is reading 865uT (8.65Gauss) +/- 1uT.

I used the serial.ino example that came with the library. My understanding is that the value M prints raw data value that needs to be converted to gauss. The conversion is in the notes in the example. The only change I made to the example sketch is adding in the conversions and prints to gauss.

Here is the sketch:

/*
The sensor outputs provided by the library are the raw 16-bit values
obtained by concatenating the 8-bit high and low magnetometer data registers.
They can be converted to units of gauss using the
conversion factors specified in the datasheet for your particular
device and full scale setting (gain).

Example: An LIS3MDL gives a magnetometer X axis reading of 1292 with its
default full scale setting of +/- 4 gauss. The GN specification
in the LIS3MDL datasheet (page 8) states a conversion factor of 6842
LSB/gauss (where LSB means least significant bit) at this FS setting, so the raw
reading of 1292 corresponds to 1292 / 6842 = 0.1888 gauss.
*/

#include <Wire.h>
#include <LIS3MDL.h>

LIS3MDL mag;

float X_Gauss;
float Y_Gauss;
float Z_Gauss;

char report[80];

void setup()
{
  Serial.begin(9600);
  Wire.begin();

  if (!mag.init())
  {
    Serial.println("Failed to detect and initialize magnetometer!");
    while (1);
  }

  mag.enableDefault();
}

void loop()
{
  mag.read();

  snprintf(report, sizeof(report), "M: %6d %6d %6d",
    mag.m.x, mag.m.y, mag.m.z);

 //my code for gauss according to datasheet...
 X_Gauss = mag.m.x / 6842.0;
 Y_Gauss = mag.m.y / 6842.0;
 Z_Gauss = mag.m.z / 6842.0;

  Serial.print(report);            // "M" prints X Y and Z raw values, respectively
  Serial.print("  X Gauss= ");
  Serial.print(X_Gauss,3);
  Serial.print(" Y Gauss=  ");
  Serial.print(Y_Gauss,3);
  Serial.print(" Z Gauss=  ");
  Serial.println(Z_Gauss,3);

  delay(100);
}

Here is an example of the output:

M:   -972   2392  -2832  X Gauss= -0.142 Y Gauss=  0.350 Z Gauss=  -0.414
M:   -985   2408  -2800  X Gauss= -0.144 Y Gauss=  0.352 Z Gauss=  -0.409
M:   -963   2380  -2774  X Gauss= -0.141 Y Gauss=  0.348 Z Gauss=  -0.405
M:   -980   2375  -2730  X Gauss= -0.143 Y Gauss=  0.347 Z Gauss=  -0.399
M:  -1018   2404  -2726  X Gauss= -0.149 Y Gauss=  0.351 Z Gauss=  -0.398
M:   -947   2381  -2782  X Gauss= -0.138 Y Gauss=  0.348 Z Gauss=  -0.407
M:   -942   2428  -2757  X Gauss= -0.138 Y Gauss=  0.355 Z Gauss=  -0.403
M:   -965   2405  -2762  X Gauss= -0.141 Y Gauss=  0.352 Z Gauss=  -0.404
M:   -961   2382  -2648  X Gauss= -0.140 Y Gauss=  0.348 Z Gauss=  -0.387
M:   -943   2385  -2738  X Gauss= -0.138 Y Gauss=  0.349 Z Gauss=  -0.400
M:   -975   2402  -2765  X Gauss= -0.143 Y Gauss=  0.351 Z Gauss=  -0.404
M:   -999   2375  -2734  X Gauss= -0.146 Y Gauss=  0.347 Z Gauss=  -0.400
M:   -971   2392  -2683  X Gauss= -0.142 Y Gauss=  0.350 Z Gauss=  -0.392
M:   -956   2381  -2700  X Gauss= -0.140 Y Gauss=  0.348 Z Gauss=  -0.395
M:   -982   2395  -2761  X Gauss= -0.144 Y Gauss=  0.350 Z Gauss=  -0.404
M:   -983   2385  -2740  X Gauss= -0.144 Y Gauss=  0.349 Z Gauss=  -0.400
M:   -966   2382  -2729  X Gauss= -0.141 Y Gauss=  0.348 Z Gauss=  -0.399
M:   -948   2399  -2720  X Gauss= -0.139 Y Gauss=  0.351 Z Gauss=  -0.398
M:   -999   2380  -2725  X Gauss= -0.146 Y Gauss=  0.348 Z Gauss=  -0.398
M:   -986   2397  -2718  X Gauss= -0.144 Y Gauss=  0.350 Z Gauss=  -0.397
M:   -983   2384  -2758  X Gauss= -0.144 Y Gauss=  0.348 Z Gauss=  -0.403
M:   -964   2375  -2741  X Gauss= -0.141 Y Gauss=  0.347 Z Gauss=  -0.401
M:   -993   2376  -2698  X Gauss= -0.145 Y Gauss=  0.347 Z Gauss=  -0.394
M:   -980   2366  -2720  X Gauss= -0.143 Y Gauss=  0.346 Z Gauss=  -0.398
M:  -1002   2366  -2746  X Gauss= -0.146 Y Gauss=  0.346 Z Gauss=  -0.401
M:  -1021   2364  -2790  X Gauss= -0.149 Y Gauss=  0.346 Z Gauss=  -0.408
M:  -1022   2348  -2743  X Gauss= -0.149 Y Gauss=  0.343 Z Gauss=  -0.401
M:  -1052   2356  -2741  X Gauss= -0.154 Y Gauss=  0.344 Z Gauss=  -0.401
M:   -986   2394  -2686  X Gauss= -0.144 Y Gauss=  0.350 Z Gauss=  -0.393
M:  -1029   2364  -2750  X Gauss= -0.150 Y Gauss=  0.346 Z Gauss=  -0.402
M:  -1000   2356  -2805  X Gauss= -0.146 Y Gauss=  0.344 Z Gauss=  -0.410

I am new to this but that looks pretty stable to me. Especially compared to how it is jumping around in my other sketch.

What are your thoughts?

Maybe I need to start completely over with my project and use this example sketch as a baseline?? Maybe average the raw values together and set a trigger threshold from that?

Interestingly enough, I was playing with the numbers by moving my computer mouse close to it. The Z axis is FAR more sensitive, or maybe just more responsive, than the X and Y axis.

So using the example sketch in post 22 seems to be the way to go. I modified it and made math formulas for my particular needs. I still added in an initial calibration in the setup. Serial printing certainly reveals that different locations have different readings. I think that is the best way to configure it to different areas without having to rewrite code for every area. (I want to give one to my uncle as well.)

Anyway, I have a detectable range in my wife's little car of about 5 feet. I imagine my one ton pickup will be detected from slightly further away though I have not tested it yet. This is roughly 6" less detection range than the original code, but it has FAR more stability and is not giving me any false alarms. (I will compare codes and see if I can tell why the original jumps around so much) I wired it to a battery pack and have tested it for hours (with a buzzer so I can hear if it triggers) in multiple places throughout my house, garage, and shop. So far so good!

The project still is not finished. I need to install a relay and get it wired into the gate operator. This last code and initial tests with it give me enough confidence in the system to move forward. If I get a false alarm, it will be easy to adjust the thresholds very little at a time until I have max sensitivity with 0 false triggers.

Thank you all for the help, advice, and guidance. Each little project is a learning experience. I am trying to build projects I will actually use. That makes the learning, failures, and trial and error more fun!

Oh and by the way, I answered my own initial question "Why is magnetometer stronger on one side than the other?" Turns out it's not. Not that I can tell anyway. The magnetometer is incredibly sensitive and is subject to different objects or orientations on different sides of it. If you move it a fraction of a millimeter the readings will change a little. If you move it two fractions of a millimeter it will change a lot! When I started this project I had no idea the magnetometer would be so sensitive. I think I was fighting it's very nature. I had to learn how to manipulate the beast and make it do my bidding.