nanoFramework.Iot.Device.Ak8963 1.2.570

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package nanoFramework.Iot.Device.Ak8963 --version 1.2.570
NuGet\Install-Package nanoFramework.Iot.Device.Ak8963 -Version 1.2.570
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nanoFramework.Iot.Device.Ak8963" Version="1.2.570" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.Iot.Device.Ak8963 --version 1.2.570
#r "nuget: nanoFramework.Iot.Device.Ak8963, 1.2.570"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install nanoFramework.Iot.Device.Ak8963 as a Cake Addin
#addin nuget:?package=nanoFramework.Iot.Device.Ak8963&version=1.2.570

// Install nanoFramework.Iot.Device.Ak8963 as a Cake Tool
#tool nuget:?package=nanoFramework.Iot.Device.Ak8963&version=1.2.570

AK8963 - Magnetometer

The AK8963 is a magnetometer that can be controlled either thru I2C either thru SPI. It is present in other sensors like the MPU9250. This implementation fully supports the I2C mode and the usage thru the MPU9250. It does not support SPI.

Documentation

Documentation for the AK8963 can be found here

Usage

Important: make sure you properly setup the I2C pins especially for ESP32 before creating the I2cDevice, make sure you install the nanoFramework.Hardware.ESP32 nuget:

//////////////////////////////////////////////////////////////////////
// when connecting to an ESP32 device, need to configure the I2C GPIOs
// used for the bus
Configuration.SetPinFunction(21, DeviceFunction.I2C1_DATA);
Configuration.SetPinFunction(22, DeviceFunction.I2C1_CLOCK);

For other devices like STM32, please make sure you're using the preset pins for the I2C bus you want to use.

You can find an example in the sample directory. Usage is straight forward including the possibility to have a calibration.

var mpui2CConnectionSettingmpus = new I2cConnectionSettings(1, Ak8963.Ak8963.DefaultI2cAddress);
// This will use the default I2C interface
Ak8963 ak8963 = new Ak8963(I2cDevice.Create(mpui2CConnectionSettingmpus));
if (!ak8963.CheckVersion())
    throw new IOException($"This device does not contain the correct signature 0x48 for a AK8963");

while (true)
{
    var magne = ak8963.ReadMagnetometer(true);
    Debug.WriteLine($"Mag X = {magne.X, 15}");
    Debug.WriteLine($"Mag Y = {magne.Y, 15}");
    Debug.WriteLine($"Mag Z = {magne.Z, 15}");
    Thread.Sleep(100);
}

Calibration and bias

You can get access to the self tests and calibration thru the CalibrateMagnetometer function which will return the bias calibration. Be aware that the calibration takes couple of seconds.

var magBias = ak8963.CalibrateMagnetometer();
Debug.WriteLine($"Factory Bias:");
Debug.WriteLine($"Mag X = {magBias.X}");
Debug.WriteLine($"Mag Y = {magBias.Y}");
Debug.WriteLine($"Mag Z = {magBias.Z}");
Debug.WriteLine($"Bias from calibration:");
Debug.WriteLine($"Mag X = {ak8963.MagnometerBias.X}");
Debug.WriteLine($"Mag Y = {ak8963.MagnometerBias.Y}");
Debug.WriteLine($"Mag Z = {ak8963.MagnometerBias.Z}");

You will find a full example on how to extract raw data without calibration on the MPU9250 sample.

If no calibration is performed, you will get a raw data cloud which looks like this:

raw data

Running the calibration properly require to move the sensor in all the possible directions while performing the calibration. You should consider running it with enough samples, at least few hundreds. The default is set to 1000. While moving the sensor in all direction, far from any magnetic field, you will get the previous clouds. Calculating the average from those clouds and subtracting it from the read value will give you a centered cloud of data like this:

raw data

To create those cloud point graphs, every cloud is a coordinate of X-Y, Y-Z and Z-X.

Once the calibration is done, you will be able to read the data with the bias corrected using the ReadMagnetometer function. You will still be able to read the data without any calibration using the ReadMagnetometerWithoutCalibration function.

Using a different I2C interface

This sensor is used for example in the MPU9250. The MPU9250 is in this case a master I2C controlling the secondary AK8963 I2C sensor. An abstract class is available to implement basic I2C operation:

public abstract class Ak8963I2cBase
{
    public abstract void WriteRegister(I2cDevice i2CDevice, Register reg, byte data);
    public abstract byte ReadByte(I2cDevice i2CDevice, Register reg);
    public abstract void ReadBytes(I2cDevice i2CDevice, Register reg, Span<byte> readBytes);
}

