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
11sensor = adafruit_lis2mdl.LIS2MDL(i2c)
12
13while True:
14 mag_x, mag_y, mag_z = sensor.magnetic
15
16 print("X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(mag_x, mag_y, mag_z))
17 print("")
18 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
9lis = adafruit_lis2mdl.LIS2MDL(i2c)
10lis.interrupt_threshold = 80
11lis.interrupt_enabled = True
12
13while True:
14 x_hi, y_hi, z_hi, x_lo, y_lo, z_lo, int_triggered = lis.faults
15
16 print(lis.magnetic)
17 print("Xhi:%s\tYhi:%s\tZhi:%s" % (x_hi, y_hi, z_hi))
18 print("Xlo:%s\tYlo:%s\tZlo:%s" % (x_lo, y_lo, z_lo))
19 print("Int triggered: %s" % int_triggered)
20 print()
21
22 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
12sensor = adafruit_lis2mdl.LIS2MDL(i2c)
13
14# You will need the calibration values from your magnetometer calibration
15# these values are in uT and are in X, Y, Z order (min and max values).
16#
17# To get these values run the lis2mdl_calibrate.py script on your device.
18# Twist the device around in 3D space while it calibrates. It will print
19# some calibration values like these:
20# ...
21# Calibrating - X: -46.62, Y: -22.33, Z: -16.94 uT
22# ...
23# Calibration complete:
24# hardiron_calibration = [[-63.5487, 33.0313], [-40.5145, 53.8293], [-43.7153, 55.5101]]
25#
26# You need t copy your own value for hardiron_calibration from the output and paste it
27# into this script here:
28hardiron_calibration = [[-61.4879, 34.4782], [-43.6714, 53.5662], [-40.7337, 52.4554]]
29
30
31# This will take the magnetometer values, adjust them with the calibrations
32# and return a new array with the XYZ values ranging from -100 to 100
33def normalize(_magvals):
34 ret = [0, 0, 0]
35 for i, axis in enumerate(_magvals):
36 minv, maxv = hardiron_calibration[i]
37 axis = min(max(minv, axis), maxv) # keep within min/max calibration
38 ret[i] = (axis - minv) * 200 / (maxv - minv) + -100
39 return ret
40
41
42while True:
43 magvals = sensor.magnetic
44 normvals = normalize(magvals)
45 print("magnetometer: %s -> %s" % (magvals, normvals))
46
47 # we will only use X and Y for the compass calculations, so hold it level!
48 compass_heading = int(math.atan2(normvals[1], normvals[0]) * 180.0 / math.pi)
49 # compass_heading is between -180 and +180 since atan2 returns -pi to +pi
50 # this translates it to be between 0 and 360
51 compass_heading += 180
52
53 print("Heading:", compass_heading)
54 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
11magnetometer = adafruit_lis2mdl.LIS2MDL(i2c)
12
13# calibration for magnetometer X (min, max), Y and Z
14hardiron_calibration = [[1000, -1000], [1000, -1000], [1000, -1000]]
15
16
17def calibrate():
18 start_time = time.monotonic()
19
20 # Update the high and low extremes
21 while time.monotonic() - start_time < 10.0:
22 magval = magnetometer.magnetic
23 print("Calibrating - X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(*magval))
24 for i, axis in enumerate(magval):
25 hardiron_calibration[i][0] = min(hardiron_calibration[i][0], axis)
26 hardiron_calibration[i][1] = max(hardiron_calibration[i][1], axis)
27 print("Calibration complete:")
28 print("hardiron_calibration =", hardiron_calibration)
29
30
31print("Prepare to calibrate! Twist the magnetometer around in 3D in...")
32print("3...")
33time.sleep(1)
34print("2...")
35time.sleep(1)
36print("1...")
37time.sleep(1)
38
39calibrate()