Simple test

Ensure your device works with this simple test.

examples/qmc5883l_simpletest.py
import time
import board
import qmc5883l

i2c = board.I2C()
qmc = qmc5883l.QMC5883L(i2c)

while True:
    mag_x, mag_y, mag_z = qmc.magnetic
    print(f"x:{mag_x:.2f}Gs, y:{mag_y:.2f}Gs, z{mag_z:.2f}Gs")
    print()
    time.sleep(0.3)

Advanced Settings

Example showing how to set up the sensor with advanced settings

examples/qmc5883l_advanced_settings.py
import qmc5883l


i2c = board.I2C()
qmc = qmc5883l.QMC5883L(i2c)

print("Initial Configuration register", bin(qmc._conf_reg))
print("Setting Oversample to 64(0b11)")
qmc.oversample = qmc5883l.OVERSAMPLE_64
print("Configuration register", bin(qmc._conf_reg))
print("Setting Oversample to 128(0b10)")
qmc.oversample = qmc5883l.OVERSAMPLE_128
print("Configuration register", bin(qmc._conf_reg))
print("Setting Oversample to 2G (0b00)")
qmc.field_range = qmc5883l.FIELDRANGE_2G
print("Configuration register", bin(qmc._conf_reg))
print("Setting Range to 8G (0b01)")
qmc.field_range = qmc5883l.FIELDRANGE_8G
print("Configuration register", bin(qmc._conf_reg))
print("setting ouput Data Rate 100HZ (0b10)")
qmc.output_data_rate = qmc5883l.OUTPUT_DATA_RATE_100
print("Configuration register", bin(qmc._conf_reg))
print("setting ouput Data Rate 200HZ (0b11)")
qmc.output_data_rate = qmc5883l.OUTPUT_DATA_RATE_200
print("Configuration register", bin(qmc._conf_reg))
print("setting Mode to Continuous (0b01)")
qmc.mode_control = qmc5883l.MODE_CONTINUOUS
print("Configuration register", bin(qmc._conf_reg))
print("-" * 40)
print("Final Congifuration")
print("Field Range", qmc.field_range)
print("Oversample: ", qmc.oversample)
print("Output Data Rate: ", qmc.output_data_rate)
print("Mode: ", qmc.mode_control)

Oversample Settings

Example showing the oversample setting

examples/qmc5883l_oversample.py
import time
import board
import qmc5883l

i2c = board.I2C()
qmc = qmc5883l.QMC5883L(i2c)

while True:
    for oversample in qmc5883l.oversample_values:
        print("Current Oversample Setting: ", qmc.oversample)
        for _ in range(10):
            mag_x, mag_y, mag_z = qmc.magnetic
            print(f"x:{mag_x:.2f}Gs, y:{mag_y:.2f}Gs, z{mag_z:.2f}Gs")
            print()
            time.sleep(0.5)
        qmc.oversample = oversample

Data Rate Settings

Example showing the data rate setting

examples/qmc5883l_data_rate.py
import time
import board
import qmc5883l

i2c = board.I2C()
qmc = qmc5883l.QMC5883L(i2c)

while True:
    for data_rate in qmc5883l.data_rate_values:
        print("Current Data Rate Setting: ", qmc.output_data_rate)
        for _ in range(10):
            mag_x, mag_y, mag_z = qmc.magnetic
            print(f"x:{mag_x:.2f}Gs, y:{mag_y:.2f}Gs, z{mag_z:.2f}Gs")
            print()
            time.sleep(0.5)
        qmc.output_data_rate = data_rate

Field Range Settings

Example showing the field range setting

examples/qmc5883l_field_range.py
import time
import board
import qmc5883l

i2c = board.I2C()
qmc = qmc5883l.QMC5883L(i2c)

while True:
    for field_range in qmc5883l.field_range_values:
        print("Current Field Range Setting: ", qmc.field_range)
        for _ in range(10):
            mag_x, mag_y, mag_z = qmc.magnetic
            print(f"x:{mag_x:.2f}Gs, y:{mag_y:.2f}Gs, z{mag_z:.2f}Gs")
            print()
            time.sleep(0.5)
        qmc.field_range = field_range

Magnetic Compss

Example showing the field range setting

examples/qmc5883l_magnetic_compass.py
import time
from math import atan2, degrees
import board
import qmc5883l as qmc5883

i2c = board.I2C()
qmc = qmc5883.QMC5883L(i2c)


def vector_2_degrees(x, y):
    angle = degrees(atan2(y, x))
    if angle < 0:
        angle = angle + 360
    return angle


def get_heading(sensor):
    mag_x, mag_y, _ = sensor.magnetic
    return vector_2_degrees(mag_x, mag_y)


while True:
    print(f"heading: {get_heading(qmc.magnetic):.2f} degrees")
    print()
    time.sleep(0.2)