LIS3MDL magnetic field sensor connected to a Wemos Lolin ESP32 module

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 sensor

Lets look at the sensor and the usual specs

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

Parts Required


Name Link
Wemos Lolin32 WeMos Mini D1 LOLIN32 ESP32
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
sensor shield Expansion IO Board Sensor Shield


This is the sensor I bought


Here is a schematic for this module

Here is how I connected this to my Wemos Lolin32

ESP32 Sensor
3.3v Vcc
Gnd Gnd


Code Example

This uses the library from

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 and you should see something like this, move the sensor about to see the values change

M: -1411 -767 6957
M: -1767 -1288 6568
M: -2956 -842 5892
M: -2134 -1613 5961
M: 1148 -1143 6532
M: 1654 514 7015
M: 485 -559 7097



Click to access lis3mdl.pdf




Previous articleLPS22HB absolute pressure sensor and ESP32 example
Next articleBMM150 digital geomagnetic sensor and ESP32 example