nanoFramework.Iot.Device.Ak8963 1.2.869

Prefix Reserved
dotnet add package nanoFramework.Iot.Device.Ak8963 --version 1.2.869
                    
NuGet\Install-Package nanoFramework.Iot.Device.Ak8963 -Version 1.2.869
                    
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.869" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="nanoFramework.Iot.Device.Ak8963" Version="1.2.869" />
                    
Directory.Packages.props
<PackageReference Include="nanoFramework.Iot.Device.Ak8963" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add nanoFramework.Iot.Device.Ak8963 --version 1.2.869
                    
#r "nuget: nanoFramework.Iot.Device.Ak8963, 1.2.869"
                    
#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.
#:package nanoFramework.Iot.Device.Ak8963@1.2.869
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=nanoFramework.Iot.Device.Ak8963&version=1.2.869
                    
Install as a Cake Addin
#tool nuget:?package=nanoFramework.Iot.Device.Ak8963&version=1.2.869
                    
Install as a Cake Tool

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.869 198 4/2/2025
1.2.864 178 4/2/2025
1.2.852 196 3/11/2025
1.2.822 131 2/26/2025
1.2.775 135 2/4/2025
1.2.772 138 2/4/2025
1.2.759 145 1/31/2025
1.2.755 135 1/31/2025
1.2.737 118 1/13/2025
1.2.704 139 12/18/2024
1.2.696 126 12/16/2024
1.2.673 140 10/23/2024
1.2.631 151 8/28/2024
1.2.590 133 7/17/2024
1.2.570 160 6/14/2024
1.2.436 274 11/10/2023
1.2.416 152 11/8/2023
1.2.329 226 5/26/2023
1.2.313 216 5/12/2023
1.2.297 222 5/3/2023
1.2.253 333 2/22/2023
1.2.222 369 1/9/2023
1.2.217 374 1/6/2023
1.2.212 355 1/5/2023
1.2.208 363 1/3/2023
1.2.203 340 12/28/2022
1.2.159 436 11/14/2022
1.2.153 442 11/5/2022
1.2.141 477 10/25/2022
1.2.128 457 10/22/2022
1.2.87 570 9/15/2022
1.2.82 578 9/14/2022
1.1.116.8772 484 6/24/2022
1.1.113.2032 494 6/23/2022
1.1.97.17326 515 6/13/2022
1.1.92.53000 528 6/8/2022
1.1.58.10097 530 5/23/2022
1.1.3 565 4/15/2022
1.1.1 537 4/14/2022
1.0.300 523 3/31/2022
1.0.288-preview.114 192 3/25/2022
1.0.288-preview.113 183 3/25/2022
1.0.288-preview.106 187 3/23/2022
1.0.288-preview.104 182 3/22/2022
1.0.288-preview.100 180 3/19/2022
1.0.288-preview.99 194 3/18/2022
1.0.288-preview.98 188 3/18/2022
1.0.288-preview.93 190 3/15/2022
1.0.288-preview.87 189 3/10/2022
1.0.288-preview.86 185 3/8/2022
1.0.288-preview.65 196 2/18/2022
1.0.288-preview.48 209 2/4/2022
1.0.288-preview.41 207 1/31/2022
1.0.288-preview.29 205 1/28/2022
1.0.288-preview.20 208 1/27/2022
1.0.288-preview.19 198 1/27/2022
1.0.288-preview.5 206 1/24/2022
1.0.288-preview.1 199 1/21/2022
1.0.272 405 1/10/2022
1.0.259 424 12/9/2021
1.0.218 443 10/18/2021
1.0.155 409 8/31/2021
1.0.130 273 7/6/2021
1.0.129 242 7/6/2021
1.0.125 276 7/5/2021
1.0.121 285 6/29/2021
1.0.119 303 6/28/2021
1.0.56 280 5/25/2021
1.0.9 278 5/21/2021