You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a MicroPython driver for the LIS2MDL 3-axis magnetometer. The LIS2MDL is a high-performance magnetic sensor designed for applications such as electronic compasses, motion tracking, and orientation detection. This driver provides methods for initialization, calibration, and reading magnetic field data.
4
+
5
+
## Features
6
+
- Soft reset and initialization of the LIS2MDL sensor.
7
+
- Configurable output data rate (ODR) and low-power mode.
8
+
- 3D calibration for offsets and scales.
9
+
- Heading calculation with and without tilt compensation.
10
+
- Direction labeling (e.g., N, NE, E, etc.).
11
+
12
+
## Requirements
13
+
- MicroPython-compatible board with I2C support.
14
+
- LIS2MDL sensor module.
15
+
16
+
## Installation
17
+
1. Copy the `lis2mdl` folder to your MicroPython device.
18
+
2. Import the driver in your script:
19
+
```python
20
+
from lis2mdl.device importLIS2MDL
21
+
```
22
+
23
+
## Usage
24
+
### Basic Example
25
+
```python
26
+
from time import sleep_ms
27
+
from machine importI2C
28
+
from lis2mdl.device importLIS2MDL
29
+
30
+
# Initialize I2C and LIS2MDL
31
+
i2c = I2C(1)
32
+
mag = LIS2MDL(i2c)
33
+
34
+
# Perform calibration
35
+
mag.calibrate_step()
36
+
calibration_values = mag.get_calibration()
37
+
print("Calibration values:", calibration_values)
38
+
39
+
# Continuous heading reading
40
+
print("Continuous heading readings:")
41
+
whileTrue:
42
+
angle = mag.heading_flat_only()
43
+
direction = mag.direction_label(angle)
44
+
print(f"{direction} | angle={angle:.2f}°")
45
+
sleep_ms(100)
46
+
```
47
+
48
+
### Tilt Compensation Example
49
+
To use tilt compensation, you need an accelerometer to provide roll and pitch data. Pass a function that reads accelerometer data to the `heading_with_tilt_compensation` method.
The `calibrate_step` method performs a quick 3D calibration. Rotate the sensor flat over 360° to capture the minimum and maximum magnetic field values. The offsets and scales are calculated automatically.
62
+
63
+
## Datasheet
64
+
For detailed information about the LIS2MDL sensor, refer to the [datasheet](https://www.st.com/resource/en/datasheet/lis2mdl.pdf).
65
+
66
+
## Notes
67
+
- The `heading_with_tilt_compensation` method has not been tested due to the lack of an accelerometer during development.
68
+
- Ensure the I2C pins are correctly connected to the LIS2MDL module.
0 commit comments