nanoFramework.Iot.Device.Ak8963 1.2.313

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package nanoFramework.Iot.Device.Ak8963 --version 1.2.313                
NuGet\Install-Package nanoFramework.Iot.Device.Ak8963 -Version 1.2.313                
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.313" />                
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.313                
#r "nuget: nanoFramework.Iot.Device.Ak8963, 1.2.313"                
#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.313

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

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.631 95 8/28/2024
1.2.590 84 7/17/2024
1.2.570 107 6/14/2024
1.2.436 248 11/10/2023
1.2.416 125 11/8/2023
1.2.329 175 5/26/2023
1.2.313 165 5/12/2023
1.2.297 165 5/3/2023
1.2.253 279 2/22/2023
1.2.222 308 1/9/2023
1.2.217 317 1/6/2023
1.2.212 293 1/5/2023
1.2.208 308 1/3/2023
1.2.203 285 12/28/2022
1.2.159 381 11/14/2022
1.2.153 382 11/5/2022
1.2.141 420 10/25/2022
1.2.128 397 10/22/2022
1.2.87 505 9/15/2022
1.2.82 514 9/14/2022
1.1.116.8772 414 6/24/2022
1.1.113.2032 422 6/23/2022
1.1.97.17326 445 6/13/2022
1.1.92.53000 454 6/8/2022
1.1.58.10097 451 5/23/2022
1.1.3 491 4/15/2022
1.1.1 463 4/14/2022
1.0.300 449 3/31/2022
1.0.288-preview.114 125 3/25/2022
1.0.288-preview.113 115 3/25/2022
1.0.288-preview.106 113 3/23/2022
1.0.288-preview.104 110 3/22/2022
1.0.288-preview.100 111 3/19/2022
1.0.288-preview.99 125 3/18/2022
1.0.288-preview.98 116 3/18/2022
1.0.288-preview.93 116 3/15/2022
1.0.288-preview.87 115 3/10/2022
1.0.288-preview.86 117 3/8/2022
1.0.288-preview.65 122 2/18/2022
1.0.288-preview.48 133 2/4/2022
1.0.288-preview.41 129 1/31/2022
1.0.288-preview.29 124 1/28/2022
1.0.288-preview.20 132 1/27/2022
1.0.288-preview.19 125 1/27/2022
1.0.288-preview.5 134 1/24/2022
1.0.288-preview.1 124 1/21/2022
1.0.272 319 1/10/2022
1.0.259 344 12/9/2021
1.0.218 368 10/18/2021
1.0.155 332 8/31/2021
1.0.130 186 7/6/2021
1.0.129 155 7/6/2021
1.0.125 196 7/5/2021
1.0.121 204 6/29/2021
1.0.119 222 6/28/2021
1.0.56 202 5/25/2021
1.0.9 197 5/21/2021