Remarks

Mag3110
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for 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 MeadowApp()
{
    Console.WriteLine("Initializing");

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

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

        Console.WriteLine($"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 => Console.WriteLine($"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);

    //==== one-off read
    ReadConditions().Wait();

    // start updating
    sensor.StartUpdating(TimeSpan.FromMilliseconds(500));
}

protected async Task ReadConditions()
{
    var result = await sensor.Read();
    Console.WriteLine("Initial Readings:");
    Console.WriteLine($"Mangetic field: [X:{result.MagneticField3D?.X.MicroTesla:N2}," +
        $"Y:{result.MagneticField3D?.Y.MicroTesla:N2}," +
        $"Z:{result.MagneticField3D?.Z.MicroTesla:N2} (microteslas)]");

    Console.WriteLine($"Temp: {result.Temperature?.Celsius:N2}C");
}

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>>> SensorBase<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>> SamplingSensorBase<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>> ByteCommsSensorBase<System.ValueTuple<System.Nullable<MagneticField3D>, System.Nullable<Units.Temperature>>> > Mag3110
Implements IDisposable ITemperatureSensor
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() SamplingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.StartUpdating(Nullable<TimeSpan>) SamplingSensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.StopUpdating() SensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.Updated SensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.samplingLock SensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.SamplingTokenSource SensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.Conditions SensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.IsSampling SensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.UpdateInterval SensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.ReadSensor() SensorBase<(Nullable<MagneticField3D> MagneticField3D, Nullable<Units.Temperature> Temperature)>.RaiseEventsAndNotify(IChangeResult<>) SensorBase<(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)>, IDisposable, ITemperatureSensor

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

Mag3110(IMeadowDevice, II2cBus, IPin, Byte, UInt16)

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

Declaration
public Mag3110(IMeadowDevice device, II2cBus i2cBus, IPin interruptPin = null, byte address = null, ushort speed = null)

Parameters

Type Name Description
IMeadowDevice device

IO Device.

II2cBus i2cBus
IPin interruptPin

Interrupt pin used to detect end of conversions.

System.Byte address

Address of the MAG3110 (default = 0x0e).

System.UInt16 speed

Speed of the I2C bus (default = 400 KHz).

Fields

digitalInputsEnabled

Declaration
protected bool digitalInputsEnabled

Field Value

Type Description
System.Boolean

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

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

Temperature of the sensor 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)>)

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

ReadSensor()

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

Returns

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

Overrides

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

Reset()

Reset the sensor.

Declaration
public void Reset()

Events

MagneticField3dUpdated

Declaration
public event EventHandler<IChangeResult<MagneticField3D>> MagneticField3dUpdated

Event Type

Type Description
EventHandler<IChangeResult<MagneticField3D>>

TemperatureUpdated

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

Event Type

Type Description
EventHandler<IChangeResult<Units.Temperature>>