Wemos Mini 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 used

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.


  • 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

As an added bonus here is the schematic for one of these modules

Parts Required


Name Link
Wemos Mini WeMos D1 mini
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
Base shield Double Socket Dual Base Shield for Wemos D1 Mini



Wemos mini Sensor
3.3v Vcc
Gnd Gnd


Code Example

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


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()
  if (!mag.init())
    Serial.println("Failed to detect and initialize magnetometer!");
    while (1);
void loop()
  snprintf(report, sizeof(report), "M: %6d %6d %6d",
    mag.m.x, mag.m.y, mag.m.z);




Open the serial monitor , move the sensor around and you should readings like this

M: -1639 3336 7493
M: -1839 2769 7615
M: -1374 2834 7749
M: -356 2968 7769
M: -104 2425 7931
M: 53 1223 7841
M: -90 157 7370
M: -488 -1027 6358
M: -1011 -1540 5230
M: -1255 -1699 4822
M: -1417 -1714 4831
M: -1209 -1363 5964
M: -682 -301 7271



Click to access lis3mdl.pdf





Please enter your comment!
Please enter your name here