mag3110 calibration

Hi all,

I am using the sparksfun mag3110 digital compass. I am also using their library from (GitHub - sparkfun/SparkFun_MAG3110_Breakout_Board_Arduino_Library: Arduino Library for the SparkFun Triple Axis Magnetometer Breakout - MAG3110) to calibrate it. The problem I am having is that I have to calibrate the compass everytime I use it. Is there a way to just do this once. Also how do i change the scale from 180 to -180 degrees to 0-360 degrees.

The calibration process is used to calcalute a zero-flux offset to be applied to the measurements. It has to be done everytime the magnetometer is integrated into a new environment (it is also used to compensate for hard-iron offsets).

The result of the calibration can be stored in the user offset registers in the mag3110. The device then can be configured to apply the offset automatically to every measurement. You can store the calibration result in the eeprom of the arduino and send it to the mag3110 after each power cycle.

NoName12221:
Also how do i change the scale from 180 to -180 degrees to 0-360 degrees.

y = (x >= 0) ? x : x + 360;

Thanks for the replies.

I got the scaling working. On the sparksfun's site (MAG3110 Magnetometer Hookup Guide - learn.sparkfun.com they talk about the) they talk about how to save calibration, but they don't go in much details. After the calibration these are the values I get for the x, y and z.

x = -2878, y = 2928, z = -467;

Are they the offset values or for example the mag.x_Scale, mag.y_Scale and mag.z_Scale as they mention.

https://learn.sparkfun.com/tutorials/mag3110-magnetometer-hookup-guide-:
As mentioned before, if you want to manually calibrate the MAG3110 using previously obtained calibration values, you need to do a few things: Save the offsets using mag.readOffset(), and save the scaling floats, mag.x_scale and mag.y_scale. When reloading this calibration, you have to use mag.setOffset() and write the old scaling factors back to mag.x_scale and mag.y_scale. For completeness, update the mag.calibrated value to true as well. Note that as the calibration data becomes outdated, recalibration is recommended.

The guide tells you pretty much everything you need to do. After calibration is done, read the offsets for each axis (x and y as z axis is not calibrated anyways). Then read the scaling floats and store them somewhere. After every reset of the arduino or every power cycle of the mag3110 you need to restore those values.

Here is some pseudo code

// After calibration
int magXOffset = mag.readOffset(0);
int magYOffset = mag.readOffset(1);

storeInEEPROM(MAG_X_OFFSET_MEM_ADDR, magXOffset);
storeInEEPROM(MAG_Y_OFFSET_MEM_ADDR, magYOffset);
storeInEEPROM(MAG_X_SCALE_MEM_ADDR, mag.x_scale);
storeInEEPROM(MAG_Y_SCALE_MEM_ADDR, mag.y_scale);
// In setup
int magXOffset = readIntFromEEPROM(MAG_X_OFFSET_MEM_ADDR);
int magYOffset = readIntFromEEPROM(MAG_Y_OFFSET_MEM_ADDR);
float magXScale = readFloatFromEEPROM(MAG_X_SCALE_MEM_ADDR);
float magYScale = readFloatFromEEPROM(MAG_Y_SCALE_MEM_ADDR);

mag.setOffset(0, magXOffset);
mag.setOffset(1, magYOffset);
mag.x_scale = magXScale;
mag.y_scale = magYScale;
mag.calibrated = true;

i tried running your codes, but it came with these errors.

Arduino: 1.8.8 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

SparkFun_MAG3110_Other:36:14: error: expected constructor, destructor, or type conversion before '(' token

 storeInEEPROM(MAG_X_OFFSET_MEM_ADDR, magXOffset);

              ^

SparkFun_MAG3110_Other:37:14: error: expected constructor, destructor, or type conversion before '(' token

 storeInEEPROM(MAG_Y_OFFSET_MEM_ADDR, magYOffset);

              ^

SparkFun_MAG3110_Other:38:14: error: expected constructor, destructor, or type conversion before '(' token

 storeInEEPROM(MAG_X_SCALE_MEM_ADDR, mag.x_scale);

              ^

SparkFun_MAG3110_Other:39:14: error: expected constructor, destructor, or type conversion before '(' token

 storeInEEPROM(MAG_Y_SCALE_MEM_ADDR, mag.y_scale);

              ^

C:\Users\Ash\Desktop\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\examples\SparkFun_MAG3110_Other\SparkFun_MAG3110_Other.ino: In function 'void setup()':

SparkFun_MAG3110_Other:52:36: error: 'MAG_X_OFFSET_MEM_ADDR' was not declared in this scope

 int magXOffset = readIntFromEEPROM(MAG_X_OFFSET_MEM_ADDR);

                                    ^

SparkFun_MAG3110_Other:52:57: error: 'readIntFromEEPROM' was not declared in this scope

 int magXOffset = readIntFromEEPROM(MAG_X_OFFSET_MEM_ADDR);

                                                         ^

SparkFun_MAG3110_Other:53:36: error: 'MAG_Y_OFFSET_MEM_ADDR' was not declared in this scope

 int magYOffset = readIntFromEEPROM(MAG_Y_OFFSET_MEM_ADDR);

                                    ^

SparkFun_MAG3110_Other:54:39: error: 'MAG_X_SCALE_MEM_ADDR' was not declared in this scope

 float magXScale = readFloatFromEEPROM(MAG_X_SCALE_MEM_ADDR);

                                       ^

SparkFun_MAG3110_Other:54:59: error: 'readFloatFromEEPROM' was not declared in this scope

 float magXScale = readFloatFromEEPROM(MAG_X_SCALE_MEM_ADDR);

                                                           ^

SparkFun_MAG3110_Other:55:39: error: 'MAG_Y_SCALE_MEM_ADDR' was not declared in this scope

 float magYScale = readFloatFromEEPROM(MAG_Y_SCALE_MEM_ADDR);

                                       ^

exit status 1
expected constructor, destructor, or type conversion before '(' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

NoName12221:
i tried running your codes, but it came with these errors.

LightuC:
Here is some pseudo code

The actual implementation is left to you as an exercise.