# Adafruit LIS2MDL Triple Axis Magnetometer

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/088/770/medium800/sensors_beauty_crop.jpg?1582909819)

Sense the magnetic fields that surround us with this handy triple-axis magnetometer (compass) module. Magnetometers can sense where the strongest magnetic force is coming from, generally used to detect magnetic north, but can also be used for measuring magnetic fields. This sensor tends to be paired with a 6-DoF (degree of freedom) accelerometer/gyroscope to create a 9-DoF inertial measurement unit that can detect its orientation in real-space, thanks to Earth's stable magnetic field. It's a great match for any of our 6-DoF IMU sensors such as the[&nbsp;LSM6DSOX](https://www.adafruit.com/product/4438) or [LSM6DS33](https://www.adafruit.com/product/4480).

![](https://cdn-learn.adafruit.com/assets/assets/000/088/773/medium800/sensors_top_wide_crop.jpg?1582910115)

We based this breakout on ST's LIS2MDL, a great general purpose magnetometer. This compact sensor uses I2C to communicate and its very easy to use. Simply download our library and connect the SCL pin to your I2C clock pin, and SDA pin to your I2C data pin and upload our test program to read out magnetic field data. If you'd like, you can also use SPI to receive data (we just happen to prefer I2C here)

This sensor can measure up to nearly **+-50 gauss** (49.152 gauss to be specific **,** +- 4952 uTesla) which is quite a bit! It also has an adjustable data rate and can take measurements as slowly as 10Hz and as fast as 100Hz.

![](https://cdn-learn.adafruit.com/assets/assets/000/088/772/medium800/sensors_stemma_cleanup.jpg?1582910051)

This adorable little magnetometer is quite capable but it is near-microscopic at 2mm square. To make things easier, we've put it on a&nbsp; breakout PCB along with support circuitry to let you use this little wonder with 3.3V (Feather/Raspberry Pi) or 5V (Arduino/ Metro328) logic levels.

Additionally since it speaks I2C, you can easily connect it up with two wires (plus power and ground!).&nbsp; We've even included&nbsp;[SparkFun qwiic](https://www.sparkfun.com/qwiic)&nbsp;compatible**&nbsp;[STEMMA QT](https://learn.adafruit.com/introducing-adafruit-stemma-qt)**&nbsp;connectors for the I2C bus so&nbsp; **you don't even need to solder!** &nbsp;Just wire up to your favorite micro and&nbsp;[you can use our CircuitPython / Python](https://github.com/adafruit/Adafruit_CircuitPython_LIS2MDL)&nbsp;or&nbsp;[Arduino drivers to easily interface with the LIS2MDL](https://github.com/adafruit/Adafruit_LIS2MDL)&nbsp;and get magnetic measurements ASAP.

# Adafruit LIS2MDL Triple Axis Magnetometer

## Pinouts

![](https://cdn-learn.adafruit.com/assets/assets/000/088/774/medium800/sensors_top_wide_crop.jpg?1582910142)

## Power Pins

- **Vin** - this is the power pin. Since the sensor chip uses 3 VDC, we have included a voltage regulator on board that will take 3-5VDC and safely convert it down. To power the board, give it the same power as the logic level of your microcontroller - e.g. for a 5V microcontroller like Arduino, use 5V
- **3Vo** - this is the 3.3V output from the voltage regulator, you can grab up to 100mA from this if you like
- **GND** - common ground for power and logic

## I2C Logic Pins

- **SCL** - I2C clock pin, connect to your microcontroller's I2C clock line. This pin is level shifted so you can use 3-5V logic, and there's a **10K pullup** on this pin.
- **SDA** - I2C data pin, connect to your microcontroller's I2C data line. This pin is level shifted so you can use 3-5V logic, and there's a 1 **0K pullup** on this pin.
- **[STEMMA QT](https://learn.adafruit.com/introducing-adafruit-stemma-qt)&nbsp;-** These connectors allow you to connectors to dev boards with&nbsp; **STEMMA QT** connectors or to other things with&nbsp;[various associated accessories](https://www.adafruit.com/?q=JST%20SH%204)

## SPI Logic pins:

All pins going into the breakout have level shifting circuitry to make them 3-5V logic level safe. Use whatever logic level is on **Vin!**

- **SCL** - This is _also_&nbsp;the **S** PI **C** loc **k** pin, it's an input to the chip
- **SDA** &nbsp;- this is _also&nbsp;_the **S** erial **D** ata **I** n / **M** icrocontroller **O** ut **S** ensor **I** n pin, for data sent from your processor to the LIS2MDL
- **SDO** - this is the **S** erial **D** ata **O** ut / **M** icrocontroller **I** n **S** ensor **O** ut&nbsp;pin, for data sent from the LIS2MDL to your processor.&nbsp;
- **CS** - this is the **C** hip **S** elect pin, drop it low to start an SPI transaction. Its an input to the chip

If you want to connect multiple LIS2MDLs to one microcontroller, have them share the **SDA** , **SDO** and **SCL** &nbsp;pins. Then assign each one a unique **CS** pin.

# Adafruit LIS2MDL Triple Axis Magnetometer

## Arduino

# I2C Wiring

Use this wiring if you want to connect via I2C interface:

The I2C address is **0x1E.**

- Connect **board VIN (red wire)** to&nbsp; **Arduino**  **5V** &nbsp;if you are running a **5V** board Arduino (Uno, etc.).&nbsp;If your board is **3V,** connect to that instead.
- Connect **board** **GND (black wire)&nbsp;**to&nbsp; **Arduino**  **GND**
- Connect **board** **SCL (yellow wire)&nbsp;**to&nbsp; **Arduino**  **SCL**
- Connect **board** **SDA (blue wire)&nbsp;**to&nbsp; **Arduino**  **SDA**

![sensors_c_arduino_wiring_qt.png](https://cdn-learn.adafruit.com/assets/assets/000/088/078/medium640/sensors_c_arduino_wiring_qt.png?1580864853)

![sensors_c_arduino_wiring_bb.png](https://cdn-learn.adafruit.com/assets/assets/000/088/079/medium640/sensors_c_arduino_wiring_bb.png?1580864876)

# SPI Wiring

Since this is a SPI-capable sensor, we can use hardware or 'software' SPI. To make wiring identical on all microcontrollers, we'll begin with 'software' SPI. The following pins should be used:

- Connect **Vin** to the power supply, 3V or 5V is fine. Use the same voltage that the microcontroller logic is based off of.
- Connect **GND** to common power/data ground
- Connect the **SCL** &nbsp;pin to **Digital #13** but any pin can be used later
- Connect the **DO** pin to **Digital #12** but any pin can be used later
- Connect the **SDA** &nbsp;pin to **Digital #11** but any pin can be used later
- Connect the **CS** pin **Digital #10** but any pin can be used later

![sensors_c_arduino_wiring_spi.png](https://cdn-learn.adafruit.com/assets/assets/000/088/775/medium640/sensors_c_arduino_wiring_spi.png?1582910540)

Later on, once we get it working, we can adjust the library to use hardware SPI if you desire, or change the pins to others.

# Library Installation

You can install the **Adafruit LIS2MDL Library** for Arduino using the Library Manager in the Arduino IDE.

![](https://cdn-learn.adafruit.com/assets/assets/000/088/081/medium800/sensors_ARDUINO_-_library_manager_menu.png?1580865240)

Click the&nbsp; **Manage Libraries ...** menu item, search for **Adafruit LIS2MDL** ,&nbsp;and select the&nbsp; **Adafruit LIS2MDL** &nbsp;library:

![](https://cdn-learn.adafruit.com/assets/assets/000/088/080/medium800/sensors_c_arduino_libmgr_search.png?1580865209)

Then follow the same process for the&nbsp; **Adafruit BusIO** &nbsp;library.

![](https://cdn-learn.adafruit.com/assets/assets/000/088/083/medium800/sensors_ARDUINO-_BusIO_Lib_Manager.png?1580865272)

Finally follow the same process for the&nbsp; **Adafruit Unified Sensor** library:

![](https://cdn-learn.adafruit.com/assets/assets/000/088/082/medium800/sensors_ARDUINO_-Unified_Sensor_LibMGR.png?1580865263)

# Load Example

Open up&nbsp; **File -\> Examples -\> Adafruit LIS2MDL**  **-\> magsensor** and upload to your Arduino which has been wired up to the sensor.

Depending on whether you are using I2C or SPI, change the pin names and comment or uncomment the following lines.

```cpp
if (!lis2mdl.begin()) {  // I2C mode
  //if (! lis2mdl.begin_SPI(LIS2MDL_CS)) {  // hardware SPI mode
  //if (! lis2mdl.begin_SPI(LIS2MDL_CS, LIS2MDL_CLK, LIS2MDL_MISO, LIS2MDL_MOSI)) { // soft SPI
```

Once you upload the code and open the Serial Monitor ( **Tools-\>Serial Monitor** ) at **115200** baud, you will see the current configuration printed, followed by magnetometer measurements, similar to this:

![](https://cdn-learn.adafruit.com/assets/assets/000/088/084/medium800/sensors_c_arduino_example_ss.png?1580865585)

The sensor class in the magnetometer library reports X, Y and Z axis magnetometer readings directly in [micro-Teslas](http://en.wikipedia.org/wiki/Tesla_(unit)). The **magsensor** example code&nbsp;reads from the sensor and prints the micro-Tesla readings to the Serial Monitor.  
  
In the absence of any strong local magnetic fields, the sensor readings should reflect the magnetic field of the earth (between 20 and 60 micro-Teslas). When the sensor is held level, by calculating the angle of the magnetic filed with respect to the X and Y axis, the device can be used as a compass.

## Example Code
https://github.com/adafruit/Adafruit_LIS2MDL/blob/master/examples/magsensor/magsensor.ino

# Adafruit LIS2MDL Triple Axis Magnetometer

## Arduino Docs

# Adafruit LIS2MDL Triple Axis Magnetometer

## Python & CircuitPython

It's easy to use the LIS2MDL sensor with CircuitPython and the [Adafruit CircuitPython LIS2MDL](https://github.com/adafruit/Adafruit_CircuitPython_LIS2MDL) library.&nbsp; This library will allow you to easily write Python code that reads the magnetometer values from the sensor.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python [thanks to Adafruit\_Blinka, our CircuitPython-for-Python compatibility library](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux).

# CircuitPython Microcontroller Wiring

First wire up a LIS2MDL breakout to your board exactly as shown below. Here's an example of wiring a Feather M4 to the sensor with I2C:

- **Board 3V** &nbsp;to&nbsp;**sensor VIN (red wire)**
- **Board GND** &nbsp;to&nbsp;**sensor GND (black wire)**
- **Board SCL** &nbsp;to&nbsp;**sensor SCL (yellow wire)**
- **Board SDA** &nbsp;to&nbsp;**sensor SDA (blue wire)**

![sensors_d_python_feather_wiring_qt.png](https://cdn-learn.adafruit.com/assets/assets/000/088/085/medium640/sensors_d_python_feather_wiring_qt.png?1580865816)

![sensors_c_arduino_wiring_bb.png](https://cdn-learn.adafruit.com/assets/assets/000/088/086/medium640/sensors_c_arduino_wiring_bb.png?1580865852)

# Python Computer Wiring

Since there's _dozens_ of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, [please visit the guide for CircuitPython on Linux to see whether your platform is supported](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux).&nbsp;

Here's the Raspberry Pi wired to the sensor using I2C:

- **Pi 3V** &nbsp;to&nbsp;**sensor VCC (red wire)**
- **Pi GND** &nbsp;to&nbsp;**sensor GND (black wire)**
- **Pi SCL** &nbsp;to&nbsp;**sensor SCL (yellow wire)**
- **Pi SDA** &nbsp;to&nbsp;**sensor SDA (blue wire)**

![sensors_d_python_rpi_wiring_qt.png](https://cdn-learn.adafruit.com/assets/assets/000/088/087/medium640/sensors_d_python_rpi_wiring_qt.png?1580865912)

![sensors_d_python_rpi_wiring_bb.png](https://cdn-learn.adafruit.com/assets/assets/000/088/088/medium640/sensors_d_python_rpi_wiring_bb.png?1580865952)

## CircuitPython Installation of LIS2MDL Library

You'll need to install the&nbsp;[Adafruit CircuitPython LIS2MDL](https://github.com/adafruit/Adafruit_CircuitPython_LIS2MDL) library on your CircuitPython board.

First make sure you are running the&nbsp;[latest version of Adafruit CircuitPython](https://learn.adafruit.com/welcome-to-circuitpython/installing-circuitpython)&nbsp;for your board.

Next you'll need to install the necessary libraries to use the hardware. Carefully follow the steps to find and install these libraries from [Adafruit's CircuitPython library bundle](https://circuitpython.org/libraries).&nbsp; Our CircuitPython starter guide has [a great page on how to install the library bundle](https://learn.adafruit.com/welcome-to-circuitpython/circuitpython-libraries).

For non-Express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from the bundle:

- **adafruit\_lis2mdl.mpy**
- **adafruit\_bus\_device**
- **adafruit\_register**

Before continuing make sure your board's **lib** folder has the **adafruit\_lis2mdl****.mpy, **** adafruit\_bus\_device **, and&nbsp;** adafruit\_register **files and folders** &nbsp;**copied over.

Next&nbsp;[connect to the board's serial REPL&nbsp;](https://learn.adafruit.com/welcome-to-circuitpython/the-repl)so you are at the CircuitPython&nbsp;`>>>`&nbsp;prompt.

## Python Installation of LIS2MDL Library

You'll need to install the **Adafruit\_Blinka** library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3.&nbsp;[Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux)!

Once that's done, from your command line, run the following command:

- `sudo pip3 install adafruit-circuitpython-lis2mdl`

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

## CircuitPython & Python Usage

To demonstrate the usage of the sensor, we'll initialize it and read the magnetometer measurements from the board's Python REPL.

Run the following code to import the necessary modules and initialize the I2C connection with the sensor:

```python
import time
import board
import adafruit_lis2mdl

i2c = board.I2C()
sensor = adafruit_lis2mdl.LIS2MDL(i2c)
```

Now you're ready to read values from the magnetometer using the **magnetic** property which returns a 3-tuple of the X, Y, and Z magnetometer readings in **micro-Teslas (uT)**

```python
mag_x, mag_y, mag_z = sensor.magnetic
print('X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT'.format(mag_x, mag_y, mag_z))
```

## Example Code
https://github.com/adafruit/Adafruit_CircuitPython_LIS2MDL/blob/main/examples/lis2mdl_simpletest.py

# Adafruit LIS2MDL Triple Axis Magnetometer

## Python Docs

# Adafruit LIS2MDL Triple Axis Magnetometer

## Downloads

## Files

- [LI23MDL Datasheet](https://www.st.com/resource/en/datasheet/lis2mdl.pdf)
- [EagleCAD files on GitHub](https://github.com/adafruit/Adafruit-LIS2MDL-PCB)
- [Fritzing object in Adafruit Fritzing Library](https://github.com/adafruit/Fritzing-Library/blob/master/parts/Adafruit%20LIS2MDL.fzpz)

## Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/088/076/medium800/sensors_zz_downloads_schematic.png?1580864519)

## Fab Print
![](https://cdn-learn.adafruit.com/assets/assets/000/088/075/medium800/sensors_zz_downloads_fab_print.png?1580864408)


## Primary Products

### Adafruit Triple-axis Magnetometer - LIS2MDL

[Adafruit Triple-axis Magnetometer - LIS2MDL](https://www.adafruit.com/product/4488)
Sense the magnetic fields that surround us with this handy triple-axis magnetometer (compass) module. Magnetometers can sense where the strongest magnetic force is coming from, generally used to detect magnetic north, but can also be used for measuring magnetic fields. This sensor tends to be...

In Stock
[Buy Now](https://www.adafruit.com/product/4488)
[Related Guides to the Product](https://learn.adafruit.com/products/4488/guides)

## Featured Products

### STEMMA QT / Qwiic JST SH 4-pin to Premium Male Headers Cable

[STEMMA QT / Qwiic JST SH 4-pin to Premium Male Headers Cable](https://www.adafruit.com/product/4209)
This 4-wire cable is a little over 150mm / 6" long and fitted with JST-SH female 4-pin connectors on one end and premium Dupont male headers on the other. Compared with the chunkier JST-PH these are 1mm pitch instead of 2mm, but still have a nice latching feel, while being easy to insert...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4209)
[Related Guides to the Product](https://learn.adafruit.com/products/4209/guides)
### JST SH 4-pin Cable with Alligator Clips - STEMMA QT / Qwiic

[JST SH 4-pin Cable with Alligator Clips - STEMMA QT / Qwiic](https://www.adafruit.com/product/4398)
This cable will make it super easy to use our plug-and-play STEMMA QT boards with boards like Circuit Playground. On one end you get a Qwiic / STEMMA QT connector (technically known as a JST SH 4-pin plug)&nbsp; into 4 individual wires with grippy alligator clips. We're carrying these to...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4398)
[Related Guides to the Product](https://learn.adafruit.com/products/4398/guides)
### STEMMA QT / Qwiic JST SH 4-pin Cable - 100mm Long

[STEMMA QT / Qwiic JST SH 4-pin Cable - 100mm Long](https://www.adafruit.com/product/4210)
This 4-wire cable is a little over 100mm / 4" long and fitted with JST-SH female 4-pin connectors on both ends. Compared with the chunkier JST-PH these are 1mm pitch instead of 2mm, but still have a nice latching feel, while being easy to insert and remove.

<a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/4210)
[Related Guides to the Product](https://learn.adafruit.com/products/4210/guides)
### STEMMA QT / Qwiic JST SH 4-pin Cable with Premium Female Sockets

[STEMMA QT / Qwiic JST SH 4-pin Cable with Premium Female Sockets](https://www.adafruit.com/product/4397)
This 4-wire cable is a little over 150mm / 6" long and fitted with JST-SH female 4-pin connectors on one end and premium female headers on the other. Compared with the chunkier JST-PH these are 1mm pitch instead of 2mm, but still have a nice latching feel, while being easy to insert and...

In Stock
[Buy Now](https://www.adafruit.com/product/4397)
[Related Guides to the Product](https://learn.adafruit.com/products/4397/guides)

## Related Guides

- [Adafruit TRRS Trinkey](https://learn.adafruit.com/adafruit-trrs-trinkey.md)
- [Send Raspberry Pi Data to COSM](https://learn.adafruit.com/send-raspberry-pi-data-to-cosm.md)
- [Adafruit QMC5883P - Triple Axis Magnetometer](https://learn.adafruit.com/adafruit-qmc5883p-triple-axis-magnetometer.md)
- [Adafruit 1.3" Color TFT Bonnet for Raspberry Pi](https://learn.adafruit.com/adafruit-1-3-color-tft-bonnet-for-raspberry-pi.md)
- [Adafruit TSC2046 SPI Resistive Touch Screen Controller](https://learn.adafruit.com/adafruit-tsc2046-spi-resistive-touch-screen-controller.md)
- [SMT Breadboard Prototyping Using Breakout PCBs](https://learn.adafruit.com/smt-prototyping-using-breakout-pcbs.md)
- [CircuitPython Powered Sip & Puff with ST LPS33HW Pressure Sensor](https://learn.adafruit.com/st-lps33-and-circuitpython-sip-and-puff.md)
- [Adafruit QT Py ESP32-C3 WiFi Dev Board](https://learn.adafruit.com/adafruit-qt-py-esp32-c3-wifi-dev-board.md)
- [ST 9-DoF Combo Breakouts and Wings](https://learn.adafruit.com/st-9-dof-combo.md)
- [Adafruit 10-DOF IMU Breakout](https://learn.adafruit.com/adafruit-10-dof-imu-breakout-lsm303-l3gd20-bmp180.md)
- [Motion Sensor Bat](https://learn.adafruit.com/motion-sensor-bat.md)
- [Adafruit I2C to 8 Channel Solenoid Driver](https://learn.adafruit.com/adafruit-i2c-to-8-channel-solenoid-driver.md)
- [Festival Finder Hat with WLED](https://learn.adafruit.com/festival-finder-hat-with-wled.md)
- [Adafruit ADXL375](https://learn.adafruit.com/adafruit-adxl375.md)
- [LIS3MDL Triple-axis Magnetometer](https://learn.adafruit.com/lis3mdl-triple-axis-magnetometer.md)
