nanoFramework.Iot.Device.Bmm150 1.2.696

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package nanoFramework.Iot.Device.Bmm150 --version 1.2.696                
NuGet\Install-Package nanoFramework.Iot.Device.Bmm150 -Version 1.2.696                
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.Bmm150" Version="1.2.696" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.Iot.Device.Bmm150 --version 1.2.696                
#r "nuget: nanoFramework.Iot.Device.Bmm150, 1.2.696"                
#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.Bmm150 as a Cake Addin
#addin nuget:?package=nanoFramework.Iot.Device.Bmm150&version=1.2.696

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

Bmm150 - Magnetometer

The Bmm150 is a magnetometer that can be controlled either thru I2C either thru SPI. This implementation was tested in a ESP32 platform, specificaly in a M5Stack Gray.

Documentation

Documentation for the Bmm150 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.

// The I2C pins 21 and 22 in the sample below are ESP32 specific and may differ from other platforms.
// Please double check your device datasheet.
Configuration.SetPinFunction(22, DeviceFunction.I2C1_CLOCK);
Configuration.SetPinFunction(21, DeviceFunction.I2C1_DATA);

I2cConnectionSettings mpui2CConnectionSettingmpus = new(1, Bmm150.DefaultI2cAddress);

using Bmm150 bmm150 = new Bmm150(I2cDevice.Create(mpui2CConnectionSettingmpus));

Debug.WriteLine($"Please move your device in all directions...");

bmm150.CalibrateMagnetometer();

Debug.WriteLine($"Calibration completed.");

while (true)
{
    Vector3 magne = bmm150.ReadMagnetometer(true, TimeSpan.FromMilliseconds(11));

    var head_dir = Math.Atan2(magne.X, magne.Y) * 180.0 / Math.PI;

    Debug.WriteLine($"Mag data: X={magne.X,15}, Y={magne.Y,15}, Z={magne.Z,15}, head_dir: {head_dir}");

    Thread.Sleep(100);
}

Expected output

Please move your device in all directions...
Calibration completed.
Mag data: X=    32.97089767, Y=   -10.99029922, Z=   -27.41439819, head_dir: 108.43494945
Mag data: X=    38.83239364, Y=   -10.62395668, Z=    -22.2116661, head_dir: 105.30084201
Mag data: X=    43.96039581, Y=     -8.4257431, Z=     4.60182046, head_dir: 100.85010634
Mag data: X=    42.49582672, Y=   -8.059553146, Z=   9.0047292709, head_dir: 100.7388972
Mag data: X=    42.86371994, Y=    -12.8224802, Z=     8.20643711, head_dir: 106.65430547
Mag data: X=    36.26864242, Y=    -6.22794914, Z=   -21.41402244, head_dir: 99.74364301
Mag data: X=    29.30693054, Y=    -9.89108943, Z=   -32.21274185, head_dir: 108.6495335
Mag data: X=    15.75333309, Y=    -8.42620182, Z=  -37.029045104, head_dir: 118.14159082
Mag data: X=      4.7626357, Y=    -6.22806167, Z=   -42.23312759, head_dir: 142.59463794
Mag data: X=    -4.39627885, Y=    -6.59441852, Z=   -36.22841644, head_dir: -146.309933
Mag data: X=   -10.25779819, Y=    -5.12889909, Z=   -38.62528991, head_dir: -116.56504656
Mag data: X=  -19.050889968, Y=     0.73272651, Z=  -37.033847808, head_dir: -87.7974031
Mag data: X=   -35.90294647, Y=     1.46542632, Z=   -18.61460113, head_dir: -87.66269127
Mag data: X=   -37.73472976, Y=     8.42620182, Z=   -13.41051959, head_dir: -77.41230537
Mag data: X=   -37.73472976, Y=     9.52527141, Z=   -12.20957756, head_dir: -75.83294707
Mag data: X=   -18.31749725, Y=     0.73269987, Z=   -31.42057418, head_dir: -87.70938928
Mag data: X=    -2.19813942, Y=    -7.69348812, Z=  -37.029045104, head_dir: -164.054600542

Calibration

You can get access perfom calibration thru the CalibrateMagnetometer function which will. Be aware that the calibration takes a few seconds.

bmm150.CalibrateMagnetometer();

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

Not supported/implemented features of the Bmm150

  • Device Self-Tests
  • Device Reset
  • Toggle operation modes (defaults to normal mode)

Testing

Unit tests project is in \Bmm150.tests. You can use VS2019 built-in test capabilites as follows:

unit tests

