
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.
Requirements
- 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
Step-by-Step
- Hardware Setup
- Installing Arduino Libraries
- Code
- Result
- 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() {
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();
}
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.