Simple tests

Ensure your device works with these simple tests.

examples/lis2mdl_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4""" Display magnetometer data once per second """
 5
 6import time
 7import board
 8import adafruit_lis2mdl
 9
10i2c = board.I2C()  # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
12sensor = adafruit_lis2mdl.LIS2MDL(i2c)
13
14while True:
15    mag_x, mag_y, mag_z = sensor.magnetic
16
17    print("X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(mag_x, mag_y, mag_z))
18    print("")
19    time.sleep(1.0)

Interrupt Example

Example showing how to use the interrupts

examples/lis2mdl_interrupt.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5import board
 6import adafruit_lis2mdl
 7
 8i2c = board.I2C()  # uses board.SCL and board.SDA
 9# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
10lis = adafruit_lis2mdl.LIS2MDL(i2c)
11lis.interrupt_threshold = 80
12lis.interrupt_enabled = True
13
14while True:
15    x_hi, y_hi, z_hi, x_lo, y_lo, z_lo, int_triggered = lis.faults
16
17    print(lis.magnetic)
18    print("Xhi:%s\tYhi:%s\tZhi:%s" % (x_hi, y_hi, z_hi))
19    print("Xlo:%s\tYlo:%s\tZlo:%s" % (x_lo, y_lo, z_lo))
20    print("Int triggered: %s" % int_triggered)
21    print()
22
23    time.sleep(1)

Compass Example

Example showing how to use the compass capabilities of the device

examples/lis2mdl_compass.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4""" Display compass heading data from a calibrated magnetometer """
 5
 6import time
 7import math
 8import board
 9import adafruit_lis2mdl
10
11i2c = board.I2C()  # uses board.SCL and board.SDA
12# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
13sensor = adafruit_lis2mdl.LIS2MDL(i2c)
14
15# You will need the calibration values from your magnetometer calibration
16# these values are in uT and are in X, Y, Z order (min and max values).
17#
18# To get these values run the lis2mdl_calibrate.py script on your device.
19# Twist the device around in 3D space while it calibrates. It will print
20# some calibration values like these:
21# ...
22# Calibrating - X:    -46.62, Y:    -22.33, Z:    -16.94 uT
23# ...
24# Calibration complete:
25# hardiron_calibration = [[-63.5487, 33.0313], [-40.5145, 53.8293], [-43.7153, 55.5101]]
26#
27# You need t copy your own value for hardiron_calibration from the output and paste it
28# into this script here:
29hardiron_calibration = [[-61.4879, 34.4782], [-43.6714, 53.5662], [-40.7337, 52.4554]]
30
31
32# This will take the magnetometer values, adjust them with the calibrations
33# and return a new array with the XYZ values ranging from -100 to 100
34def normalize(_magvals):
35    ret = [0, 0, 0]
36    for i, axis in enumerate(_magvals):
37        minv, maxv = hardiron_calibration[i]
38        axis = min(max(minv, axis), maxv)  # keep within min/max calibration
39        ret[i] = (axis - minv) * 200 / (maxv - minv) + -100
40    return ret
41
42
43while True:
44    magvals = sensor.magnetic
45    normvals = normalize(magvals)
46    print("magnetometer: %s -> %s" % (magvals, normvals))
47
48    # we will only use X and Y for the compass calculations, so hold it level!
49    compass_heading = int(math.atan2(normvals[1], normvals[0]) * 180.0 / math.pi)
50    # compass_heading is between -180 and +180 since atan2 returns -pi to +pi
51    # this translates it to be between 0 and 360
52    compass_heading += 180
53
54    print("Heading:", compass_heading)
55    time.sleep(0.1)

Calibrate Test

Calibrate the magnetometer and print out the hard-iron calibrations

examples/lis2mdl_calibrate.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4""" Calibrate the magnetometer and print out the hard-iron calibrations """
 5
 6import time
 7import board
 8import adafruit_lis2mdl
 9
10i2c = board.I2C()  # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
12magnetometer = adafruit_lis2mdl.LIS2MDL(i2c)
13
14# calibration for magnetometer X (min, max), Y and Z
15hardiron_calibration = [[1000, -1000], [1000, -1000], [1000, -1000]]
16
17
18def calibrate():
19    start_time = time.monotonic()
20
21    # Update the high and low extremes
22    while time.monotonic() - start_time < 10.0:
23        magval = magnetometer.magnetic
24        print("Calibrating - X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(*magval))
25        for i, axis in enumerate(magval):
26            hardiron_calibration[i][0] = min(hardiron_calibration[i][0], axis)
27            hardiron_calibration[i][1] = max(hardiron_calibration[i][1], axis)
28    print("Calibration complete:")
29    print("hardiron_calibration =", hardiron_calibration)
30
31
32print("Prepare to calibrate! Twist the magnetometer around in 3D in...")
33print("3...")
34time.sleep(1)
35print("2...")
36time.sleep(1)
37print("1...")
38time.sleep(1)
39
40calibrate()