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(new FilterableChangeObserver<FloatChangeResult, float>(
            h => Console.WriteLine($"Water level changed by 10 mm; new: {h.New}, old: {h.Old}"),
            e => { return Math.Abs(e.Delta) > 0.1f; }
        ));

        // classical .NET events can also be used:
        analogWaterLevel.Updated += (object sender, FloatChangeResult 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 FilterableChangeObservableBase<FloatChangeResult, System.Single> > AnalogWaterLevel
Implements System.IObservable<FloatChangeResult>
Inherited Members FilterableChangeObservableBase<FloatChangeResult, Single>._observers FilterableChangeObservableBase<FloatChangeResult, Single>.NotifyObservers(FloatChangeResult) FilterableChangeObservableBase<FloatChangeResult, Single>.Subscribe(IObserver<FloatChangeResult>) System.Object.ToString() System.Object.Equals(System.Object) System.Object.Equals(System.Object, System.Object) System.Object.ReferenceEquals(System.Object, System.Object) System.Object.GetHashCode() System.Object.GetType() System.Object.MemberwiseClone()
Namespace Meadow.Foundation.Sensors.Environmental
Assembly Meadow.Foundation.dll

Syntax

public class AnalogWaterLevel : FilterableChangeObservableBase<FloatChangeResult, float>, IObservable<FloatChangeResult>

Constructors

AnalogWaterLevel(IAnalogInputPort, AnalogWaterLevel.Calibration)

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

Parameters

Type Name Description
IAnalogInputPort analogInputPort
AnalogWaterLevel.Calibration calibration

AnalogWaterLevel(IIODevice, IPin, AnalogWaterLevel.Calibration)

New instance of the AnalogWaterLevel class.

Declaration
public AnalogWaterLevel(IIODevice device, IPin analogPin, AnalogWaterLevel.Calibration calibration = null)

Parameters

Type Name Description
IIODevice device
IPin analogPin

Analog pin the temperature sensor is connected to.

AnalogWaterLevel.Calibration calibration

Calibration for the analog temperature sensor. Only used if sensorType is set to Custom.

Properties

AnalogInputPort

Declaration
public IAnalogInputPort AnalogInputPort { get; protected set; }

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

RaiseEventsAndNotify(FloatChangeResult)

Declaration
protected void RaiseEventsAndNotify(FloatChangeResult changeResult)

Parameters

Type Name Description
FloatChangeResult changeResult

Read(Int32, Int32)

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

Declaration
public Task<float> Read(int sampleCount = 10, int sampleIntervalDuration = 40)

Parameters

Type Name Description
System.Int32 sampleCount

The number of sample readings to take. Must be greater than 0. These samples are automatically averaged.

System.Int32 sampleIntervalDuration

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

Returns

Type Description
System.Threading.Tasks.Task<System.Single>

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

StartUpdating(Int32, Int32, Int32)

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(int sampleCount = 10, int sampleIntervalDuration = 40, int standbyDuration = 100)

Parameters

Type Name Description
System.Int32 sampleCount

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

System.Int32 sampleIntervalDuration

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

System.Int32 standbyDuration

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.

StopUpdating()

Stops sampling the temperature.

Declaration
public void StopUpdating()

VoltageToWaterLevel(Single)

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

Declaration
protected float VoltageToWaterLevel(float voltage)

Parameters

Type Name Description
System.Single voltage

Returns

Type Description
System.Single

Events

Updated

Raised when the value of the reading changes.

Declaration
public event EventHandler<FloatChangeResult> Updated

Event Type

Type Description
System.EventHandler<FloatChangeResult>