Microbit and LIS3MDL magnetic field sensor example

In this article we look at another sensor – this time its the LIS3MDL which is a 3-axis MEMS magnetic field sensor, digital output, I2C, SPI, low power mode, high performance

This is the sensor that I bought

Lets see what the sensor manufacturer says about it

The LIS3MDL has user-selectable full scales of ±4/±8/±12/±16 gauss.
The self-test capability allows the user to check the functioning of the sensor in the final application.
The device may be configured to generate interrupt signals for magnetic field detection.
The LIS3MDL includes an I2C serial bus interface that supports standard and fast mode (100 kHz and 400 kHz) and SPI serial standard interface.
The LIS3MDL is available in a small thin plastic land grid array package (LGA) and is guaranteed to operate over an extended temperature range of -40 °C to +85 °C.

Features

  • Wide supply voltage, 1.9 V to 3.6 V
  • Independent IO supply (1.8 V)
  • ±4/±8/±12/±16 gauss selectable magnetic full scales
  • Continuous and single-conversion modes
  • 16-bit data output
  • Interrupt generator
  • Self-test
  • I2C/SPI digital output interface
  • Power-down mode / low-power mode

Parts Required

Once again for ease of use I connect an expansion board to the microbit, I feel this makes it easier to connect to a sensor like the one pictured above using connecting wire

Name Link
Microbit BBC Micro:bit Micro-Controller with Motion Detection, Compass, LED Display and Bluetooth
LIS3MDL Taidacent LIS3MDL High Precision 3 Axis Magnetometer Sensor Compass Module Tilt Compensated Compass Replacement HMC5883L
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
expansion board Microbit expansion board

Schematic/Connection

Microbit Sensor
3.3v Vcc
Gnd Gnd
SDA – 20 SDA
SCL – 19 SCL

 

Code Example

This uses the library from https://github.com/pololu/lis3mdl-arduino

This is the serial example which as you can guess simply outputs values to the serial port

/*
The sensor outputs provided by the library are the raw 16-bit values
obtained by concatenating the 8-bit high and low magnetometer data registers.
They can be converted to units of gauss using the
conversion factors specified in the datasheet for your particular
device and full scale setting (gain).
 
Example: An LIS3MDL gives a magnetometer X axis reading of 1292 with its
default full scale setting of +/- 4 gauss. The GN specification
in the LIS3MDL datasheet (page 8) states a conversion factor of 6842
LSB/gauss (where LSB means least significant bit) at this FS setting, so the raw
reading of 1292 corresponds to 1292 / 6842 = 0.1888 gauss.
*/
 
#include <Wire.h>
#include <LIS3MDL.h>
 
LIS3MDL mag;
 
char report[80];
 
void setup()
{
  Serial.begin(9600);
  Wire.begin();
 
  if (!mag.init())
  {
    Serial.println("Failed to detect and initialize magnetometer!");
    while (1);
  }
 
  mag.enableDefault();
}
 
void loop()
{
  mag.read();
 
  snprintf(report, sizeof(report), "M: %6d %6d %6d",
    mag.m.x, mag.m.y, mag.m.z);
  Serial.println(report);
 
  delay(100);
}

 

Output

Open the serial monitor and you should see something like this, move the sensor about to see the values change

M: -2304 -972 4756
M: -2197 -879 5280
M: -1554 -788 5950
M: -1067 -485 6546
M: -570 182 7095
M: -1822 567 6935
M: -3282 1201 6044

Links

https://www.st.com/resource/en/datasheet/lis3mdl.pdf