GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - VasilKalchev/AK8963: I2C library for the AK8963 3-axis electronic compass.

BranchesTags
Open more actions menu

Latest commit

 

History

5 Commits
5 Commits

Folders and files

NameName
Last commit message
Last commit date
examples/Basic
examples/Basic
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
AK8963.cpp
AK8963.cpp
 
 
AK8963.hpp
AK8963.hpp
 
 
I2Chelper.cpp
I2Chelper.cpp
 
 
I2Chelper.hpp
I2Chelper.hpp
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
keywords.txt
keywords.txt
 
 
library.properties
library.properties
 
 

Repository files navigation

AK8963

License Status

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.

Status

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).

Features

  • 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

Requirements

  • AK8963 (standalone, or inside an MPU-9250/9255)
  • A microcontroller with I2C, using the Arduino Wire library

Quick start

#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.

Notes

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.

Version history

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 rejects FUSE_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, and initialize() always returned false. Mode changes now go through an internal unguarded path; the public setMode() 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 for DRDY on 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 a millis() rollover.
  • Mode changes did not honour the datasheet timing. The delay was an uncalibrated nop loop; 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.properties and keywords.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.

License

The MIT License (MIT)

Copyright (c) 2018 Vasil Kalchev

About

I2C library for the AK8963 3-axis electronic compass.

Topics

ak8963firmware-libraryi2c-sensorsmagnetometer

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.