sparkfun MAG3110 Скачать руководство пользователя страница 7

#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

Содержание MAG3110

Страница 1: ...r You may want to use something like this 3 3V Low Dropout Regulator LDO For the rest of the items you will need see the wish list below SparkFun Logic Level Converter Bi Directional BOB 12009 If you...

Страница 2: ...upply Voltage 1 95V to 3 6V GND Must be connected to ground SDA Serial Data pin for I C Communication SCL Serial Clock pin for I C Communication INT Interrupt pin high when new data is ready You may n...

Страница 3: ...SCL Leonard Micro 2 SDA 3 SCL Due 20 SDA 21 SCL SDA1 SCL1 SparkFun MAG3110 Library SparkFun has created a library to make it easier to get readings from the MAG3110 sensor It also has code to calibra...

Страница 4: ...lk 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 whet...

Страница 5: ...n 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 functi...

Страница 6: ...0 Triggered You can also do triggered measurements while in active mode This is more advanced and is covered in the datasheet Calibration All these functions are great but what if you need to find a p...

Страница 7: ...e 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...

Страница 8: ...library allows You can use an accelerometer in conjunction with this device to create a tilt compensated compass SparkFun has a variety of accelerometers available including the ADXL345 To get you sta...

Отзывы: