Home » LIS3MDL magnetic field sensor connected to a Wemos Lolin ESP32 module

LIS3MDL magnetic field sensor connected to a Wemos Lolin ESP32 module

by shedboy71

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.

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

Name Link
ESP32
LIS3MDL  

Connecting cables
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

Schematic/Connection

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
SDA/21 SDA
SCL/22 SCL

 

Code Example

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

[codesyntax lang=”cpp”]

/*
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);
}

[/codesyntax]

Output

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

 

Links

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

 

 

 

You may also like

Leave a Comment

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.