Calibrating the MAG3110

I have a MAG3110 unit that I’m using with an Arduino. What I would like to do is to save my calibration data and then reload it in another sketch. I’m not interested in using the magnetometer as a compass, but instead would like to simply zero each axis within a mu-metal magnetic shielding container so I can use the device for absolute magnetic field measurements (or as close as possible!).

I can run the calibration example file (SparkFun_MAG3110_Calibrated), which I’ve used as a starting point. I’ve extended it to work for the z-axis as well. From there, I have saved the offset value (x_offset) and scale (x_scale) from the calibration in the EEPROM. I can reload these values in another sketch, however, I can’t seem to actually set the offsets to the calibrated values. I’m using the setOffset function as mentioned in https://learn.sparkfun.com/tutorials/ma … guide-/all. For x_scale, I simply load the saved variable when initializing the magnetometer, but I’m not sure if this is the right thing to do.

As an example, my raw x reading (without calibration) is around 1200 mG. After running the calibration sketch, this resets to a value close to zero, which I would have expected. x_offset comes out as around 200 mG and x_scale is 0.00. When I load these values in another sketch, my x axis reads as around 1000 mG (i.e., 1200 - 200). Naively, I would expect that the offset should be more like 1200. The x scale is a mystery to me. The calculation for it is x_scale = 1.0/(x_max - x_min) from the SparkFun library, so how can it possibly come out as 0.00?

If anyone has any experience with the magnetometer, I would appreciate some assistance! I’m a total newbie to Arduino, so it’s possible that I’m missing something obvious.

Okay, I have solved my previous problem regarding storing the offset and scale values. I was using EEPROM.write to save, but actually, EEPROM.put is better in this case. Because EEPROM.write can only save integer values between 0 and 255 (as I understand it), it was saving my offset as modulo 256, possibly. EEPROM.put works with no problems so far, and I can retrieve my offset values using EEPROM.get.

I have also figured out the scale factor. By printing additional decimal places , e.g., Serial.println(x_scale, 5), it is revealed that x_scale is small, but finite. I verified the value by monitoring x_min and x_max and checking the calculation myself. I got x_min = 1232 and x_max = 1760, which gives x_scale = 0.00189 using the formula in my original post. By default, Serial.print gives only two decimal places, which gives the appearance of the scale factor being zero, when in fact it is not!

Maybe this will be useful to other confused new people like myself! I’m happy to share my sketch modifications if there is any interest. They are simple, but when you’re new and don’t know what you’re doing, it can take a day or so to figure out -_-