[TestMethod]
public void TestCompensateVector3()
{
        uint rhall = 42;
        Vector3 rawMagnetormeterData = new Vector3 { X = 13.91375923, Y = -28.74289894, Z = 10.16711997 };
        Bmm150TrimRegisterData trimRegisterData = new Bmm150TrimRegisterData()
        {
            DigX1 = 0,
            DigX2 = 26,
            DigXy1 = 29,
            DigXy2 = -3,
            DigXyz1 = 7053,
            DigY1 = 0,
            DigY2 = 26,
            DigZ1 = 24747,
            DigZ2 = 763,
            DigZ3 = 0,
            DigZ4 = 0
        };

        double x = Bmm150Compensation.CompensateX(rawMagnetormeterData.X, rhall, trimRegisterData);
        double y = Bmm150Compensation.CompensateY(rawMagnetormeterData.Y, rhall, trimRegisterData);
        double z = Bmm150Compensation.CompensateZ(rawMagnetormeterData.Z, rhall, trimRegisterData);

        // Calculated value should be: -1549.91882323
        Assert.Equal(Math.Ceiling(x), Math.Ceiling(-1549.918823), "Unexpected x-axis value.");

        // Calculated value should be: 3201.80615234
        Assert.Equal(Math.Ceiling(y), Math.Ceiling(3201.80615234), "Unexpected y-axis value.");

        // Calculated value should be: 26.20077896
        Assert.Equal(Math.Ceiling(z), Math.Ceiling(26.20077896), "Unexpected z-axis value.");
}

Notes

  • The BMI160 embedd this BMM150.
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 (4)

Showing the top 4 NuGet packages that depend on nanoFramework.Iot.Device.Bmm150:

Package Downloads
nanoFramework.M5Core2

This package includes the nanoFramework.M5Core2 assembly for .NET nanoFramework C# projects.

nanoFramework.M5Core

This package includes the nanoFramework.M5Core assembly for .NET nanoFramework C# projects.

nanoFramework.Fire

This package includes the nanoFramework.Fire assembly for .NET nanoFramework C# projects.

nanoFramework.M5Stack

This package includes the nanoFramework.M5Stack assembly for .NET nanoFramework C# projects.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on nanoFramework.Iot.Device.Bmm150:

Repository Stars
nanoframework/nanoFramework.IoT.Device
📦 This repo includes .NET nanoFramework implementations for various sensors, chips, displays, hats and drivers
nanoframework/nanoFramework.M5Stack
:package: Board support package for M5Stack, M5StickC and M5StickCPlus for .NET nanoFramework
Version Downloads Last updated
1.2.704 195 12/18/2024
1.2.696 178 12/16/2024
1.2.673 631 10/23/2024
1.2.631 1,194 8/28/2024
1.2.590 871 7/17/2024
1.2.570 581 6/14/2024
1.2.560 314 5/29/2024
1.2.552 209 5/17/2024
1.2.536 610 4/15/2024
1.2.514 576 3/22/2024
1.2.436 1,501 11/10/2023
1.2.416 414 11/8/2023
1.2.329 3,128 5/26/2023
1.2.313 623 5/12/2023
1.2.308 441 5/11/2023
1.2.302 226 5/10/2023
1.2.297 455 5/3/2023
1.2.273 1,729 3/17/2023
1.2.253 3,030 2/22/2023
1.2.222 1,747 1/9/2023
1.2.217 1,327 1/6/2023
1.2.215 857 1/6/2023
1.2.212 865 1/5/2023
1.2.208 627 1/3/2023
1.2.203 894 12/28/2022
1.2.159 1,259 11/14/2022
1.2.153 2,419 11/5/2022
1.2.141 3,169 10/25/2022
1.2.128 500 10/22/2022
1.2.87 8,840 9/15/2022
1.2.32 8,210 8/2/2022
1.1.116.8772 14,818 6/24/2022
1.1.113.2032 479 6/23/2022
1.1.97.17326 3,977 6/13/2022
1.1.92.53000 1,435 6/8/2022
1.1.58.10097 9,815 5/23/2022
1.1.3 16,695 4/15/2022
1.1.1 497 4/14/2022
1.0.300 5,145 3/31/2022
1.0.288-preview.114 161 3/25/2022
1.0.288-preview.113 132 3/25/2022
1.0.288-preview.106 148 3/23/2022
1.0.288-preview.104 126 3/22/2022
1.0.288-preview.100 139 3/19/2022
1.0.288-preview.99 147 3/18/2022
1.0.288-preview.98 132 3/18/2022
1.0.288-preview.93 170 3/15/2022
1.0.288-preview.86 189 3/8/2022
1.0.288-preview.65 192 2/18/2022
1.0.288-preview.50 188 2/5/2022
1.0.288-preview.48 155 2/4/2022
1.0.288-preview.41 163 1/31/2022
1.0.288-preview.29 206 1/28/2022
1.0.288-preview.22 198 1/27/2022
1.0.288-preview.20 156 1/27/2022
1.0.288-preview.19 142 1/27/2022
1.0.288-preview.5 183 1/24/2022
1.0.288-preview.1 170 1/21/2022
1.0.272 569 1/10/2022
1.0.260 837 12/10/2021
1.0.259 343 12/9/2021
1.0.218 536 10/18/2021
1.0.207 402 10/11/2021
1.0.191 360 9/29/2021
1.0.170 366 9/17/2021