Skip to main content

Meadow.Foundation.Sensors.Moisture.Capacitive

Capacitive
StatusStatus badge: working
Source codeGitHub
NuGet packageNuGet 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 moisture changes by filter defined.
var consumer = Capacitive.CreateObserver(
handler: result =>
{
string oldValue = (result.Old is { } old) ? $"{old:n2}" : "n/a";
Resolver.Log.Info($"Subscribed - " +
$"new: {result.New}, " +
$"old: {oldValue}");
},
filter: null
);
capacitive.Subscribe(consumer);

// classical .NET events can also be used:
capacitive.MoistureUpdated += (sender, result) =>
{
string oldValue = (result.Old is { } old) ? $"{old:n2}" : "n/a";
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 moisture = await capacitive.Read();
Resolver.Log.Info($"Initial moisture: {moisture:n2}");
}

Sample project(s) available on GitHub

Wiring Example

Class Capacitive

Capacitive Soil Moisture Sensor

Assembly: Capacitive.dll
View Source
Declaration
public class Capacitive : SamplingSensorBase<double>, IObservable<IChangeResult<double>>, IMoistureSensor, ISamplingSensor<double>, ISensor<double>, ISensor, ISamplingSensor, IDisposable

Inheritance: System.Object -> Meadow.Foundation.ObservableBase<UNIT>

Implements:
System.IObservable<Meadow.IChangeResult<System.Double>>, Meadow.Peripherals.Sensors.Moisture.IMoistureSensor, Meadow.Peripherals.Sensors.ISamplingSensor<System.Double>, Meadow.Peripherals.Sensors.ISensor<System.Double>, Meadow.Peripherals.Sensors.ISensor, Meadow.Peripherals.Sensors.ISamplingSensor, System.IDisposable

Properties

AnalogInputPort

Returns the analog input port

View Source
Declaration
protected IAnalogInputPort AnalogInputPort { get; }

Moisture

Last value read from the moisture sensor

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

MinimumVoltageCalibration

Voltage value of most dry soil - default is 0 volts

View Source
Declaration
public Voltage MinimumVoltageCalibration { get; set; }

MaximumVoltageCalibration

Voltage value of most moist soil - default of 3.3V

View Source
Declaration
public Voltage MaximumVoltageCalibration { get; set; }

IsDisposed

Is the object disposed

View Source
Declaration
public bool IsDisposed { get; }

Methods

ReadSensor()

Reads data from the sensor

View Source
Declaration
protected override Task<double> ReadSensor()
Returns

System.Threading.Tasks.Task<System.Double>: The latest sensor reading### StartUpdating(TimeSpan?) Starts continuously sampling the sensor

View Source
Declaration
public override void StartUpdating(TimeSpan? updateInterval)
Parameters
TypeName
System.Nullable<System.TimeSpan>updateInterval

StopUpdating()

Stops sampling the sensor

View Source
Declaration
public override void StopUpdating()

VoltageToMoisture(Voltage)

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

View Source
Declaration
protected double VoltageToMoisture(Voltage voltage)
Returns

System.Double

Parameters
TypeName
Meadow.Units.Voltagevoltage

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

View Source
Declaration
public void Dispose()

Dispose(bool)

Dispose of the object

View Source
Declaration
protected virtual void Dispose(bool disposing)
Parameters
TypeNameDescription
System.BooleandisposingIs disposing

Implements

  • System.IObservable<Meadow.IChangeResult<System.Double>>
  • Meadow.Peripherals.Sensors.Moisture.IMoistureSensor
  • Meadow.Peripherals.Sensors.ISamplingSensor<System.Double>
  • Meadow.Peripherals.Sensors.ISensor<System.Double>
  • Meadow.Peripherals.Sensors.ISensor
  • Meadow.Peripherals.Sensors.ISamplingSensor
  • System.IDisposable