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()
{
    Console.WriteLine("Initialize...");

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

    return Task.CompletedTask;
}

public async override Task Run()
{
    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");

    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 > Mag3110
Implements ITemperatureSensor IMagnetometer
Namespace Meadow.Foundation.Sensors.Motion
Assembly MAG3110.dll

Syntax

public class Mag3110 : ByteCommsSensorBase<(MagneticField3D? MagneticField3D, Units.Temperature? Temperature)>, 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)

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

The I2C bus

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

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

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