MAG3110 Magnetometer Hookup Guide

Pages
Contributors: AGlass0fMilk
Favorited Favorite 1

SparkFun MAG3110 Library

SparkFun has created a library to make it easier to get readings from the MAG3110 sensor. It also has code to calibrate the sensor and obtain magnetic north headings!

Note: The calibration and magnetic north readings only work if the magnetometer is oriented level with the z axis pointing up or down! Obtaining magnetic north independent of orientation requires the use of an accelerometer to know which way is down and more complex math.

You can download the library here along with example code. You can find the latest library and example files in the GitHub repository for this library. Not sure how to install an Arduino library? Check out this guide!

There are a variety of functions beyond basic readings. To learn more, browse through the included examples in the library. You can also look at the library source code. We'll go over a few of the basic commands in this guide.

Using the Library

Once you have the library installed, open the included example SparkFun-MAG3110-Basic.ino.

This sketch is bare-bones way of reading data from the MAG3110.

language:c
#include <SparkFun_MAG3110.h>

MAG3110 mag = MAG3110(); //Instantiate MAG3110

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

  Wire.begin();             //setup I2C bus
  Wire.setClock(400000);    // I2C fast mode, 400kHz

  mag.initialize(); //Initializes the mag sensor
  mag.start();      //Puts the sensor in active mode
}

void loop() {

  int x, y, z;
  //Only read data when it's ready
  if(mag.dataReady()) {
    //Read the data
    mag.readMag(&x, &y, &z);

    Serial.print("X: ");
    Serial.print(x);
    Serial.print(", Y: ");
    Serial.print(y);
    Serial.print(", Z: ");
    Serial.println(z);

    Serial.println("--------");
  }
}

Calling mag.initialize() sets up the sensor and checks whether a MAG3110 is connected properly. If mag.error is true, the Arduino was unable to talk to the MAG3110! When initialized, the magnetometer is set to standby mode with all offsets set to 0. To put the magnetometer in active mode and start sampling, simply write mag.start()

You can check whether any new data is ready using the mag.dataReady() function. This will return true if new data is available.

You can read all three axes using the mag.readMag() function. Don't be afraid of the & symbol! This simply means we are giving the address of the variables to the function so that they can be filled with the data. If this seems confusing, you might want to read about pointers.

If you run this sketch and open up Tools->Serial Monitor, you should see the following:

serial output

Other functions can be seen in the SparkFun-MAG3110-Other.ino sketch:

language:c
#include <SparkFun_MAG3110.h>

MAG3110 mag = MAG3110(); //Instantiate MAG3110

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

  Wire.begin();             //setup I2C bus
  Wire.setClock(400000);    // I2C fast mode, 400kHz

  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

mag.isCalibrating() - Tells you if the mag sensor is currently being calibrated

mag.getSysMode() - Reads the SYSMOD register. See the datasheet for more information

mag.rawData(boolean) - This sets whether the MAG3110 will output raw (non-user corrected) or offset data. Giving the function a true value will set the data to raw output, and vice-versa.

mag.readMicroTeslas() - This gives data in µTeslas. It really just divides the normal output by 10 since this is the scale the MAG3110 uses.

There are additional functions shown in other example sketches included with the library. A few of these are:

mag.readRegister(address) - This allows you to read any register of the MAG3110. Read the datasheet for more information. The library has all registers mapped in the format MAG3110_REGISTER_NAME.

mag.writeRegister(address, value) - This allows you write a value to any register of the MAG3110. Be careful if you aren't sure what you're doing! You might change settings you didn't mean to!

mag.triggerMeasurement() - When the MAG3110 is in standby, this triggers a single measurement to take place. It lets you save power by keeping the device mostly inactive and only take data when you need to. Be aware this single-shot reading may be less accurate than active mode reading since the signal takes time to settle. See the datasheet for more information. If you want to learn how to do triggered measurements, see the included example "SparkFun-MAG3110-Triggered".

You can also do triggered measurements while in active mode. This is more advanced and is covered in the datasheet.