
#include <SparkFun_MAG3110.h>
MAG3110 mag = MAG3110(); //Instantiate MAG3110
void setup() {
Serial.begin(9600);
mag.initialize(); //Initialize the MAG3110
}
void loop() {
int x, y, z;
if(!mag.isCalibrated()) //If we're not calibrated
{
if(!mag.isCalibrating()) //And we're not currently calibrating
{
Serial.println("Entering calibration mode");
mag.enterCalMode(); //This sets the output data rate to the highest possible and puts the mag sensor in active mode
}
else
{
//Must call every loop while calibrating to collect calibration data
//This will automatically exit calibration
//You can terminate calibration early by calling mag.exitCalMode();
mag.calibrate();
}
}
else
{
Serial.println("Calibrated!");
}
mag.readMag(&x, &y, &z);
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Z: ");
Serial.println(z);
Serial.print("Heading: ");
Serial.println(mag.readHeading());
Serial.println("--------");
delay(100);
}
Note a few things about the calibration functions.
Setting the MAG3110 to calibration mode (ie: using
mag.enterCalMode()
) will set it to the highest output data rate and enter active mode. You will need to set your
desired ODR and OSR after calibration, and enter standby if you wish. If you just want to simply read values, you can leave these settings alone.
When calibrating, you must call
mag.calibrate()
every loop cycle. This allows the code to sample the MAG3110 readings and find the minimum and maximum
values.
The calibration mode will automatically exit after some time but at least 5 seconds. If this is too long, you can terminate the calibration earlier by calling
mag.exitCalMode()
. Please note that the calibration may be offset if you do not calibrate enough!
If you have not calibrated the MAG3110, calling
mag.readHeading()
will only give you 0! Otherwise, it will show values ranging from -180 to +180. When the heading
is 0, the x-axis is pointing towards magnetic north (see the diagram from before).
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.
Alternative Applications
Aside from sensing magnetic north, this sensor has a few other uses. Many high power electronics create magnetic fields of their own. With this sensor, you can see
this disturbance.
An example sketch is included with the library called
SparkFun-MAG3110-Magnitude.ino
. This sketch first calibrates the sensor and then outputs serial data of the
magnitude of the scaled x and y-axes. Recall the magnitude of a vector is:
In this case a is 0 since we do not take the z-axis into account.
Once the sketch is running and calibration has completed, open up the Tools->Serial Plotter in the Arduino IDE (only available in the newest versions of the Arduino
IDE). If you put the sensor near a microwave and then warm something up, you will see some noise on the signal.
3