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