Simple test

Ensure your device works with this simple test.

examples/lis3mdl_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
 7
 8import board
 9
10import adafruit_lis3mdl
11
12i2c = board.I2C()  # uses board.SCL and board.SDA
13# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
14sensor = adafruit_lis3mdl.LIS3MDL(i2c)
15
16while True:
17    mag_x, mag_y, mag_z = sensor.magnetic
18
19    print(f"X:{mag_x:10.2f}, Y:{mag_y:10.2f}, Z:{mag_z:10.2f} uT")
20    print("")
21    time.sleep(1.0)

Compass Example

Use the magnetometer to calculate compass headings.

examples/lis3mdl_compass.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""Display compass heading data five times per second"""
 5
 6import time
 7from math import atan2, degrees
 8
 9import board
10
11import adafruit_lis3mdl
12
13i2c = board.I2C()  # uses board.SCL and board.SDA
14# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
15sensor = adafruit_lis3mdl.LIS3MDL(i2c)
16
17
18def vector_2_degrees(x, y):
19    angle = degrees(atan2(y, x))
20    if angle < 0:
21        angle += 360
22    return angle
23
24
25def get_heading(_sensor):
26    magnet_x, magnet_y, _ = _sensor.magnetic
27    return vector_2_degrees(magnet_x, magnet_y)
28
29
30while True:
31    print(f"heading: {get_heading(sensor):.2f} degrees")
32    time.sleep(0.2)

Data Rate Example

Test each data rate

examples/lis3mdl_data_rate_test.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""Test Each Data Rate"""
 5
 6import time
 7
 8import board
 9
10from adafruit_lis3mdl import LIS3MDL, PerformanceMode, Rate
11
12i2c = board.I2C()  # uses board.SCL and board.SDA
13# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
14sensor = LIS3MDL(i2c)
15
16current_rate = Rate.RATE_155_HZ
17sensor.data_rate = current_rate
18start_time = time.monotonic()
19print("data_rate is", Rate.string[sensor.data_rate], "HZ")
20print("performance_mode is", PerformanceMode.string[sensor.performance_mode])
21while True:
22    mag_x, mag_y, mag_z = sensor.magnetic
23
24    print(f"X:{mag_x:10.2f}, Y:{mag_y:10.2f}, Z:{mag_z:10.2f} uT")
25
26    # sleep for enough time so that we'll read the value twice per measurement
27    sleep_time = 1 / (Rate.string[current_rate] * 2)
28    time.sleep(sleep_time)
29
30    # exit loop after a second to prevent hard to stop loops with short delays
31    if (time.monotonic() - start_time) > 1:
32        break

LSM6DS Test

Test the LSM6DS device

examples/lis3mdl_lsm6ds3trc_test.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5
 6import board
 7from adafruit_lsm6ds.lsm6dsox import LSM6DSOX as LSM6DS
 8
 9# To use LSM6DS33, comment out the LSM6DSOX import line
10# and uncomment the next line
11# from adafruit_lsm6ds.lsm6ds33 import LSM6DS33 as LSM6DS
12# To use ISM330DHCX, comment out the LSM6DSOX import line
13# and uncomment the next line
14# from adafruit_lsm6ds.lsm330dhcx import ISM330DHCX as LSM6DS
15# To use LSM6DS3TR-C, comment out the LSM6DSOX import line
16# and uncomment the next line
17# from adafruit_lsm6ds.lsm6ds3trc import LSM6DS3TRC as LSM6DS
18from adafruit_lis3mdl import LIS3MDL
19
20i2c = board.I2C()  # uses board.SCL and board.SDA
21# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
22accel_gyro = LSM6DS(i2c)
23mag = LIS3MDL(i2c)
24
25while True:
26    acceleration = accel_gyro.acceleration
27    gyro = accel_gyro.gyro
28    magnetic = mag.magnetic
29    print("Acceleration: X:{0:7.2f}, Y:{1:7.2f}, Z:{2:7.2f} m/s^2".format(*acceleration))
30    print("Gyro          X:{0:7.2f}, Y:{1:7.2f}, Z:{2:7.2f} rad/s".format(*gyro))
31    print("Magnetic      X:{0:7.2f}, Y:{1:7.2f}, Z:{2:7.2f} uT".format(*magnetic))
32    print("")
33    time.sleep(0.5)

Range Test

Test each range

examples/lis3mdl_range_test.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""Test Each range"""
 5
 6import time
 7
 8import board
 9
10from adafruit_lis3mdl import LIS3MDL, Range
11
12i2c = board.I2C()  # uses board.SCL and board.SDA
13# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
14sensor = LIS3MDL(i2c)
15
16while True:
17    for mag_range in [
18        Range.RANGE_4_GAUSS,
19        Range.RANGE_8_GAUSS,
20        Range.RANGE_12_GAUSS,
21        Range.RANGE_16_GAUSS,
22    ]:
23        sensor.range = mag_range
24        print(f"Range: {Range.string[sensor.range]:d} Gauss")
25        mag_x, mag_y, mag_z = sensor.magnetic
26
27        print(f"X:{mag_x:10.2f}, Y:{mag_y:10.2f}, Z:{mag_z:10.2f} uT")
28        print("")
29        time.sleep(0.3)