Simple test¶
Ensure your device works with this simple test.
import time
from machine import Pin, I2C
from micropython_lis3mdl import lis3mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis3mdl.LIS3MDL(i2c)
while True:
mag_x, mag_y, mag_z = lis.magnetic
print(f"X:{mag_x:0.2f}, Y:{mag_y:0.2f}, Z:{mag_z:0.2f} uT")
print("")
time.sleep(0.5)
Data rate settings¶
Example showing the Data rate setting
import time
from machine import Pin, I2C
from micropython_lis3mdl import lis3mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis3mdl.LIS3MDL(i2c)
lis.data_rate = lis3mdl.RATE_300_HZ
while True:
for data_rate in lis3mdl.data_rate_values:
print("Current Data rate setting: ", lis.data_rate)
for _ in range(10):
magx, magy, magz = lis.magnetic
print(f"X:{magx:0.2f}, Y:{magy:0.2f}, Z:{magz:0.2f} uT")
print()
time.sleep(0.5)
lis.data_rate = data_rate
Scale range settings¶
Example showing the Scale range setting
import time
from machine import Pin, I2C
from micropython_lis3mdl import lis3mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis3mdl.LIS3MDL(i2c)
lis.scale_range = lis3mdl.SCALE_16_GAUSS
while True:
for scale_range in lis3mdl.scale_range_values:
print("Current Scale range setting: ", lis.scale_range)
for _ in range(10):
magx, magy, magz = lis.magnetic
print(f"X:{magx:0.2f}, Y:{magy:0.2f}, Z:{magz:0.2f} uT")
print()
time.sleep(0.5)
lis.scale_range = scale_range
Low power mode settings¶
Example showing the Low power mode setting
import time
from machine import Pin, I2C
from micropython_lis3mdl import lis3mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis3mdl.LIS3MDL(i2c)
lis.low_power_mode = lis3mdl.LP_DISABLED
while True:
for low_power_mode in lis3mdl.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:0.2f}, Y:{magy:0.2f}, Z:{magz:0.2f} uT")
print()
time.sleep(0.5)
lis.low_power_mode = low_power_mode
Operation mode settings¶
Example showing the Operation mode setting
import time
from machine import Pin, I2C
from micropython_lis3mdl import lis3mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis3mdl.LIS3MDL(i2c)
lis.operation_mode = lis3mdl.POWER_DOWN
while True:
for operation_mode in lis3mdl.operation_mode_values:
print("Current Operation mode setting: ", lis.operation_mode)
for _ in range(10):
magx, magy, magz = lis.magnetic
print(f"X:{magx:0.2f}, Y:{magy:0.2f}, Z:{magz:0.2f} uT")
print()
time.sleep(0.5)
lis.operation_mode = operation_mode
Compass Example¶
Example showing how to use the LIS3MDL as a compass
import time
from math import atan2, degrees
from machine import Pin, I2C
from micropython_lis3mdl import lis3mdl
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
lis = lis3mdl.LIS3MDL(i2c)
def vector_2_degrees(x, y):
angle = degrees(atan2(y, x))
if angle < 0:
angle += 360
return angle
def get_heading(_sensor):
magnet_x, magnet_y, _ = _sensor.magnetic
return vector_2_degrees(magnet_x, magnet_y)
while True:
print(f"heading: {get_heading(lis):.2f} degrees")
time.sleep(0.2)