Remarks

Mpl115a2
Status Status badge: in-progress
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Atmospheric.Mpl115a2

The MPL115A2 is a low cost device for reading barometric pressure.

  • I2C digital interface (address: 0x60)
  • Resolution: 1.5 hPa
  • Range: 100-1150 hPa up to 10Km

Sample projects available on GitHub

Purchasing

The MPL115A2 sensor is available on a breakout board from Adafruit

The application below connects the MPL115A2 to two interrupt handlers. These interrupt handlers (events) will display the Temperature and Pressure properties when the handlers are triggered. The sensor is checked every 500 milliseconds.

Code Example

Mpl115a2 sensor;

public override Task Initialize()
{
    Console.WriteLine("Initializing...");

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

    var consumer = Mpl115a2.CreateObserver(
        handler: result =>
        {
            Console.WriteLine($"Observer: Temp changed by threshold; new temp: {result.New.Temperature?.Celsius:N2}C, old: {result.Old?.Temperature?.Celsius:N2}C");
        },
        filter: result =>
        {
            //c# 8 pattern match syntax. checks for !null and assigns var.
            if (result.Old?.Temperature is { } oldTemp &&
                result.New.Temperature is { } newTemp)
            {
                return (newTemp - oldTemp).Abs().Celsius > 0.5; // returns true if > 0.5°C change.
            }
            return false;
        }
    );
    sensor.Subscribe(consumer);

    sensor.Updated += (sender, result) => {
        Console.WriteLine($"  Temperature: {result.New.Temperature?.Celsius:N2}C");
        Console.WriteLine($"  Pressure: {result.New.Pressure?.Bar:N2}Bar");
    };

    return Task.CompletedTask;
}

public override async Task Run()
{
    var conditions = await sensor.Read();
    Console.WriteLine($"Temperature: {conditions.Temperature?.Celsius}°C, Pressure: {conditions.Pressure?.Pascal}Pa");

    sensor.StartUpdating(TimeSpan.FromSeconds(1));
}

Sample project(s) available on GitHub

Wiring Example

Connecting the MPL115A2 to Meadow requires four connections:

In this diagram, the shutdown (SDWN) and reset (RST) pins have been left floating. Both of these pins are active low and can be tied to Vcc in normal operation.

Note that the Adafruit breakout board has 10K pull-up resistors on the SDA and SCK lines.

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

Syntax

public class Mpl115a2 : ByteCommsSensorBase<(Units.Temperature? Temperature, Pressure? Pressure)>, IDisposable, ITemperatureSensor, IBarometricPressureSensor

Constructors

Mpl115a2(II2cBus, Byte, Int32)

Create a new MPL115A2 temperature and humidity sensor object.

Declaration
public Mpl115a2(II2cBus i2cBus, byte address = null, int updateIntervalMs = 1000)

Parameters

Type Name Description
II2cBus i2cBus

I2CBus (default to 100 KHz).

System.Byte address

Sensor address (default to 0x60).

System.Int32 updateIntervalMs

Properties

Pressure

The pressure, in hectopascals (hPa), from the last reading. 1 hPa is equal to one millibar, or 1/10th of a kilopascal (kPa)/centibar.

Declaration
public Pressure? Pressure { get; }

Property Value

Type Description
System.Nullable<Pressure>

Temperature

The temperature, in degrees celsius (°C), from the last reading.

Declaration
public Units.Temperature? Temperature { get; }

Property Value

Type Description
System.Nullable<Units.Temperature>

Methods

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

Inheritance-safe way to raise events and notify observers.

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

Parameters

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

ReadSensor()

Update the temperature and pressure from the sensor and set the Pressure property.

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

Returns

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

Overrides

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

Events

PressureUpdated

Declaration
public event EventHandler<IChangeResult<Pressure>> PressureUpdated

Event Type

Type Description
EventHandler<IChangeResult<Pressure>>

TemperatureUpdated

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

Event Type

Type Description
EventHandler<IChangeResult<Units.Temperature>>