Meadow.Foundation.Sensors.Motion.Mag3110
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 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)
{
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($"Magnetic 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 |
Class Mag3110
Represents the Xtrinsic MAG3110 Three-Axis, Digital Magnetometer
Assembly: MAG3110.dll
View Source
public class Mag3110 : ByteCommsSensorBase<(MagneticField3D? MagneticField3D, Temperature? Temperature)>, IObservable<IChangeResult<(MagneticField3D? MagneticField3D, Temperature? Temperature)>>, ISamplingSensor<(MagneticField3D? MagneticField3D, Temperature? Temperature)>, ISensor<(MagneticField3D? MagneticField3D, Temperature? Temperature)>, IDisposable, ITemperatureSensor, ISamplingSensor<Temperature>, ISensor<Temperature>, IMagnetometer, ISamplingSensor<MagneticField3D>, ISensor<MagneticField3D>, ISensor, ISamplingSensor, II2cPeripheral
Inheritance: System.Object
-> Meadow.Foundation.ObservableBase<UNIT>
Implements:
Expand
System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.MagneticField3D>,System.Nullable<Meadow.Units.Temperature>>>>
, Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.MagneticField3D>,System.Nullable<Meadow.Units.Temperature>>>
, Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.MagneticField3D>,System.Nullable<Meadow.Units.Temperature>>>
, System.IDisposable
, Meadow.Peripherals.Sensors.ITemperatureSensor
, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Temperature>
, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Temperature>
, Meadow.Peripherals.Sensors.Motion.IMagnetometer
, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.MagneticField3D>
, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.MagneticField3D>
, Meadow.Peripherals.Sensors.ISensor
, Meadow.Peripherals.Sensors.ISamplingSensor
, Meadow.Hardware.II2cPeripheral
Properties
DefaultI2cAddress
The default I2C address for the peripheral
View Source
public byte DefaultI2cAddress { get; }
MagneticField3D
The current magnetic field value
View Source
public MagneticField3D? MagneticField3D { get; }
Temperature
Current temperature of the die
View Source
public Temperature? Temperature { get; }
Standby
Change or get the standby status of the sensor
View Source
public bool Standby { get; set; }
IsDataReady
Indicate if there is any data ready for reading (x, y or z).
View Source
public bool IsDataReady { get; }
DigitalInputsEnabled
Enable or disable interrupts.
View Source
public bool DigitalInputsEnabled { get; set; }
Fields
interruptPort
Interrupt port used to detect then end of a conversion
View Source
protected readonly IDigitalInterruptPort? interruptPort
Methods
Reset()
Reset the sensor
View Source
public void Reset()
RaiseEventsAndNotify(IChangeResult<(MagneticField3D? MagneticField3D, Temperature? Temperature)>)
Raise events for subscribers and notify of value changes
View Source
protected override void RaiseEventsAndNotify(IChangeResult<(MagneticField3D? MagneticField3D, Temperature? Temperature)> changeResult)
Parameters
Type | Name | Description |
---|---|---|
Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.MagneticField3D>,System.Nullable<Meadow.Units.Temperature>>> | changeResult | The updated sensor data |
ReadSensor()
Reads data from the sensor
View Source
protected override Task<(MagneticField3D? MagneticField3D, Temperature? Temperature)> ReadSensor()
Returns
System.Threading.Tasks.Task<System.ValueTuple<System.Nullable<Meadow.Units.MagneticField3D>,System.Nullable<Meadow.Units.Temperature>>>
: The latest sensor reading
Implements
System.IObservable<Meadow.IChangeResult<System.ValueTuple<System.Nullable<Meadow.Units.MagneticField3D>,System.Nullable<Meadow.Units.Temperature>>>>
Meadow.Peripherals.Sensors.ISamplingSensor<System.ValueTuple<System.Nullable<Meadow.Units.MagneticField3D>,System.Nullable<Meadow.Units.Temperature>>>
Meadow.Peripherals.Sensors.ISensor<System.ValueTuple<System.Nullable<Meadow.Units.MagneticField3D>,System.Nullable<Meadow.Units.Temperature>>>
System.IDisposable
Meadow.Peripherals.Sensors.ITemperatureSensor
Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Temperature>
Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Temperature>
Meadow.Peripherals.Sensors.Motion.IMagnetometer
Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.MagneticField3D>
Meadow.Peripherals.Sensors.ISensor<Meadow.Units.MagneticField3D>
Meadow.Peripherals.Sensors.ISensor
Meadow.Peripherals.Sensors.ISamplingSensor
Meadow.Hardware.II2cPeripheral