Home Code Arduino and LIS3MDL magnetic field sensor example

Arduino and LIS3MDL magnetic field sensor example

by shedboy71
[lebox id="1"]

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

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
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
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

Couldn't find a good fritzing part or image to use, this is the sensor I bought

 

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

Arduino Sensor
3.3v Vcc
Gnd Gnd
SDA (A4) SDA
SCL (A5) 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

M: -1934 5556 6732
M: -2222 5096 6958
M: -1508 5329 7391
M: -1174 5782 7194
M: -119 6447 6429
M: 447 6333 4702
M: 92 6073 4256
M: -1190 6107 5068
M: -1401 5808 6758
M: -502 5195 7839
M: 740 4453 8173
M: 1327 3639 8161
M: 1421 3006 4294

 

Links

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

 

 

 

Share
[lebox id="2"]

You may also like

Leave a Comment