3D Magnetic Field Sensing with XinaBox

3D Magnetic Field Sensing with XinaBox

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

XinaBox produces a range of 80+ modular xChips, which include cores/CPUs, sensors, power, communication, output, and storage, using a connectivity standard without wires, soldering, breadboards or hardware knowledge

xChip CW01 (ESP8266 Core) is a microprocessor and Wi-Fi module, that enables users to send/receive data from XinaBox's modular xChips to/from the cloud.

xChip SI01 (LSM9DS1) is an IMU 9DoF module comprises of 3 sensors, an accelerometer that measures acceleration of the X, Y and Z axes, a magnometer that computes orientation to magnetic north, on the X, Y and Z axes, therefore we can calculate which direction we are facing as well as the angle at which we are leaning. The last sensor is the gyroscope that measures our orientation to the center of the earth.

xChip OD01 is a display module that forms part of the output modules and is equipped with an OLED display unit that is capable of displaying any graphics or text.

The magnetic field sensor embedded in xChip SI01 senses all of the magnetic fields nearby. Due to magnetic field distortions caused by hard and soft iron effects, calibration is required to get correct measurements. Therefore, a calibration algorithm is added to the program.

By the end of this guide, you will be able to build a 3D magnetic sensor (magnetometer) that will first require calibration by moving in the device in a figure 8 pattern to give accurate measurements.


  • 1x CW01 - Wi-Fi Core (ESP8266/ESP-12F)
  • 1x IP01/IP02 – Simple/Advanced USB Programming Interface (FT232R)
  • 1x OD01 - OLED Display 128x64 (SSD1306)
  • 1x SI01 - IMU 9DoF (LSM9DS1)
  • 1x XC10 - 10-Pack xBus Connectors
  • Arduino IDE


  1. Hardware Setup
  2. Installing Arduino Libraries
  3. Code
  4. Result
  5. Summary

1. Hardware Setup

1. Connect xChips CW01, SI01 and IP01/IP02 together using the XC10 xBus connectors. You may connect it as shown in the diagram below. Please see this guide on how to assemble xChips generally.

2. Then connect IP01/IP02 to the USB port of your PC.

3. Select “XinaBox CW01” Board in Arduino IDE for uploading code, after installing required Arduino cores/libraries.

2. Installing Arduino Libraries

1. Install Arduino IDE 1.8.8

2. Install these cores/libraries into Arduino IDE:

NOTE: If you are not familiar with how to install libraries, please refer to the links: Installing Arduino libraries and Installing cores

3. 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() {



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

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

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

  //Calibration loop, keeps running for 10 seconds


    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;


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

  OD01.println("Calibration done!");


void loop() {

  mx_calibrated = avg_scale/(mX - mx_offset);
  my_calibrated = avg_scale/(mY - my_offset);
  mz_calibrated = avg_scale/(mZ - mz_offset);

  //Prints result to Serial output
  Serial.print(" MX: ");
  Serial.print(" MY: ");
  Serial.print(" MZ: ");

  //Prints result to OD01
  OD01.println("Magnetic: ");
  OD01.print(" MX: ");
  OD01.print(" MY: ");
  OD01.print(" MZ: ");


4. Result

1. After uploading the code, power on the device.

2. The device will request to calibrate by moving it in a figure 8 pattern at startup.

3. After 10 seconds of calibration, the magnetic field values for 3-Axis will be displayed.

5. Summary

In this project we have shown you how to make a Magnetic field sensor using XinaBox xChips CW01 and SI01. Using the sensor we can make a digital compass for navigation and routing projects. The project is simple and requires little or no hardware knowledge to build.

Previous article Build Your Own Digital Compass with XinaBox
Next article Instant Control of Appliances with XinaBox and Blynk