# Simple test¶

Ensure your device works with this simple test.

examples/lis3mdl_simpletest.py
``` 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
3
4""" Display magnetometer data once per second """
5
6import time
7import board
9
10i2c = board.I2C()  # uses board.SCL and board.SDA
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)
```

# Compass Example¶

Use the magnetometer to calculate compass headings.

examples/lis3mdl_compass.py
``` 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
3
4""" Display compass heading data five times per second """
5import time
6from math import atan2, degrees
7import board
9
10i2c = board.I2C()  # uses board.SCL and board.SDA
12
13
14def vector_2_degrees(x, y):
15    angle = degrees(atan2(y, x))
16    if angle < 0:
17        angle += 360
18    return angle
19
20
22    magnet_x, magnet_y, _ = _sensor.magnetic
23    return vector_2_degrees(magnet_x, magnet_y)
24
25
26while True:
28    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
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
12sensor = LIS3MDL(i2c)
13
14current_rate = Rate.RATE_155_HZ
15sensor.data_rate = current_rate
16start_time = time.monotonic()
17print("data_rate is", Rate.string[sensor.data_rate], "HZ")
18print("performance_mode is", PerformanceMode.string[sensor.performance_mode])
19while True:
20    mag_x, mag_y, mag_z = sensor.magnetic
21
22    print("X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(mag_x, mag_y, mag_z))
23
24    # sleep for enough time so that we'll read the value twice per measurement
25    sleep_time = 1 / (Rate.string[current_rate] * 2)
26    time.sleep(sleep_time)
27
28    # exit loop after a second to prevent hard to stop loops with short delays
29    if (time.monotonic() - start_time) > 1:
30        break
```

# LSM6DS Test¶

Test the LSM6DS device

examples/lis3mdl_lsm6ds_test.py
``` 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
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
17
18i2c = board.I2C()  # uses board.SCL and board.SDA
19accel_gyro = LSM6DS(i2c)
20mag = LIS3MDL(i2c)
21
22while True:
23    acceleration = accel_gyro.acceleration
24    gyro = accel_gyro.gyro
25    magnetic = mag.magnetic
26    print(
27        "Acceleration: X:{0:7.2f}, Y:{1:7.2f}, Z:{2:7.2f} m/s^2".format(*acceleration)
28    )
29    print("Gyro          X:{0:7.2f}, Y:{1:7.2f}, Z:{2:7.2f} rad/s".format(*gyro))
30    print("Magnetic      X:{0:7.2f}, Y:{1:7.2f}, Z:{2:7.2f} uT".format(*magnetic))
31    print("")
32    time.sleep(0.5)
```

# Range Test¶

Test each range

examples/lis3mdl_range_test.py
``` 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
3
4""" Test Each range """
5# pylint: disable=no-member
6import time
7import board
9
10i2c = board.I2C()  # uses board.SCL and board.SDA
11sensor = LIS3MDL(i2c)
12
13while True:
14
15    for mag_range in [
16        Range.RANGE_4_GAUSS,
17        Range.RANGE_8_GAUSS,
18        Range.RANGE_12_GAUSS,
19        Range.RANGE_16_GAUSS,
20    ]:
21        sensor.range = mag_range
22        print("Range: %d Gauss" % Range.string[sensor.range])
23        mag_x, mag_y, mag_z = sensor.magnetic
24
25        print("X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(mag_x, mag_y, mag_z))
26        print("")
27        time.sleep(0.3)
```