A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.circuitpython.org/projects/lis2mdl/en/latest/examples.html below:

Website Navigation


Simple tests — Adafruit LIS2MDL 3-axis Magnetometer Library 1.0 documentation

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

This is a simple test for boards with built-in display.

examples/lis2mdl_displayio_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
 2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
 3#
 4# SPDX-License-Identifier: MIT
 5
 6import time
 7
 8import board
 9from adafruit_display_text.bitmap_label import Label
10from displayio import Group
11from terminalio import FONT
12
13import adafruit_lis2mdl
14
15# Simple demo of using the built-in display.
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C()  # uses board.SCL and board.SDA
20sensor = adafruit_lis2mdl.LIS2MDL(i2c)
21
22# Create Label(s) to show the readings. If you have a very small
23# display you may need to change to scale=1.
24display_output_label = Label(FONT, text="", scale=2)
25
26# place the label(s) in the middle of the screen with anchored positioning
27display_output_label.anchor_point = (0, 0)
28display_output_label.anchored_position = (
29    4,
30    board.DISPLAY.height // 2 - 60,
31)
32
33# add the label(s) to the main_group
34main_group.append(display_output_label)
35
36# set the main_group as the root_group of the built-in DISPLAY
37board.DISPLAY.root_group = main_group
38
39# begin main loop
40while True:
41    # update the text of the label(s) to show the sensor readings
42    mag_x, mag_y, mag_z = sensor.magnetic
43    display_output_label.text = f"X:{mag_x:10.2f}, Y:{mag_y:10.2f}, Z:{mag_z:10.2f} uT"
44    # wait for a bit
45    time.sleep(0.5)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4