Remarks
Mag3110 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
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 |
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
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>> |