
#include <SparkFun_MAG3110.h>
MAG3110 mag = MAG3110(); //Instantiate MAG3110
void setup() {
Serial.begin(9600);
mag.initialize();
//This line makes the output data rate a lot slower
//Output Data Rate = 1.25Hz
//Oversampling Ratio = 32
//This means it takes 32 samples and averages the results
if(!mag.error) //You can use this to check if there was an error during initialization.
{
mag.setDR_OS(MAG3110_DR_OS_1_25_32);
mag.start();
}
//You can set your own offsets without calibration
//mag.setOffset(MAG3110_X_AXIS, -100);
//mag.setOffset(MAG3110_Y_AXIS, 300);
//mag.setOffset(MAG3110_Z_AXIS, -300);
//You can read the sensor's offset by calling:
//int offset = mag.readOffset(MAG3110_X_AXIS);
//You can obtain system information by calling any of the following:
//mag.isActive(); //Tells you whether the mag sensor is active or in standby
//mag.isRaw(); //Tells you if the mag sensor is outputting raw data or not
//mag.isCalibrated(); //Tells you if the mag sensor has been calibrated
//mag.isCalibrating(); //Tells you if the mag sensor is currently being calibrated
//uint8_t mode = mag.getSysMode(); //Reads the SYSMOD register. See the datasheet for more information
//This will reset the sensor to default values
//It sets the offsets to 0, flags it as uncalibrated, and sets the device to standby mode
//The Output Data Rate and Oversampling ratio will also be set to 80 and 16 respectively (see datasheet)
//mag.reset();
//This will disable the use of user offsets
//User offsets are enabled by default but are initialized to 0
//mag.rawData(true);
}
void loop() {
float xf, yf, zf;
//Only read data when it's ready
if(mag.error)
Serial.println("Could not connect to MAG3110 Sensor!");
if(mag.dataReady()) {
mag.readMicroTeslas(&xf, &yf, &zf); //This divides the values by 10 to get the reading in microTeslas
Serial.print("X: ");
Serial.print(xf);
Serial.print(", Y: ");
Serial.print(yf);
Serial.print(", Z: ");
Serial.println(zf);
Serial.println("--------");
}
}
A few functions to point out from the example above:
mag.setDR_OS()
- This functions allows you to set the MAG3110’s Output Data Rate (ODR) and Over-sampling Ratio (OSR). The output data rate tells you how many
new datasets the MAG3110 will provide in one second. For example, an output data rate of 80 (the default) will give you 80 new readings per second. The over-
sampling ratio tells the MAG3110 how many samples to average together for one reading. For example, with an OSR of 16 the MAG3110 will take 16 measurements,
average the results together, and give you one averaged result.
These two variables are related, and you can find more about this setting in the datasheet. The library includes defined settings for this that follow this format:
MAG3110_DR_OS_80_16
. This will give you an ODR of 80Hz and an OSR of 16.
mag.setOffset(axis, offset)
- This allows you to set your own offsets in case you have calibration data saved. You can choose which axis to change using these
defined constants:
MAG3110_X_AXIS
,
MAG3110_Y_AXIS
,
MAG3110_Z_AXIS
.
mag.readOffset(axis)
- This allows you to get the offsets the MAG3110 is using. You could use this function to save the offsets after calibration!
Note: If you want to fully save calibration data, you will have to save
mag.x_scale
and
mag.y_scale
. These are both float values that are calculated during
calibration. Be sure to only modify these values when you have saved calibration data or you may have to recalibrate! You can also directly set
mag.calibrated
to
true if you manually calibrate the sensor
mag.isActive()
- Tells you whether the mag sensor is active or in standby
mag.isRaw()
- Tells you if the mag sensor is outputting raw data or not
mag.isCalibrated()
- Tells you if the mag sensor has been calibrated