CircuitPython QMC5883L Library

qmc5883l

CircuitPython driver for the qmc5883l magnetometer

  • Author: Jose D. Montoya

Implementation Notes

class qmc5883l.QMC5883L(i2c_bus: busio.I2C, address: int = _I2C_ADDR)[source]

Driver for the QMC5883L magnetometer connected over I2C.

Parameters:
i2c_bus : I2C

The I2C bus the QMC5883L is connected to.

address : int

The I2C device address. Defaults to 0xD

Raises:

RuntimeError – if the sensor is not found

Quickstart: Importing and using the device

Here is an example of using the QMC5883L class. First you will need to import the libraries to use the sensor

import board
import qmc5883l

Once this is done you can define your board.I2C object and define your sensor object

i2c = board.I2C()  # uses board.SCL and board.SDA
qmc = qmc5883l.QMC5883L(i2c)

Now you have access to the magnetic attribute

mag_x, mag_y, mag_z = qmc.magnetic
property oversample : int

Over sample Rate (OSR) registers are used to control bandwidth of an internal digital filter. Larger OSR value leads to smaller filter bandwidth, less in-band noise and higher power consumption. It could be used to reach a good balance between noise and power.

Four oversample ratios can be selected, 64, 128, 256 or 512. With the following global variables.

Mode

Value

qmc5883l.OVERSAMPLE_64

0b11

qmc5883l.OVERSAMPLE_128

0b10

qmc5883l.OVERSAMPLE_256

0b01

qmc5883l.OVERSAMPLE_512

0b00

Example

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


qmc.oversample = qmc5883l.OVERSAMPLE_64
property field_range : str

Field ranges of the magnetic sensor can be selected through the register RNG. The full scale field range is determined by the application environments. For magnetic clear environment, low field range such as +/- 2gauss can be used. The field range goes hand in hand with the sensitivity of the magnetic sensor. The lowest field range has the highest sensitivity, therefore, higher resolution.

Two field range values can be selected, 2G and 8G. With the following global variables.

Mode

Value

qmc5883l.FIELDRANGE_2G

0b00

qmc5883l.FIELDRANGE_8G

0b01

Example

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


qmc.field_range = qmc5883l.FIELDRANGE_2G
property output_data_rate : str

Output data rate is controlled by ODR registers. Four data update frequencies can be selected: 10Hz, 50Hz, 100Hz and 200Hz. For most compassing applications, 10 Hz for low power consumption is recommended. For gaming, the high update rate such as 100Hz or 200Hz can be used.

Four oversample ratios can be selected, 10, 50, 100 or 200. With the following global variables.

Mode

Value

qmc5883l.OUTPUT_DATA_RATE_10

0b00

qmc5883l.OUTPUT_DATA_RATE_50

0b01

qmc5883l.OUTPUT_DATA_RATE_100

0b10

qmc5883l.OUTPUT_DATA_RATE_200

0b11

Example

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


qmc.output_data_rate = qmc5883l.OUTPUT_DATA_RATE_200
property mode_control : str

Two bits of MODE registers can transfer mode of operations in the device, the two modes are Standby, and Continuous measurements. The default mode after Power-on-Reset (POR) is standby. There is no any restriction in the transferring between the modes.

Two modes can be selected Standby and Continuous With the following global variables.

Mode

Value

qmc5883l.MODE_CONTINUOUS

0b01

qmc5883l.MODE_STANDBY

0b00

Example

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


qmc.output_data_rate = qmc5883l.MODE_STANDBY
property magnetic : tuple[float, float, float]

Magnetic property