KalbeAbbasRachel Janse van Rensburg
Published © MIT

3D Magnetic Field Sensing with XinaBox

Learn how to make Calibrated 3-dimensional magnetic field sensor with XinaBox using xChip SI01 – IMU 9DoF.

BeginnerFull instructions provided6 minutes444
3D Magnetic Field Sensing with XinaBox

Things used in this project

Hardware components

CW01
XinaBox CW01
×1
IP02
XinaBox IP02
×1
OD01
XinaBox OD01
×1
SI01
XinaBox SI01
×1
XC10
XinaBox XC10
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Magnetometer

C/C++
Calibrate after uploading code by moving the device in figure 8 pattern.
#include <xCore.h>
#include <xSI01.h>
#include <xOD01.h>


xSI01 SI01;
xOD01 OD01;

float mX,mY,mZ; //variable to store raw values

//variables to store calibration parameters
float mx_min, my_min, mz_min;
float mx_max, my_max, mz_max;
float mx_offset, my_offset, mz_offset;
float mx_calibrated, my_calibrated, mz_calibrated;
float mx_scale, my_scale, mz_scale;
float avg_scale

static unsigned long startTime;

void setup() {

  mx_max=0;my_max=0;mz_max=0;
  mx_min=0;my_min=0;mz_min=0;

  startTime=millis();

   // Start the Serial Monitor at 115200 BAUD
  Serial.begin(115200);
  
 // Set the I2C Pins for CW01
  #ifdef ESP8266
    Wire.pins(2, 14);
    Wire.setClockStretchLimit(15000);
  #endif

  Wire.begin();
    
  if (!SI01.begin()) {
    Serial.println("Failed to communicate with SI01.");
    Serial.println("Check the Connector");
  } else {
    Serial.println("start successful");
  }

 OD01.begin();
 OD01.println("Move the device");
 OD01.println("in figure 8");
 OD01.println("pattern for 10s");
 delay(1000);


  //Calibration loop, keeps running for 10 seconds
  while(true)
  {

    SI01.poll();
    mX=SI01.getMX();mY=SI01.getMY();mZ=SI01.getMZ();

    if (mX > mx_max) mx_max = mX;if (mY > my_max) my_max = mY;if (mZ > mz_max)      mz_max = mZ;
    if (mX < mx_min) mx_min = mX;if (mY < my_min) my_min = mY;if (mZ < mz_min)      mz_min = mZ;
  
    mx_offset = (mx_min + mx_max)/2;
    my_offset = (my_min + my_max)/2;
    mz_offset = (mz_min + mz_max)/2;

    mx_scale = (mx_max - mx_min)/2;
    my_scale = (my_max - my_min)/2;
    mz_scale = (mz_max - mz_min)/2;

    avg_scale=(mx_scale + my_scale + mz_scale)/3;

    delay(100);

    if (millis()-startTime>10000) break;

  }
  
  OD01.println("Calibration done!");
  delay(2000);
  OD01.clear();

}

void loop() {
  
  SI01.poll();

  mX=SI01.getMX();mY=SI01.getMY();mZ=SI01.getMZ();
  mx_calibrated = avg_scale/(mX - mx_offset);
  my_calibrated = avg_scale/(mY - my_offset);
  mz_calibrated = avg_scale/(mZ - mz_offset);

  OD01.set2X();
  
  
  //Prints result to Serial output
  Serial.print(" MX: ");
  Serial.print(mx_calibrated);
  Serial.print(" MY: ");
  Serial.print(my_calibrated);
  Serial.print(" MZ: ");
  Serial.print(mz_calibrated);
  Serial.println();

  //Prints result to OD01
  OD01.println("Magnetic: ");
  OD01.print(" MX: ");
  OD01.println(mx_calibrated);
  OD01.print(" MY: ");
  OD01.println(my_calibrated);
  OD01.print(" MZ: ");
  OD01.print(mz_calibrated);
  delay(2000);
  OD01.clear();

}

Credits

KalbeAbbas

KalbeAbbas

25 projects • 12 followers
An enthusiastic and dedicated Electronic engineer graduated from SSUET Karachi, Pakistan. Loves to discover Embedded electronics projects.
Rachel Janse van Rensburg

Rachel Janse van Rensburg

8 projects • 2 followers
Thanks to XinaBox.

Comments