Characteristic Locus
Inheritance System.Object > Capacitive
Implements IMoistureSensor
Namespace Meadow.Foundation.Sensors.Moisture
Assembly Capacitive.dll

Syntax

public class Capacitive : FilterableObservableBase<FloatChangeResult, float>, IMoistureSensor

Remarks

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.

Examples

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

using System.Threading;
using Meadow;
using Meadow.Foundation.Sensors.Moisture;

namespace Capacitive_Sample
{
    public class Program
    {
        static IApp _app; 
        public static void Main()
        {
            _app = new App();
        }
    }

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

        public App ()
        {
            // 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);
            }
        }
    }
}
Example Circuit

Constructors

View Source

Capacitive(IAnalogInputPort, Single, Single)

Creates a Capacitive soil moisture sensor object with the especified AnalogInputPort.

Declaration
public Capacitive(IAnalogInputPort analogPort, float minimumVoltageCalibration = 0F, float maximumVoltageCalibration = 3.3F)

Parameters

Type Name Description
IAnalogInputPort analogPort
System.Single minimumVoltageCalibration
System.Single maximumVoltageCalibration
View Source

Capacitive(IIODevice, IPin, Single, Single)

Creates a Capacitive soil moisture sensor object with the specified analog pin and a IO device.

Declaration
public Capacitive(IIODevice device, IPin analogPin, float minimumVoltageCalibration = 0F, float maximumVoltageCalibration = 3.3F)

Parameters

Type Name Description
IIODevice device
IPin analogPin
System.Single minimumVoltageCalibration
System.Single maximumVoltageCalibration

Properties

View Source

AnalogInputPort

Returns the analog input port

Declaration
public IAnalogInputPort AnalogInputPort { get; protected set; }

Property Value

Type Description
IAnalogInputPort
View Source

IsSampling

Gets a value indicating whether the analog input port is currently sampling the ADC. Call StartSampling() to spin up the sampling process.

Declaration
public bool IsSampling { get; protected set; }

Property Value

Type Description
System.Boolean

true if sampling; otherwise, false.

View Source

MaximumVoltageCalibration

Voltage value of most moist soil

Declaration
public float MaximumVoltageCalibration { get; set; }

Property Value

Type Description
System.Single
View Source

MinimumVoltageCalibration

Voltage value of most dry soil

Declaration
public float MinimumVoltageCalibration { get; set; }

Property Value

Type Description
System.Single
View Source

Moisture

Last value read from the moisture sensor.

Declaration
public float Moisture { get; protected set; }

Property Value

Type Description
System.Single

Methods

View Source

Map(Single, Single, Single, Single, Single)

Re-maps a value from one range (fromLow - fromHigh) to another (toLow - toHigh).

Declaration
protected float Map(float value, float fromLow, float fromHigh, float toLow, float toHigh)

Parameters

Type Name Description
System.Single value
System.Single fromLow
System.Single fromHigh
System.Single toLow
System.Single toHigh

Returns

Type Description
System.Single
View Source

RaiseChangedAndNotify(FloatChangeResult)

Declaration
protected void RaiseChangedAndNotify(FloatChangeResult changeResult)

Parameters

Type Name Description
FloatChangeResult changeResult
View Source

Read(Int32, Int32)

Convenience method to get the current soil moisture. For frequent reads, use StartUpdating() and StopUpdating().

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

Parameters

Type Name Description
System.Int32 sampleCount

The number of sample readings to take. Must be greater than 0.

System.Int32 sampleInterval

The interval, in milliseconds, between sample readings.

Returns

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

StartUpdating(Int32, Int32, Int32)

Starts continuously sampling the sensor.

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

Declaration
public void StartUpdating(int sampleCount = 10, int sampleIntervalDuration = 40, int standbyDuration = 1000)

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 Updated events are raised and IObservable consumers are notified.

View Source

StopUpdating()

Stops sampling the sensor.

Declaration
public void StopUpdating()
View Source

VoltageToMoisture(Single)

Declaration
protected float VoltageToMoisture(float voltage)

Parameters

Type Name Description
System.Single voltage

Returns

Type Description
System.Single

Events

View Source

Updated

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

Declaration
public event EventHandler<FloatChangeResult> Updated

Event Type

Type Description
System.EventHandler<FloatChangeResult>