nanoFramework.Iot.Device.Mpu9250 1.2.889

Prefix Reserved
dotnet add package nanoFramework.Iot.Device.Mpu9250 --version 1.2.889
                    
NuGet\Install-Package nanoFramework.Iot.Device.Mpu9250 -Version 1.2.889
                    
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.Mpu9250" Version="1.2.889" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="nanoFramework.Iot.Device.Mpu9250" Version="1.2.889" />
                    
Directory.Packages.props
<PackageReference Include="nanoFramework.Iot.Device.Mpu9250" />
                    
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.Mpu9250 --version 1.2.889
                    
#r "nuget: nanoFramework.Iot.Device.Mpu9250, 1.2.889"
                    
#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.Mpu9250@1.2.889
                    
#: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.Mpu9250&version=1.2.889
                    
Install as a Cake Addin
#tool nuget:?package=nanoFramework.Iot.Device.Mpu9250&version=1.2.889
                    
Install as a Cake Tool

MPU6050/MPU6500/MPU9250 - Gyroscope, Accelerometer, Temperature and Magnetometer (MPU9250 only)

MPU6050/MPU6500 are 3 axis Gyroscope, 3 axis Accelerometer and Temperature sensors that can be accessed either thru I2C or SPI. This implementation is only for I2C. The sensor can be found in various implementation but its main usage is in the MPU9250.

MPU9250 is a 3 axis Gyroscope, 3 axis Accelerometer, 3 axis Magnetometer and Temperature sensor that can be accessed either thru I2C or SPI. This implementation is only for I2C. The sensor can be found in various implementation like Grove or Sparkfun. MPU9250 incorporate a MPU6500 and an AK8963.

The Magnetometer used is an AK8963. In general, it is managed thru the main MPU9250 and setup as a replica I2C. All operations go thru the MPU9250. In some cases, the AK8963 is exposed and the operations are not going thru the MPU9250 but directly.

Documentation

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.

