Remarks

AnalogWaterLevel
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

The analog water level sensor will output a voltage based on the amount of conductivity detected across the sensor area. Most sensors will jump to a signal of about ~1V as soon as the sensor area touches water and the voltage increases linearly as the depth increases.

Be careful to limit the water contact to only the the sensor area. Do not submerge the water level sensor.

Code Example

AnalogWaterLevel analogWaterLevel;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing...");

    // configure our AnalogWaterLevel sensor
    analogWaterLevel = new AnalogWaterLevel(
        analogPin: Device.Pins.A00
    );

    // Example that uses an IObservable subscription to only be notified
    // when the level changes by at least 0.1cm
    analogWaterLevel.Subscribe(AnalogWaterLevel.CreateObserver(
        h => Resolver.Log.Info($"Water level changed by 10 mm; new: {h.New}, old: {h.Old}"),
        null //e => { return Math.Abs(e.Delta) > 0.1f; }
    ));

    // classical .NET events can also be used:
    analogWaterLevel.Updated += (object sender, IChangeResult<float> e) => {
        Resolver.Log.Info($"Level Changed, level: {e.New}cm");
    };

    // Get an initial reading.
    ReadLevel().Wait();

    // Spin up the sampling thread so that events are raised and IObservable notifications are sent.
    analogWaterLevel.StartUpdating(TimeSpan.FromSeconds(5));

    return Task.CompletedTask;
}

protected async Task ReadLevel()
{
    var conditions = await analogWaterLevel.Read();
    Resolver.Log.Info($"Initial level: { conditions }");
}

Sample project(s) available on GitHub

Wiring Example

To wire a AnalogWaterLevel to your Meadow board, connect the following:

AnalogWaterLevel Meadow Pin
GND GND
IN A00
VCC 3.3V
Characteristic Locus
Inheritance System.Object ObservableBase<System.Single> SamplingSensorBase<System.Single> > AnalogWaterLevel
Implements ISamplingSensor<System.Single>
Inherited Members SamplingSensorBase<Single>.samplingLock SamplingSensorBase<Single>.Updated SamplingSensorBase<Single>.SamplingTokenSource SamplingSensorBase<Single>.Conditions SamplingSensorBase<Single>.IsSampling SamplingSensorBase<Single>.UpdateInterval SamplingSensorBase<Single>.RaiseEventsAndNotify(IChangeResult<Single>) SamplingSensorBase<Single>.Read() ObservableBase<Single>.observers ObservableBase<Single>.NotifyObservers(IChangeResult<Single>) ObservableBase<Single>.Subscribe(IObserver<IChangeResult<Single>>) ObservableBase<Single>.CreateObserver(Action<IChangeResult<Single>>, Nullable<Predicate<IChangeResult<Single>>>)
Namespace Meadow.Foundation.Sensors.Environmental
Assembly Meadow.Foundation.dll

Syntax

public class AnalogWaterLevel : SamplingSensorBase<float>, ISamplingSensor<float>

Constructors

AnalogWaterLevel(IAnalogInputPort, AnalogWaterLevel.Calibration)

New instance of the AnalogWaterLevel class.

Declaration
public AnalogWaterLevel(IAnalogInputPort analogInputPort, AnalogWaterLevel.Calibration calibration = null)

Parameters

Type Name Description
IAnalogInputPort analogInputPort

Analog port the sensor is connected to.

AnalogWaterLevel.Calibration calibration

Calibration for the analog sensor.

AnalogWaterLevel(IPin, AnalogWaterLevel.Calibration, Nullable<TimeSpan>)

New instance of the AnalogWaterLevel class.

Declaration
public AnalogWaterLevel(IPin analogPin, AnalogWaterLevel.Calibration calibration = null, TimeSpan? updateInterval = null)

Parameters

Type Name Description
IPin analogPin

Analog pin the sensor is connected to.

AnalogWaterLevel.Calibration calibration

Calibration for the analog sensor.

System.Nullable<TimeSpan> updateInterval

The time, in milliseconds, to wait between sets of sample readings. This value determines how often Changed events are raised and IObservable consumers are notified.

Properties

AnalogInputPort

AnalogInputPort connected to temperature sensor

Declaration
protected IAnalogInputPort AnalogInputPort { get; }

Property Value

Type Description
IAnalogInputPort

LevelCalibration

Calibration of water level

Declaration
public AnalogWaterLevel.Calibration LevelCalibration { get; protected set; }

Property Value

Type Description
AnalogWaterLevel.Calibration

WaterLevel

Water level

Declaration
public float WaterLevel { get; protected set; }

Property Value

Type Description
System.Single

Methods

ReadSensor()

Convenience method to get the current water level. For frequent reads, use StartSampling() and StopSampling() in conjunction with the SampleBuffer.

Declaration
protected override Task<float> ReadSensor()

Returns

Type Description
Task<System.Single>

Overrides

Meadow.Foundation.SamplingSensorBase<System.Single>.ReadSensor()

StartUpdating(Nullable<TimeSpan>)

Starts continuously sampling the sensor

This method also starts raising Changed events and IObservable subscribers getting notified. Use the readIntervalDuration parameter to specify how often events and notifications are raised/sent.

Declaration
public override void StartUpdating(TimeSpan? updateInterval)

Parameters

Type Name Description
System.Nullable<TimeSpan> updateInterval

A TimeSpan that specifies how long to wait between readings. This value influences how often *Updated events are raised and IObservable consumers are notified.

Overrides

Meadow.Foundation.SamplingSensorBase<System.Single>.StartUpdating(System.Nullable<TimeSpan>)

StopUpdating()

Stops sampling the water level

Declaration
public override void StopUpdating()

Overrides

Meadow.Foundation.SamplingSensorBase<System.Single>.StopUpdating()

VoltageToWaterLevel(Voltage)

Converts a voltage value to a level in centimeters, based on the current calibration values.

Declaration
protected float VoltageToWaterLevel(Voltage voltage)

Parameters

Type Name Description
Voltage voltage

Returns

Type Description
System.Single