Remarks

AnalogWaterLevel
Status
Source code GitHub
NuGet package

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

public class MeadowApp : App<F7Micro, MeadowApp>
{
    AnalogWaterLevel analogWaterLevel;

    public MeadowApp()
    {
        Console.WriteLine("Initializing...");

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

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

        // classical .NET events can also be used:
        analogWaterLevel.Updated += (object sender, ChangeResult<float> e) => {
            Console.WriteLine($"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();
    }

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

Sample projects 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> SensorBase<System.Single> > AnalogWaterLevel
Inherited Members SensorBase<Single>.Updated SensorBase<Single>.samplingLock SensorBase<Single>.SamplingTokenSource SensorBase<Single>.Conditions SensorBase<Single>.IsSampling SensorBase<Single>.UpdateInterval SensorBase<Single>.RaiseEventsAndNotify(IChangeResult<Single>) SensorBase<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 : SensorBase<float>

Constructors

AnalogWaterLevel(IAnalogInputController, IPin, AnalogWaterLevel.Calibration, Int32, Int32, Int32)

New instance of the AnalogWaterLevel class.

Declaration
public AnalogWaterLevel(IAnalogInputController device, IPin analogPin, AnalogWaterLevel.Calibration calibration = null, int updateIntervalMs = 1000, int sampleCount = 5, int sampleIntervalMs = 40)

Parameters

Type Name Description
IAnalogInputController device

The IAnalogInputController to create the port on.

IPin analogPin

Analog pin the sensor is connected to.

AnalogWaterLevel.Calibration calibration

Calibration for the analog sensor.

System.Int32 updateIntervalMs

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.

System.Int32 sampleCount

How many samples to take during a given reading. These are automatically averaged to reduce noise.

System.Int32 sampleIntervalMs

The time, in milliseconds, to wait in between samples during a reading.

AnalogWaterLevel(IAnalogInputPort, AnalogWaterLevel.Calibration)

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

Parameters

Type Name Description
IAnalogInputPort analogInputPort
AnalogWaterLevel.Calibration calibration

Fields

sampleCount

Declaration
protected int sampleCount

Field Value

Type Description
System.Int32

sampleIntervalMs

Declaration
protected int sampleIntervalMs

Field Value

Type Description
System.Int32

Properties

AnalogInputPort

Declaration
protected IAnalogInputPort AnalogInputPort { get; }

Property Value

Type Description
IAnalogInputPort

LevelCalibration

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

Property Value

Type Description
AnalogWaterLevel.Calibration

WaterLevel

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>

A float value that's ann average value of all the samples taken.

Overrides

Meadow.Foundation.SensorBase<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 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. The default is 5 seconds.

StopUpdating()

Stops sampling the temperature.

Declaration
public void 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