adafruit / adafruit/Adafruit_CircuitPython_QMC5883P

No calibration procedure for the sensor

Open Beginner friendly
#3 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2
Forks
0
PR merge metrics
No merged PRs in 30d

Description

A magnetometer must be calibrated when it is first used.
I would recommend including this procedure for others in the example section of the code.

```
import time
import board
import adafruit_qmc5883p
import numpy as np

from micropython import const

i2c = board.I2C()

sensor = adafruit_qmc5883p.QMC5883P(i2c)

minX = 100
minY = 100
minZ = 100
maxX = 0
maxY = 0
maxZ = 0

RANGE_30G = const(0x00)
RANGE_12G = const(0x01)
RANGE_8G = const(0x02)
RANGE_2G = const(0x03)

_LSB_PER_GAUSS = {RANGE_30G: 1000.0, RANGE_12G: 2500.0, RANGE_8G: 3750.0, RANGE_2G: 15000.0}

curr_time = time.time()

while time.time() < curr_time + 30:
raw_mag_x, raw_mag_y, raw_mag_z = sensor.magnetic_raw
lsb_per_gauss = _LSB_PER_GAUSS[sensor._range]

mag_x = raw_mag_x / lsb_per_gauss
mag_y = raw_mag_y / lsb_per_gauss
mag_z = raw_mag_z / lsb_per_gauss

minX = np.min([mag_x, minX])
minY = np.min([mag_y, minY])
minZ = np.min([mag_z, minZ])
maxX = np.max([mag_x, maxX])
maxY = np.max([mag_y, maxY])
maxZ = np.max([mag_z, maxZ])

time.sleep(0.1)

offX = (maxX + minX) / 2.0
offY = (maxY + minY) / 2.0
offZ = (maxZ + minZ) / 2.0
scaleX = (maxX - minX) / 2.0
scaleY = (maxY - minY) / 2.0
scaleZ = (maxZ - minZ) / 2.0

print(f'offX = {offX}')
print(f'offY = {offY}')
print(f'offZ = {offZ}')

print(f'scaleX = {scaleX}')
print(f'scaleY = {scaleY}')
print(f'scaleZ = {scaleZ}')

while True:
raw_mag_x, raw_mag_y, raw_mag_z = sensor.magnetic_raw
lsb_per_gauss = _LSB_PER_GAUSS[sensor._range]

mag_x = raw_mag_x / lsb_per_gauss
mag_y = raw_mag_y / lsb_per_gauss
mag_z = raw_mag_z / lsb_per_gauss

mag_x = (mag_x - offX) / scaleX
mag_y = (mag_y - offY) / scaleY
mag_z = (mag_z - offZ) / scaleZ

print(f"X:{mag_x:2.3f}, Y:{mag_y:2.3f}, Z:{mag_z:2.3f}, NORM:{np.linalg.norm([mag_x, mag_y, mag_z]):2.3f} G")

time.sleep(1)
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the repository's example section and the sensor usage shown in the issue. Add the calibration procedure there, using the provided example as the reference, and consider the work done when users can follow the example to calibrate the magnetometer and see corrected readings.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
embedded-iot
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.