Simple test¶
Ensure your device works with this simple test.
import time
from machine import Pin, I2C
from micropython_lis2mdl import lis2mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis2mdl.LIS2MDL(i2c)
while True:
mag_x, mag_y, mag_z = lis.magnetic
print(f"X:{mag_x:.2f}, Y:{mag_y:.2f}, Z:{mag_z:.2f} uT")
print()
time.sleep(1)
Operation mode settings¶
Example showing the Operation mode setting
import time
from machine import Pin, I2C
from micropython_lis2mdl import lis2mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis2mdl.LIS2MDL(i2c)
lis.operation_mode = lis2mdl.ONE_SHOT
while True:
for operation_mode in lis2mdl.operation_mode_values:
print("Current Operation mode setting: ", lis.operation_mode)
for _ in range(10):
mag_x, mag_y, mag_z = lis.magnetic
print(f"X:{mag_x:.2f}, Y:{mag_y:.2f}, Z:{mag_z:.2f} uT")
print()
time.sleep(0.5)
lis.operation_mode = operation_mode
Data rate settings¶
Example showing the Data rate setting
import time
from machine import Pin, I2C
from micropython_lis2mdl import lis2mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis2mdl.LIS2MDL(i2c)
lis.data_rate = lis2mdl.RATE_100_HZ
while True:
for data_rate in lis2mdl.data_rate_values:
print("Current Data rate setting: ", lis.data_rate)
for _ in range(10):
mag_x, mag_y, mag_z = lis.magnetic
print(f"X:{mag_x:.2f}, Y:{mag_y:.2f}, Z:{mag_z:.2f} uT")
print()
time.sleep(0.5)
lis.data_rate = data_rate
Low power mode settings¶
Example showing the Low power mode setting
import time
from machine import Pin, I2C
from micropython_lis2mdl import lis2mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis2mdl.LIS2MDL(i2c)
lis.low_power_mode = lis2mdl.LP_ENABLED
while True:
for low_power_mode in lis2mdl.low_power_mode_values:
print("Current Low power mode setting: ", lis.low_power_mode)
for _ in range(10):
magx, magy, magz = lis.magnetic
print(f"X:{magx:.2f}, Y:{magy:.2f}, Z:{magz:.2f} uT")
print()
time.sleep(0.5)
lis.low_power_mode = low_power_mode
Low pass filter mode settings¶
Example showing the Low pass filter mode setting
import time
from machine import Pin, I2C
from micropython_lis2mdl import lis2mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis2mdl.LIS2MDL(i2c)
lis.low_pass_filter_mode = lis2mdl.LPF_ENABLED
while True:
for low_pass_filter_mode in lis2mdl.low_pass_filter_mode_values:
print("Current Low pass filter mode setting: ", lis.low_pass_filter_mode)
for _ in range(10):
mag_x, mag_y, mag_z = lis.magnetic
print(f"X:{mag_x:.2f}, Y:{mag_y:.2f}, Z:{mag_z:.2f} uT")
print()
time.sleep(0.5)
lis.low_pass_filter_mode = low_pass_filter_mode
Interrupt mode settings¶
Example showing the Interrupt mode setting
import time
from machine import Pin, I2C
from micropython_lis2mdl import lis2mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis2mdl.LIS2MDL(i2c)
lis.interrupt_threshold = 80
lis.interrupt_mode = lis2mdl.INT_ENABLED
while True:
mag_x, mag_y, mag_z = lis.magnetic
print(f"X:{mag_x:.2f}, Y:{mag_y:.2f}, Z:{mag_z:.2f} uT")
print()
if lis.interrupt_triggered:
alert_status = lis.alert_status
if alert_status.x_high:
print("X axes above high set limit!")
if alert_status.x_low:
print("X axes below low set limit!")
time.sleep(0.5)