Remarks

Capacitive
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Moisture.Capacitive

Capacitive Soil Moisture sensor is a simple breakout for measuring the moisture in soil and similar materials. This sensor measures moisture levels by capacitive sensing, rather then resistive sensing like other types of moisture sensor such as the FC-28.

Capacitive sensing means measuring the dielectrum that is formed by the soil and the water is the most important factor that forms the dielectrum. Even though this kind of sensor might be a little pricier, it is made of corrosion resistant material giving it a longer service of life than a resistive sensor.

The following example shows how read the soil moisture every second:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    Capacitive _Capacitive;

    public MeadowApp()
    {
        // create a new Capacitive sensor object connected to analog pin A01
        _Capacitive = new Capacitive(Device.Pins.A01);

        Run();
    }

    async Task Run()
    {
        while (true)
        {
            float moisture = await _Capacitive.Read();
            Console.WriteLine($"Moisture: {0}", moisture);
            Thread.Sleep(1000);
        }
    }
}

Sample projects available on GitHub

Code Example

Capacitive capacitive;

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

    capacitive = new Capacitive(
        Device.Pins.A00,
        minimumVoltageCalibration: new Voltage(2.84f),
        maximumVoltageCalibration: new Voltage(1.63f)
    );

    // Example that uses an IObservable subscription to only be notified when the humidity changes by filter defined.
    var consumer = Capacitive.CreateObserver(
        handler: result =>
        {
            string oldValue = (result.Old is { } old) ? $"{old:n2}" : "n/a"; // C# 8 pattern matching
            Resolver.Log.Info($"Subscribed - " +
                $"new: {result.New}, " +
                $"old: {oldValue}");
        },
        filter: null
    );
    capacitive.Subscribe(consumer);

    // classical .NET events can also be used:
    capacitive.HumidityUpdated += (sender, result) =>
    {
        string oldValue = (result.Old is { } old) ? $"{old:n2}" : "n/a"; // C# 8 pattern matching
        Resolver.Log.Info($"Updated - New: {result.New}, Old: {oldValue}");
    };

    //==== One-off reading use case/pattern
    ReadSensor().Wait();

    capacitive.StartUpdating(TimeSpan.FromMilliseconds(1000));

    return Task.CompletedTask;
}

protected async Task ReadSensor()
{
    var humidity = await capacitive.Read();
    Resolver.Log.Info($"Initial humidity: {humidity:N2}C");
}

Sample project(s) available on GitHub

Wiring Example

Characteristic Locus
Inheritance System.Object ObservableBase<System.Double> SamplingSensorBase<System.Double> > Capacitive
Implements ISamplingSensor<System.Double> IMoistureSensor
Inherited Members SamplingSensorBase<Double>.samplingLock SamplingSensorBase<Double>.Updated SamplingSensorBase<Double>.SamplingTokenSource SamplingSensorBase<Double>.Conditions SamplingSensorBase<Double>.IsSampling SamplingSensorBase<Double>.UpdateInterval SamplingSensorBase<Double>.RaiseEventsAndNotify(IChangeResult<>) SamplingSensorBase<Double>.Read() ObservableBase<Double>.observers ObservableBase<Double>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<System.Double>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<System.Double>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Moisture
Assembly Capacitive.dll

Syntax

public class Capacitive : SamplingSensorBase<double>, ISamplingSensor<double>, IMoistureSensor

Constructors

Capacitive(IAnalogInputPort, Nullable<Voltage>, Nullable<Voltage>)

Creates a Capacitive soil moisture sensor object with the especified AnalogInputPort

Declaration
public Capacitive(IAnalogInputPort analogInputPort, Voltage? minimumVoltageCalibration, Voltage? maximumVoltageCalibration)

Parameters

Type Name Description
IAnalogInputPort analogInputPort

The port for the analog input pin

System.Nullable<Voltage> minimumVoltageCalibration

Minimum calibration voltage

System.Nullable<Voltage> maximumVoltageCalibration

Maximum calibration voltage

Capacitive(IPin, Nullable<Voltage>, Nullable<Voltage>, Int32, Nullable<TimeSpan>)

Declaration
public Capacitive(IPin analogInputPin, Voltage? minimumVoltageCalibration, Voltage? maximumVoltageCalibration, int sampleCount = 5, TimeSpan? sampleInterval = null)

Parameters

Type Name Description
IPin analogInputPin
System.Nullable<Voltage> minimumVoltageCalibration
System.Nullable<Voltage> maximumVoltageCalibration
System.Int32 sampleCount
System.Nullable<TimeSpan> sampleInterval

Properties

AnalogInputPort

Returns the analog input port

Declaration
protected IAnalogInputPort AnalogInputPort { get; }

Property Value

Type Description
IAnalogInputPort

MaximumVoltageCalibration

Voltage value of most moist soil. Default of 3.3V.

Declaration
public Voltage MaximumVoltageCalibration { get; set; }

Property Value

Type Description
Voltage

MinimumVoltageCalibration

Voltage value of most dry soil. Default of 0V.

Declaration
public Voltage MinimumVoltageCalibration { get; set; }

Property Value

Type Description
Voltage

Moisture

Last value read from the moisture sensor.

Declaration
public double? Moisture { get; protected set; }

Property Value

Type Description
System.Nullable<System.Double>

Methods

RaiseEventsAndNotify(IChangeResult<Double>)

Raise change events for subscribers

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<double> changeResult)

Parameters

Type Name Description
IChangeResult<System.Double> changeResult

The change result with the current sensor data

ReadSensor()

Reads data from the sensor

Declaration
protected override Task<double> ReadSensor()

Returns

Type Description
Task<System.Double>

The latest sensor reading

Overrides

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

StartUpdating(Nullable<TimeSpan>)

Starts continuously sampling the sensor

Declaration
public override void StartUpdating(TimeSpan? updateInterval)

Parameters

Type Name Description
System.Nullable<TimeSpan> updateInterval

StopUpdating()

Stops sampling the sensor

Declaration
public override void StopUpdating()

Overrides

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

VoltageToMoisture(Voltage)

Converts voltage to moisture value, ranging from 0 (most dry) to 1 (most wet)

Declaration
protected double VoltageToMoisture(Voltage voltage)

Parameters

Type Name Description
Voltage voltage

Returns

Type Description
System.Double

Events

HumidityUpdated

Raised when a new sensor reading has been made. To enable, call StartUpdating().

Declaration
public event EventHandler<IChangeResult<double>> HumidityUpdated

Event Type

Type Description
EventHandler<IChangeResult<System.Double>>