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

#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

Содержание 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...

Отзывы: