Magnetometer

A magnetometer is a device that measures magnetic field, typically expressed in Tesla T or Gauss G.   A triaxial magnetometer measures that strength in three directions along the X, Y, and Z axis.   You may use a magnetometer to measure local magnetic fields, or you can use it to detect the Earth's magnetic field.  

In order to use a magnetometer as a digital compass it is necessary to calibrate the device to compensate for hard iron distortion, soft iron distortion, and magnetic declination.   Hard iron distortion is caused by permanent magnets.   Soft iron distortion is caused by the presence of iron, steel and other ferrous materials near the sensor that influence magnetic fields themselves (not the magnetic field source).   The soft iron distortion is dependent upon the orientation of the magnetometer relative to the source of the soft iron distortion.   Magnetic declination is the difference between the magnetic poles of the earth and the geographic poles.  

 

Magnetometer Calibration

Hard iron distortions cause an additive offset to magnetometer readings.   To correct for them, you take a series of X,Y,Z readings while moving the magnetometer in all directions, and then calculate the midway point between the minimum and maximum values for each axis.   The midway point calculated for each axis is the offset correction to be applied to subsequent readings.  


offset_x = (max(x) + min(x)) / 2
offset_y = (max(y) + min(y)) / 2
offset_z = (max(z) + min(z)) / 2

corrected_x = sensor_x - offset_x
corrected_y = sensor_y - offset_y
corrected_z = sensor_z - offset_z

Soft iron distortions are more difficult to remove because they are best performed with the use of a 3x3 transformation matrix.   However, it is possible to use scale biases to achieve a reasonably good correction factor.  


avg_delta_x = (max(x) - min(x)) / 2
avg_delta_y = (max(y) - min(y)) / 2
avg_delta_z = (max(z) - min(z)) / 2

avg_delta = (avg_delta_x + avg_delta_y + avg_delta_z) / 3

scale_x = avg_delta / avg_delta_x
scale_y = avg_delta / avg_delta_y
scale_z = avg_delta / avg_delta_z

corrected_x = sensor_x * scale_x
corrected_y = sensor_y * scale_y
corrected_z = sensor_z * scale_z

The pseudocode below applies the hard iron and soft iron correction factors to the measurement values (sensor_x   sensor_y   sensor_z).  


corrected_x = (sensor_x - offset_x) * scale_x
corrected_y = (sensor_y - offset_y) * scale_y
corrected_z = (sensor_z - offset_z) * scale_z

Magnetic declination must be compensated for if you intend to use a magnetometer to provide a geographic heading.   It is important to note that the magnetic declination changes based on your geographic position (latitude, longitude) on the earth, and it varies from year to year.   See NOAA Magnetic Declination Estimated Value.   It is possible to use an API to fetch the declination values based on latitude / longitude from NOAA's website.  

 

Links

Magnetometer Calibration

Digi-Key How to Calibrate a Magnetometer

How to Calibrate a Magnetometer?

Simple and Effective Magnetometer Calibration

A way to calibrate a magnetometer

 


Do you need help developing or customizing a IoT product for your needs?   Send me an email requesting a free one hour phone / web share consultation.  

 

The information presented on this website is for the author's use only.   Use of this information by anyone other than the author is offered as guidelines and non-professional advice only.   No liability is assumed by the author or this web site.  

Sensors

Sensor Calibration

Sensitivity, Resolution, Response Time, etc.

50000 PPM (5%) CO2 Sensors

MQ Gas Sensors

MQ Gas Sensors

MQ-6 LPG Gas Sensor

MQ-6 LPG Gas Sensor

MQ-7 carbon monoxide CO Gas Sensor

MQ-7 Carbon Monoxide Gas Sensor

Sensor to microcontroller / data acquisition device cables

Accelerometers

IMU / Gyroscope

Magnetometer

Audio

Thermocouple

AF Sensirion SHT40 Temperature & Humidity Sensor

Motion Sensors

object detection sensors

Object Detection Sensors

Strain Gauge

Color

Pressure

Liquid Flow Meter

Operational Amplifiers

Components

Tools

Basic Components

Suppliers

VAC

XBee I/O Line Passing

xBee wireless communication modules with Arduino

xBee wireless communication modules + Arduino

I2C bus / SPI / 1-Wire

UART TTL Serial RS-232

LoRa Communications

Triac

Light Emitting Diode (LED)

NeoPixels

4-20 mA Current Loops

Human Health & Electrical Power

HMI

imgAlt

Mobile Apps

Hall Effect Sensor

NTC Thermistor

Amplify an Analog Signal

Offset a input signal

Electric Motors

Laptop 12V Power Supply

MCP2515 CAN Bus Module

TJA1050 CAN Bus Module

1.2" 4-Digit 7-Segment LED Display

Batteries

2022 Character Display Comparison

Logic Level Converter

Circuit Protection

Diodes

PMOS / P-Channel MOSFET

Logic Level NMOS / N-Channel MOSFET

Voltage Measurement

Analog to Digital Conversion (ADC)

Variable output VDC

Turn On/Off Noisy DC Device

ADC Analog Input