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
7import board
8import adafruit_lis3mdl
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_lis3mdl.LIS3MDL(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)
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 """
5import time
6from math import atan2, degrees
7import board
8import adafruit_lis3mdl
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_lis3mdl.LIS3MDL(i2c)
13
14
15def vector_2_degrees(x, y):
16 angle = degrees(atan2(y, x))
17 if angle < 0:
18 angle += 360
19 return angle
20
21
22def get_heading(_sensor):
23 magnet_x, magnet_y, _ = _sensor.magnetic
24 return vector_2_degrees(magnet_x, magnet_y)
25
26
27while True:
28 print("heading: {:.2f} degrees".format(get_heading(sensor)))
29 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
6# pylint: disable=no-member
7import time
8import board
9from adafruit_lis3mdl import LIS3MDL, Rate, PerformanceMode
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 = LIS3MDL(i2c)
14
15current_rate = Rate.RATE_155_HZ
16sensor.data_rate = current_rate
17start_time = time.monotonic()
18print("data_rate is", Rate.string[sensor.data_rate], "HZ")
19print("performance_mode is", PerformanceMode.string[sensor.performance_mode])
20while True:
21 mag_x, mag_y, mag_z = sensor.magnetic
22
23 print("X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(mag_x, mag_y, mag_z))
24
25 # sleep for enough time so that we'll read the value twice per measurement
26 sleep_time = 1 / (Rate.string[current_rate] * 2)
27 time.sleep(sleep_time)
28
29 # exit loop after a second to prevent hard to stop loops with short delays
30 if (time.monotonic() - start_time) > 1:
31 break
LSM6DS Test¶
Test the LSM6DS device
examples/lis3mdl_lsm6ds_test.py¶
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6from adafruit_lsm6ds.lsm6dsox import LSM6DSOX as LSM6DS
7
8# To use LSM6DS33, comment out the LSM6DSOX import line
9# and uncomment the next line
10# from adafruit_lsm6ds.lsm6ds33 import LSM6DS33 as LSM6DS
11
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
16# To use LSM6DS3TR-C, comment out the LSM6DSOX import line
17# and uncomment the next line
18# from adafruit_lsm6ds.lsm6ds3 import LSM6DS3 as LSM6DS
19
20from adafruit_lis3mdl import LIS3MDL
21
22i2c = board.I2C() # uses board.SCL and board.SDA
23# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
24accel_gyro = LSM6DS(i2c)
25mag = LIS3MDL(i2c)
26
27while True:
28 acceleration = accel_gyro.acceleration
29 gyro = accel_gyro.gyro
30 magnetic = mag.magnetic
31 print(
32 "Acceleration: X:{0:7.2f}, Y:{1:7.2f}, Z:{2:7.2f} m/s^2".format(*acceleration)
33 )
34 print("Gyro X:{0:7.2f}, Y:{1:7.2f}, Z:{2:7.2f} rad/s".format(*gyro))
35 print("Magnetic X:{0:7.2f}, Y:{1:7.2f}, Z:{2:7.2f} uT".format(*magnetic))
36 print("")
37 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# pylint: disable=no-member
6import time
7import board
8from adafruit_lis3mdl import LIS3MDL, Range
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 = LIS3MDL(i2c)
13
14while True:
15
16 for mag_range in [
17 Range.RANGE_4_GAUSS,
18 Range.RANGE_8_GAUSS,
19 Range.RANGE_12_GAUSS,
20 Range.RANGE_16_GAUSS,
21 ]:
22 sensor.range = mag_range
23 print("Range: %d Gauss" % Range.string[sensor.range])
24 mag_x, mag_y, mag_z = sensor.magnetic
25
26 print("X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(mag_x, mag_y, mag_z))
27 print("")
28 time.sleep(0.3)