General case with AK8963 is not exposed (where you can't find it on the I2C bus at the address 0x0C)

var mpui2CConnectionSettingmpus = new I2cConnectionSettings(1, Mpu9250.DefaultI2cAddress);
Mpu9250 mpu9250 = new Mpu9250(I2cDevice.Create(mpui2CConnectionSettingmpus));

In case the AK8963 is exposed, so you can reach it directly, you can then use the following code:

var mpui2CConnectionSettingmpus = new I2cConnectionSettings(1, Mpu9250.DefaultI2cAddress);
using Mpu9250 mpu9250 = new Mpu9250(I2cDevice.Create(mpui2CConnectionSettingmpus), i2CDeviceAk8963: I2cDevice.Create(new I2cConnectionSettings(1, Ak8963.DefaultI2cAddress)));

You can find an example in the sample directory. Usage is straightforward including the possibility to have a calibration for all sub sensors.

var mpui2CConnectionSettingmpus = new I2cConnectionSettings(1, Mpu9250.DefaultI2cAddress);
Mpu9250 mpu9250 = new Mpu9250(I2cDevice.Create(mpui2CConnectionSettingmpus));
var gyro = mpu9250.GetGyroscope();
Debug.WriteLine($"Gyro X = {gyro.X, 15}");
Debug.WriteLine($"Gyro Y = {gyro.Y, 15}");
Debug.WriteLine($"Gyro Z = {gyro.Z, 15}");
var acc = mpu9250.GetAccelerometer();
Debug.WriteLine($"Acc X = {acc.X, 15}");
Debug.WriteLine($"Acc Y = {acc.Y, 15}");
Debug.WriteLine($"Acc Z = {acc.Z, 15}");
Debug.WriteLine($"Temp = {mpu9250.Temperature.Celsius.ToString("0.00")} °C");
var magne = mpu9250.ReadMagnetometer(true);
Debug.WriteLine($"Mag X = {magne.X, 15}");
Debug.WriteLine($"Mag Y = {magne.Y, 15}");
Debug.WriteLine($"Mag Z = {magne.Z, 15}");

Self-test

A self-test is available for the gyroscope and the accelerometer.

var resSelfTest = mpu9250.RunGyroscopeAccelerometerSelfTest();
Debug.WriteLine($"Self test:");
Debug.WriteLine($"Gyro X = {resSelfTest.Item1.X} vs >0.005");
Debug.WriteLine($"Gyro Y = {resSelfTest.Item1.Y} vs >0.005");
Debug.WriteLine($"Gyro Z = {resSelfTest.Item1.Z} vs >0.005");
Debug.WriteLine($"Acc X = {resSelfTest.Item2.X} vs >0.005 & <0.015");
Debug.WriteLine($"Acc Y = {resSelfTest.Item2.Y} vs >0.005 & <0.015");
Debug.WriteLine($"Acc Z = {resSelfTest.Item2.Z} vs >0.005 & <0.015");

The returned data are the raw data and allows you to estimate the quality of the test. The first item of the tuple is the gyroscope and the second one the accelerometer.

No self-test is available for the magnetometer.

Calibration and bias

You can calibrate the Gyroscope and the Accelerometer at the same time. This action is as well correcting the registers directly in the MPU9250 chip. Those data are lost in case of power stop but stays in case of soft reset.

Debug.WriteLine("Running Gyroscope and Accelerometer calibration");
mpu9250.CalibrateGyroscopeAccelerometer();
Debug.WriteLine("Calibration results:");
Debug.WriteLine($"Gyro X bias = {mpu9250.GyroscopeBias.X}");
Debug.WriteLine($"Gyro Y bias = {mpu9250.GyroscopeBias.Y}");
Debug.WriteLine($"Gyro Z bias = {mpu9250.GyroscopeBias.Z}");
Debug.WriteLine($"Acc X bias = {mpu9250.AccelerometerBias.X}");
Debug.WriteLine($"Acc Y bias = {mpu9250.AccelerometerBias.Y}");
Debug.WriteLine($"Acc Z bias = {mpu9250.AccelerometerBias.Z}");

Calibration is as well available for the magnetometer (the AK8963). For this sensor.

Debug.WriteLine("Magnetometer calibration is taking couple of seconds, please be patient!");
Debug.WriteLine("Please make sure you are not close to any magnetic field like magnet or phone. Move the sensor in all possible directions");
var mag = mpu9250.CalibrateMagnetometer();
Debug.WriteLine($"Correction factor bias:");
Debug.WriteLine($"Mag X = {mpu9250.MagnometerBias.X}");
Debug.WriteLine($"Mag Y = {mpu9250.MagnometerBias.Y}");
Debug.WriteLine($"Mag Z = {mpu9250.MagnometerBias.Z}");

See AK8963 calibration for more information on how Magnetometer calibration is working. Please note that you have a full code sample to read and save data in a file to go deeper into the Magnetometer calibration.

Note: AK8963 calibration must be performed before other calibrations and before using any other part of the sensors.

Units

Al axis are oriented this way: +Z +Y \ | / \ | / |/ /|
/ |
/ |
+X

Gyroscope

The unit used for the gyroscope are degree per seconds.

Accelerometer

The unit used for the accelerometer is G.

Magnetometer

The unit used for the magnetometer is µTesla.

Temperature

The Temperature is a normalized Units.Temperature which can provide Celsius, Kelvin or Fahrenheit degrees.

Measurement modes

The MPU9250 offers a large variety of measurement modes. They can be changed and adjusted thru the properties like:

  • MagnetometerMeasurementMode to adjust the type of measurement for the magnetometer
  • MagnetometerOutputBitMode to select between 14 and 16 bits precision of the magnetometer
  • AccelerometerRange to adjust the range of the accelerometer between 2, 4, 8 or 16 G
  • AccelerometionScale to adjust the frequency of measurement from 5 Hz to 1130 Hz
  • GyroscopeRange to adjust the range of the gyroscope from 250, 500, 1000 and 2000 degrees per second
  • GyroscopeScale to adjust the frequency of measurement from 5 Hz to 8800 Hz
  • SampleRateDivider allows you to reduce the number of samples for the gyroscope and the accelerometer. This feature is only available for some of the bandwidth modes.
  • DisableModes allows you to disable any of the gyroscope and accelerometer axis

Wake on motion

A unique SetWakeOnMotion mode is available. It puts the MPU9250 in a low consumption, low measurement rate mode and trigger an interruption on the INT pin.

mpu9250.SetWakeOnMotion(300, AccelerometerLowPowerFrequency.Frequency0Dot24Hz);
// You'll need to attach the INT pin to a GPIO and read the level. Once going up, you have
// some data and the sensor is awake
// In order to simulate this without a GPIO pin, you will see that the refresh rate is very low
// Setup here at 0.24Hz which means, about every 4 seconds

while (true)
{
    var acc = mpu9250.GetAccelerometer();
    Debug.WriteLine($"Acc X = {acc.X, 15}");
    Debug.WriteLine($"Acc Y = {acc.Y, 15}");
    Debug.WriteLine($"Acc Z = {acc.Z, 15}");
    Thread.Sleep(100);
}

FIFO mode

The Fifo mode allows you to get the data by batch. You can select the mode thru FifoModes, then read the FifoCount property. You can then read the data thru ReadFifo Make sure you'll size the Span<byte> with FifoCount length.

Data are in the order of the Register from 0x3B to 0x60 so you'll get your data in this order:

  • ACCEL_XOUT_H and ACCEL_XOUT_L
  • ACCEL_YOUT_H and ACCEL_YOUT_L
  • ACCEL_ZOUT_H and ACCEL_ZOUT_L
  • TEMP_OUT_H and TEMP_OUT_L
  • GYRO_XOUT_H and GYRO_XOUT_L
  • GYRO_YOUT_H and GYRO_YOUT_L
  • GYRO_ZOUT_H and GYRO_ZOUT_L
  • EXT_SENS_DATA_00 to EXT_SENS_DATA_24

It is then up to you to transform them into the correct data. You can multiply your raw data by AccelerometionScale and GyroscopeScale to convert them properly.

I2C replica primitives

2 primitive functions allow to read and write any register in any of the replica devices.

  • I2cWrite(I2cChannel i2cChannel, byte address, byte register, byte data)
    • i2cChannel: The replica channel to attached to the I2C device
    • address: The I2C address of the replica I2C element
    • register: The register to write to the replica I2C element
    • data: The byte data to write to the replica I2C element
  • I2cRead(I2cChannel i2cChannel, byte address, byte register, SpanByte readBytes)
    • i2cChannel: The replica channel to attached to the I2C device
    • address: The I2C address of the replica I2C element
    • register: The register to write to the replica I2C element
    • readBytes: The read data

Circuit

The following fritzing diagram illustrates one way to wire up the MPU9250 with a MCU like ESP32 using I2C.

ESP32 Breadboard diagram

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.889 88 7/28/2025
1.2.869 217 4/2/2025
1.2.864 191 4/2/2025
1.2.852 188 3/11/2025
1.2.846 207 3/10/2025
1.2.822 150 2/26/2025
1.2.775 140 2/4/2025
1.2.772 144 2/4/2025
1.2.755 139 1/31/2025
1.2.743 133 1/20/2025
1.2.737 125 1/13/2025
1.2.718 133 12/30/2024
1.2.704 130 12/18/2024
1.2.696 143 12/16/2024
1.2.673 141 10/23/2024
1.2.662 136 10/11/2024
1.2.656 134 10/3/2024
1.2.639 160 9/6/2024
1.2.631 144 8/28/2024
1.2.613 164 8/9/2024
1.2.601 134 7/26/2024
1.2.590 142 7/17/2024
1.2.573 158 6/19/2024
1.2.570 135 6/14/2024
1.2.536 169 4/15/2024
1.2.514 175 3/22/2024
1.2.494 177 2/28/2024
1.2.462 228 1/5/2024
1.2.458 171 12/20/2023
1.2.436 219 11/10/2023
1.2.416 162 11/8/2023
1.2.403 206 10/6/2023
1.2.396 179 9/27/2023
1.2.384 217 9/6/2023
1.2.378 214 8/16/2023
1.2.369 255 8/2/2023
1.2.363 209 7/28/2023
1.2.357 234 7/19/2023
1.2.354 223 7/14/2023
1.2.345 227 6/21/2023
1.2.341 236 6/14/2023
1.2.337 242 6/7/2023
1.2.335 246 6/2/2023
1.2.329 225 5/26/2023
1.2.313 231 5/12/2023
1.2.302 236 5/10/2023
1.2.297 208 5/3/2023
1.2.273 296 3/17/2023
1.2.267 350 3/10/2023
1.2.263 345 3/8/2023
1.2.259 354 2/27/2023
1.2.256 343 2/24/2023
1.2.253 365 2/22/2023
1.2.222 415 1/9/2023
1.2.217 408 1/6/2023
1.2.212 398 1/5/2023
1.2.208 400 1/3/2023
1.2.203 413 12/28/2022
1.2.159 491 11/14/2022
1.2.153 476 11/5/2022
1.2.141 492 10/25/2022
1.2.128 487 10/22/2022
1.2.110 493 10/7/2022
1.2.87 607 9/15/2022
1.2.63 522 9/3/2022
1.2.47 500 8/15/2022
1.2.40 521 8/6/2022
1.2.38 509 8/5/2022
1.2.28 539 8/1/2022
1.2.13 542 7/24/2022
1.2.10 514 7/23/2022
1.1.142.3202 555 7/7/2022
1.1.133.52556 554 6/30/2022
1.1.121.35854 586 6/26/2022
1.1.116.8772 555 6/24/2022
1.1.113.2032 546 6/23/2022
1.1.102.51394 549 6/15/2022
1.1.99.36719 532 6/14/2022
1.1.97.17326 526 6/13/2022
1.1.92.53000 542 6/8/2022
1.1.72.29765 572 5/31/2022
1.1.64.21380 556 5/26/2022
1.1.58.10097 582 5/23/2022
1.1.54.28879 587 5/23/2022
1.1.40 567 5/5/2022
1.1.3 580 4/15/2022
1.1.1 568 4/14/2022
1.0.300 560 3/31/2022
1.0.288-preview.114 193 3/25/2022
1.0.288-preview.113 181 3/25/2022
1.0.288-preview.106 185 3/23/2022
1.0.288-preview.104 168 3/22/2022
1.0.288-preview.103 178 3/21/2022
1.0.288-preview.100 186 3/19/2022
1.0.288-preview.98 179 3/18/2022
1.0.288-preview.95 203 3/15/2022
1.0.288-preview.93 187 3/15/2022
1.0.288-preview.87 193 3/10/2022
1.0.288-preview.86 187 3/8/2022
1.0.288-preview.77 195 2/27/2022
1.0.288-preview.75 184 2/26/2022
1.0.288-preview.65 200 2/18/2022
1.0.288-preview.63 181 2/16/2022
1.0.288-preview.61 194 2/12/2022
1.0.288-preview.58 186 2/10/2022
1.0.288-preview.53 186 2/9/2022
1.0.288-preview.48 204 2/4/2022
1.0.288-preview.41 208 1/31/2022
1.0.288-preview.29 203 1/28/2022
1.0.288-preview.20 204 1/27/2022
1.0.288-preview.18 200 1/27/2022
1.0.288-preview.5 212 1/24/2022
1.0.272 607 1/10/2022
1.0.259 444 12/9/2021
1.0.258 406 12/7/2021
1.0.155 458 8/31/2021
1.0.130 242 7/6/2021
1.0.129 237 7/6/2021
1.0.125 271 7/5/2021
1.0.121 271 6/29/2021
1.0.119 299 6/28/2021
1.0.56 289 5/25/2021