Remarks

FC-28
Status
Source code GitHub
NuGet package

FC-28 Soil Moisture Sensor is a simple breakout for measuring the moisture in soil and similar materials. The sensor has two probes and measures the resistance between them, which means this sensor is of type Resistive. Since water is conductive, as moisture in the soil increases, the resistance decreases allowing the sensor to determine soil humidity.

The biggest issue of this sensor is the corrosion of the probes, not just because it is in contact with the soil but also because there is a DC current flowing which causes electrolysis of the sensors. A work-around to prolong the life of the probes is to not constantly have the sensor powered on, but activate it every time the sensor will perform a read using a digital output port connected to the VCC pin. The code and circuit example shows you how to use it.

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

public class MeadowApp : App<F7Micro, MeadowApp>
{
    Fc28 fc28;

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

        fc28 = new Fc28(
            Device.CreateAnalogInputPort(Device.Pins.A01),
            Device.CreateDigitalOutputPort(Device.Pins.D15),
            minimumVoltageCalibration: 3.24f,
            maximumVoltageCalibration: 2.25f
        );

        TestFC28Updating();
    }

    void TestFC28Updating() 
    {
        Console.WriteLine("TestFC28Updating...");

        fc28.Subscribe(new FilterableObserver<FloatChangeResult, float>(
            h => { Console.WriteLine($"Moisture values: {Math.Truncate(h.New)}, old: {Math.Truncate(h.Old)}, delta: {h.DeltaPercent}"); },
            e => { return true; }
        ));

        fc28.Updated += (object sender, FloatChangeResult e) =>
        {
            Console.WriteLine($"Moisture Updated: {e.New}");
        };

        fc28.StartUpdating();
    }
}

Sample projects available on GitHub

Wiring Example

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

Syntax

public class Fc28 : FilterableChangeObservableBase<FloatChangeResult, float>, IMoistureSensor

Constructors

Fc28(IAnalogInputPort, IDigitalOutputPort, Single, Single)

Creates a FC28 soil moisture sensor object with the especified analog pin and digital pin.

Declaration
public Fc28(IAnalogInputPort analogPort, IDigitalOutputPort digitalPort, float minimumVoltageCalibration = 0F, float maximumVoltageCalibration = 3.3F)

Parameters

Type Name Description
IAnalogInputPort analogPort
IDigitalOutputPort digitalPort
System.Single minimumVoltageCalibration
System.Single maximumVoltageCalibration

Fc28(IIODevice, IPin, IPin, Single, Single)

Creates a FC28 soil moisture sensor object with the especified analog pin, digital pin and IO device.

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

Parameters

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

Properties

AnalogInputPort

Returns the analog input port

Declaration
public IAnalogInputPort AnalogInputPort { get; protected set; }

Property Value

Type Description
IAnalogInputPort

DigitalPort

Returns the digital output port

Declaration
public IDigitalOutputPort DigitalPort { get; protected set; }

Property Value

Type Description
IDigitalOutputPort

IsSampling

Gets a value indicating whether the sensor is currently in a sampling loop. 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.

MaximumVoltageCalibration

Voltage value of most moist soil

Declaration
public float MaximumVoltageCalibration { get; set; }

Property Value

Type Description
System.Single

MinimumVoltageCalibration

Voltage value of most dry soil

Declaration
public float MinimumVoltageCalibration { get; set; }

Property Value

Type Description
System.Single

Moisture

Last value read from the moisture sensor.

Declaration
public float Moisture { get; }

Property Value

Type Description
System.Single

Methods

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

RaiseChangedAndNotify(FloatChangeResult)

Declaration
protected void RaiseChangedAndNotify(FloatChangeResult changeResult)

Parameters

Type Name Description
FloatChangeResult changeResult

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>

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.

StopUpdating()

Stops sampling the sensor.

Declaration
public void StopUpdating()

VoltageToMoisture(Single)

Declaration
protected float VoltageToMoisture(float voltage)

Parameters

Type Name Description
System.Single voltage

Returns

Type Description
System.Single

Events

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>