For example the I2C basic implementation is the following:

public class Ak8963I2c : Ak8963I2cBase
{
    public override byte ReadByte(I2cDevice i2cDevice, Register reg)
    {
        i2cDevice.WriteByte((byte)reg);
        return i2cDevice.ReadByte();
    }

    public override void ReadBytes(I2cDevice i2cDevice, Register reg, Span<byte> readBytes)
    {
        i2cDevice.WriteByte((byte)reg);
        i2cDevice.Read(readBytes);
    }

    public override void WriteRegister(I2cDevice i2cDevice, Register reg, byte data)
    {
        Span<byte> dataout = stackalloc byte[] { (byte)reg, data };
        i2cDevice.Write(dataout);
    }
}

The class embedded into the MPU9250 is more complex, for example here is the code to do the WriteRegister operation:

public override void WriteRegister(I2cDevice i2cDevice, Ak8963.Register reg, byte data)
{
    Span<byte> dataout = stackalloc byte[2] { (byte)Register.I2C_SLV0_ADDR, Ak8963.Ak8963.DefaultI2cAddress };
    i2cDevice.Write(dataout);
    dataout[0] = (byte)Register.I2C_SLV0_REG;
    dataout[1] = (byte)reg;
    i2cDevice.Write(dataout);
    dataout[0] = (byte)Register.I2C_SLV0_DO;
    dataout[1] = data;
    i2cDevice.Write(dataout);
    dataout[0] = (byte)Register.I2C_SLV0_CTRL;
    dataout[1] = 0x81;
    i2cDevice.Write(dataout);
}

If you have to use a different I2C interface, you have to use the constructor where you can pass it:

ak8963 = new Ak8963(_i2cDevice, new Ak8963Attached(), false);

Circuit

Only I2C is supported in this version.

  • SCL - SCL
  • SDA - SDA
  • VCC - 3.3V
  • GND - GND

Depending on the version you have, you may have to select I2C over SPI. This is done in different way depending on the board you'll have.

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.570 77 6/14/2024
1.2.436 229 11/10/2023
1.2.416 104 11/8/2023
1.2.329 159 5/26/2023
1.2.313 138 5/12/2023
1.2.297 149 5/3/2023
1.2.253 261 2/22/2023
1.2.222 292 1/9/2023
1.2.217 301 1/6/2023
1.2.212 274 1/5/2023
1.2.208 292 1/3/2023
1.2.203 268 12/28/2022
1.2.159 363 11/14/2022
1.2.153 363 11/5/2022
1.2.141 394 10/25/2022
1.2.128 380 10/22/2022
1.2.87 488 9/15/2022
1.2.82 486 9/14/2022
1.1.116.8772 393 6/24/2022
1.1.113.2032 404 6/23/2022
1.1.97.17326 427 6/13/2022
1.1.92.53000 432 6/8/2022
1.1.58.10097 428 5/23/2022
1.1.3 469 4/15/2022
1.1.1 442 4/14/2022
1.0.300 433 3/31/2022
1.0.288-preview.114 120 3/25/2022
1.0.288-preview.113 109 3/25/2022
1.0.288-preview.106 106 3/23/2022
1.0.288-preview.104 103 3/22/2022
1.0.288-preview.100 105 3/19/2022
1.0.288-preview.99 118 3/18/2022
1.0.288-preview.98 111 3/18/2022
1.0.288-preview.93 111 3/15/2022
1.0.288-preview.87 110 3/10/2022
1.0.288-preview.86 111 3/8/2022
1.0.288-preview.65 119 2/18/2022
1.0.288-preview.48 130 2/4/2022
1.0.288-preview.41 125 1/31/2022
1.0.288-preview.29 121 1/28/2022
1.0.288-preview.20 129 1/27/2022
1.0.288-preview.19 123 1/27/2022
1.0.288-preview.5 133 1/24/2022
1.0.288-preview.1 122 1/21/2022
1.0.272 306 1/10/2022
1.0.259 328 12/9/2021
1.0.218 362 10/18/2021
1.0.155 316 8/31/2021
1.0.130 182 7/6/2021
1.0.129 151 7/6/2021
1.0.125 192 7/5/2021
1.0.121 198 6/29/2021
1.0.119 215 6/28/2021
1.0.56 193 5/25/2021
1.0.9 193 5/21/2021