Remarks

Mag3110
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Motion.Mag3110

The MAG3110 is a three axis magnetometer with an I2C interface. The magnetometer is capable of single and continuous readings.

Code Example

Mag3110 sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initialize...");

    sensor = new Mag3110(Device.CreateI2cBus());

    // classical .NET events can  be used
    sensor.Updated += (sender, result) => {
        Resolver.Log.Info($"Magnetic Field: [X:{result.New.MagneticField3D?.X.MicroTesla:N2}," +
            $"Y:{result.New.MagneticField3D?.Y.MicroTesla:N2}," +
            $"Z:{result.New.MagneticField3D?.Z.MicroTesla:N2} (MicroTeslas)]");

        Resolver.Log.Info($"Temp: {result.New.Temperature?.Celsius:N2}C");
    };

    // Example that uses an IObservable subscription to only be notified when the filter is satisfied
    var consumer = Mag3110.CreateObserver(
        handler: result => Resolver.Log.Info($"Observer: [x] changed by threshold; new [x]: X:{result.New.MagneticField3D?.X.MicroTesla:N2}, old: X:{result.Old?.MagneticField3D?.X.MicroTesla:N2}"),
        // only notify if there's a greater than 1 micro tesla on the Y axis
        filter: result => {
            if (result.Old is { } old) { //c# 8 pattern match syntax. checks for !null and assigns var.
                return ((result.New.MagneticField3D - old.MagneticField3D)?.Y > new MagneticField(1, MU.MicroTesla));
            }
            return false;
        });
    sensor.Subscribe(consumer);

    return Task.CompletedTask;
}

public async override Task Run()
{
    var result = await sensor.Read();
    Resolver.Log.Info("Initial Readings:");
    Resolver.Log.Info($"Mangetic field: [X:{result.MagneticField3D?.X.MicroTesla:N2}," +
        $"Y:{result.MagneticField3D?.Y.MicroTesla:N2}," +
        $"Z:{result.MagneticField3D?.Z.MicroTesla:N2} (microteslas)]");

    Resolver.Log.Info($"Temp: {result.Temperature?.Celsius:N2}C");

    sensor.StartUpdating(TimeSpan.FromMilliseconds(500));
}

Sample project(s) available on GitHub

Polling Mode

The following application reads the values from the magnetometer and displays the readings on the debug output:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        Console.WriteLine("MAG3110 Test Application");
        Mag3110 mag3110 = new Mag3110();
        mag3110.Standby = false;
        int readingCount = 0;

        while (true)
        {
            mag3110.Read();
            readingCount++;
            Console.WriteLine(
                "Reading " + readingCount.ToString() + 
                ": x = " + mag3110.X.ToString() + 
                ", y = " + mag3110.Y.ToString() + 
                ", z = " + mag3110.Z.ToString());
            Thread.Sleep(1000);
        }
    }
}

Wiring Example

In it's basic configuration the magnetometer requires four connections:

Meadow Pin Sensor Pin Wire Color
3.3V Vcc Red
GND GND Black
SC SCK Green
SD SDA Blue
D8 INT1 Orange

Characteristic Locus
Inheritance System.Object ObservableBase<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>> SamplingSensorBase<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>> PollingSensorBase<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>> ByteCommsSensorBase<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>> > Mag3110
Implements ISamplingSensor<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>> ISamplingSensor<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>> IDisposable ITemperatureSensor IMagnetometer
Inherited Members ByteCommsSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.Peripheral ByteCommsSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.ReadBuffer ByteCommsSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.WriteBuffer ByteCommsSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.Init(Int32, Int32) ByteCommsSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.Dispose(Boolean) ByteCommsSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.Dispose() PollingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.StartUpdating(Nullable<TimeSpan>) PollingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.StopUpdating() SamplingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.samplingLock SamplingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.Updated SamplingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.SamplingTokenSource SamplingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.Conditions SamplingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.IsSampling SamplingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.UpdateInterval SamplingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.ReadSensor() SamplingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.RaiseEventsAndNotify(IChangeResult<>) SamplingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.Read() ObservableBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.observers ObservableBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Motion
Assembly MAG3110.dll

Syntax

public class Mag3110 : ByteCommsSensorBase<(MagneticField3D? MagneticField3D, Units.Temperature? Temperature)>, ISamplingSensor<(MagneticField3D? MagneticField3D, Units.Temperature? Temperature)>, ISamplingSensor<(MagneticField3D? MagneticField3D, Units.Temperature? Temperature)>, IDisposable, ITemperatureSensor, IMagnetometer

Constructors

Mag3110(II2cBus, IDigitalInputPort, Byte)

Create a new MAG3110 object using the default parameters for the component

Declaration
public Mag3110(II2cBus i2cBus, IDigitalInputPort interruptPort = null, byte address = null)

Parameters

Type Name Description
II2cBus i2cBus

I2C bus object - default = 400 KHz)

IDigitalInputPort interruptPort

Interrupt port used to detect end of conversions

System.Byte address

Address of the MAG3110 (default = 0x0e)

Fields

interruptPort

Interrupt port used to detect then end of a conversion

Declaration
protected readonly IDigitalInputPort interruptPort

Field Value

Type Description
IDigitalInputPort

Properties

DigitalInputsEnabled

Enable or disable interrupts.

Declaration
public bool DigitalInputsEnabled { get; set; }

Property Value

Type Description
System.Boolean

Remarks

Interrupts can be triggered when a conversion completes (see section 4.2.5 of the datasheet). The interrupts are tied to the ZYXDR bit in the DR Status register.

IsDataReady

Indicate if there is any data ready for reading (x, y or z).

Declaration
public bool IsDataReady { get; }

Property Value

Type Description
System.Boolean

Remarks

See section 5.1.1 of the datasheet.

MagneticField3d

The current magnetic field value

Declaration
public MagneticField3D? MagneticField3d { get; }

Property Value

Type Description
System.Nullable<MagneticField3D>

Standby

Change or get the standby status of the sensor

Declaration
public bool Standby { get; set; }

Property Value

Type Description
System.Boolean

Temperature

Current emperature of the die

Declaration
public Units.Temperature? Temperature { get; }

Property Value

Type Description
System.Nullable<Units.Temperature>

Methods

RaiseEventsAndNotify(IChangeResult<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>)

Raise events for subcribers and notify of value changes

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<(MagneticField3D? MagneticField3D, Units.Temperature? Temperature)> changeResult)

Parameters

Type Name Description
IChangeResult<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>> changeResult

The updated sensor data

ReadSensor()

Reads data from the sensor

Declaration
protected override Task<(MagneticField3D? MagneticField3D, Units.Temperature? Temperature)> ReadSensor()

Returns

Type Description
Task<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>>

The latest sensor reading

Overrides

Meadow.Foundation.SamplingSensorBase<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>>.ReadSensor()

Reset()

Reset the sensor

Declaration
public void Reset()

Events

MagneticField3dUpdated

Raised when the magnetic field value changes

Declaration
public event EventHandler<IChangeResult<MagneticField3D>> MagneticField3dUpdated

Event Type

Type Description
EventHandler<IChangeResult<MagneticField3D>>

TemperatureUpdated

Raised when the temperature value changes

Declaration
public event EventHandler<IChangeResult<Units.Temperature>> TemperatureUpdated

Event Type

Type Description
EventHandler<IChangeResult<Units.Temperature>>