Simple test
Ensure your device works with this simple test.
examples/qmc5883l_simpletest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | # SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT
import time
import board
import qmc5883l
i2c = board.I2C()
qmc = qmc5883l.QMC5883L(i2c)
while True:
mag_x, mag_y, mag_z = qmc.magnetic
print("x:{:.2f}Gs, y:{:.2f}Gs, z{:.2f}Gs".format(mag_x, mag_y, mag_z))
time.sleep(0.3)
|
Advanced Settings
Example showing how to set up the sensor with advanced settings
examples/qmc5883l_advanced_settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 | # SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT
# pylint: disable=protected-access
import board
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | # SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT
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("x:{:.2f}Gs, y:{:.2f}Gs, z{:.2f}Gs".format(mag_x, mag_y, mag_z))
time.sleep(0.5)
qmc.oversample = oversample
|
Data Rate Settings
Example showing the data rate setting
examples/qmc5883l_data_rate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | # SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT
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("x:{:.2f}Gs, y:{:.2f}Gs, z{:.2f}Gs".format(mag_x, mag_y, mag_z))
time.sleep(0.5)
qmc.output_data_rate = data_rate
|
Field Range Settings
Example showing the field range setting
examples/qmc5883l_field_range.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | # SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT
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("x:{:.2f}Gs, y:{:.2f}Gs, z{:.2f}Gs".format(mag_x, mag_y, mag_z))
time.sleep(0.5)
qmc.field_range = field_range
|
Magnetic Compss
Example showing the field range setting
examples/qmc5883l_magnetic_compass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 | # SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT
""" Based in example found in
https://github.com/adafruit/Adafruit_CircuitPython_LSM303DLH_Mag/blob/main/examples/lsm303dlh_mag_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("heading: {:.2f} degrees".format(get_heading(qmc)))
time.sleep(0.2)
|