Home Circuitpython Adafruit Feather M0 and LIS3MDL 3-axis MEMS magnetic field sensor circuitpython example

Adafruit Feather M0 and LIS3MDL 3-axis MEMS magnetic field sensor circuitpython example

by shedboy71

In this example we connect a LIS3MDL 3-axis MEMS magnetic field sensor to an Adafruit Feather M0 running Circuitpython

First lets look at some information about the sensor from the manufacturer

Description

Lets see what the sensor manufacturer says about it

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

This is the sensor that I bought

Parts Required

 

Name Link
Adafruit Feather M0 Express Adafruit (PID 3403) Feather M0 Express – Designed for CircuitPython – ATSAMD21 Cortex M0
lis3mdl CJMCU-LIS3MDL high precision 3 axis magnetometer sensor compass module
Connecting cables Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Schematic/Connection

feather and lis3mdl layout

feather and lis3mdl layout

Code Example

I used Mu for development

The following is based on a library , I copied the adafruit_lis3mdl.mpy library for this device to the lib folder on my Feather M0 Express – https://circuitpython.org/libraries

My sensor had an I2C address of 0x1e

[codesyntax lang=”python”]

""" Display magnetometer data once per second """

import time
import board
import busio
import adafruit_lis3mdl

i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_lis3mdl.LIS3MDL(i2c,address=0x1e)

while True:
    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))
    print("")
    time.sleep(1.0)

[/codesyntax]

Output

Here is what I saw in Mu REPL window

X: -0.79, Y: -39.20, Z: 66.22 uT

X: -1.20, Y: -38.82, Z: 66.60 uT

X: -0.88, Y: -39.93, Z: 66.59 uT

X: -0.88, Y: -39.93, Z: 66.59 uT

X: -0.88, Y: -39.93, Z: 66.59 uT

X: -0.88, Y: -39.93, Z: 66.59 uT

X: -0.88, Y: -39.93, Z: 66.59 uT

X: -0.88, Y: -39.93, Z: 66.59 uT

Links

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

You may also like

Leave a Comment