I2C library for the AK8963 3-axis electronic compass.
Reads the AK8963 magnetometer over I2C, applies the per-axis sensitivity adjustment stored in the device's fuse ROM, and reports the field in microtesla.
Archived and unmaintained. The AK8963 is end-of-life, and so is the part it mostly shipped inside - the InvenSense MPU-9250/9255, whose successor (ICM-20948) uses a different magnetometer (AK09916) with a different register map. This repository is kept as a record of the work, not as a library to build new designs on.
For new designs, use a maintained MPU-9250 library such as hideakitai/MPU9250 or bolderflight/MPU9250, or a current magnetometer (IST8310, QMC5883L, LIS3MDL, BMM150, RM3100).
- Measurements in microtesla, or in counts with the fuse ROM sensitivity adjustment applied
- Single measurement, continuous 8Hz and continuous 100Hz modes
- 14-bit and 16-bit resolution
- Magnetic overflow (
HOFL) detection - Built-in self-test, with the datasheet's pass/fail bounds
- Soft reset, power down, and I2C disable
- AK8963 (standalone, or inside an MPU-9250/9255)
- A microcontroller with I2C, using the Arduino
Wirelibrary
#include <Wire.h> #include "AK8963.hpp" AK8963 compass; void setup() { Serial.begin(115200); Wire.begin(); if (!compass.initialize()) { Serial.println("AK8963 initialization failed."); while (true) {} } } void loop() { float x, y, z; if (compass.read(&x, &y, &z)) { // microtesla Serial.print(x); Serial.print('\t'); Serial.print(y); Serial.print('\t'); Serial.println(z); } delay(20); }
See examples/Basic for the complete sketch.
Reaching the device inside an MPU-9250/9255. The MPU-92xx is a two-die
package: an MPU-6500-class accelerometer/gyroscope and an AK8963, wired
together on an internal I2C bus. The AK8963 answers at its own address
(0x0C) only when the MPU is put into I2C bypass mode. Without that, it sits
behind the MPU's I2C master and this library will not see it.
initialize() runs the self-test. It resets the device, verifies it
answers, reads the fuse ROM, self-tests, and leaves it in continuous 100Hz
mode. A false return means one of those steps failed - check
getDeviceId() returns 0x48.
read() returning false is not necessarily an error. In continuous mode
it returns false whenever no new sample is ready yet. It also returns false
on a magnetic overflow, which overflow() distinguishes.
Address. The I2C address is set by the CAD0/CAD1 pins and defaults to
0x0C; pass another to the constructor if those pins are strapped
differently.
1.0.0 - Correctness pass over the original 2018 code. The library now
compiles clean under -Wall -Wextra and builds for AVR. Fixed:
- Sensitivity adjustment was never read.
setMode()deliberately rejectsFUSE_ROM_ACCESS, but_initializeSensitivityAdjustment()called it anyway, so the device never entered fuse ROM access mode and the ASA registers were read while powered down. Every measurement was silently mis-scaled, andinitialize()always returnedfalse. Mode changes now go through an internal unguarded path; the publicsetMode()keeps its guard. - Buffer overrun. The measurement burst reads seven bytes (
HXL..ST2) into what was a six-byte member array, and then read_buffer[6]out of bounds for the overflow check - corrupting the adjacent member. selfTest()could hang forever waiting forDRDYon a device that never became ready. It is now bounded, and it releases the self-test field and restores the previous mode on every exit path.- Short I2C reads were treated as successes.
readBytes()returns a byte count; callers tested it as a boolean, so a partial transfer produced garbage measurements. Reads are now length-checked, the read loop is bounded by the caller's buffer, and the timeout survives amillis()rollover. - Mode changes did not honour the datasheet timing. The delay was an
uncalibrated
noploop; it is now an explicit 100us, and every mode change transitions through power-down as the datasheet requires. writeWords()in the I2C helper incremented its index twice per iteration, writing every other word and running off the end.- Added the packaging the library never had: this README, an example,
library.propertiesandkeywords.txt.
Also renamed from AK8963_I2C to AK8963 - class, files and repository.
The _I2C suffix originally distinguished this library from a separate
I2C-and-SPI rewrite, which was abandoned before it compiled and has since
been removed; the distinction no longer means anything. This library is
still I2C-only.
0.1.0 - Original 2018 implementation.
The MIT License (MIT)
Copyright (c) 2018 Vasil Kalchev