Soil Moisture Sensor And Led Bar Graph Using Meadow
About the project
Use Meadow. Foundation to quickly and easily wire up a Capacitive soil moisture sensor, and graph it using an LED bar graph.
Project info
Difficulty: Easy
Platforms: Microsoft, Meadow, Wilderness Labs
Estimated time: 1 hour
License: Apache License 2.0 (Apache-2.0)
Items used in this project
Hardware components
Story
In this project we're going to learn how to use a Capacitive soil moisture sensor to show the moisture values on a LED bar graph, based on the sensor's readings. Everything you need to build this project is included in the Wilderness Labs Meadow F7 w/Hack Kit Pro. We'll see how easy is to program these peripherals using Meadow.Foundation.
Meadow.Foundation is a platform for quickly and easily building connected things using.NET on Meadow. Created by Wilderness Labs, it's completely open source and maintained by the Wilderness Labs community.
If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LED project to properly set up your development environment.
Step 1 - Assemble the circuitConnect the LED Bar Graph to Meadow with 220 ohm resistors from pins D05
to D14
and the Capacitive moisture sensor to Analog Pin A0
:
Connect a Capacitive soil moisture sensor and a LED bar graph to Meadow
Connect a Capacitive soil moisture sensor and a LED bar graph to Meadow
Step 2 - Create a Meadow Application ProjectCreate a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it MoistureMeter.
Step 3 - Add the required NuGet packagesWindows
Right-click on your MoistureMeter project and click Manage NuGet Packages. In the Browse tab, search for Meadow.Foundation.Sensors.Moisture.Capacitive and click Install to add it to your project.
Adding Meadow.Foundation.Sensors.Moisture.Capacitive NuGet Package
Adding Meadow.Foundation.Sensors.Moisture.Capacitive NuGet Package
macOS
Alt-click on your MoistureMeter project in the Solution Explorer, and click Add => Add Nuget Package to open the NuGet Package window. Search for Meadow.Foundation.Sensors.Moisture.Capacitive and click Install to add it to your project.
Adding Meadow.Foundation.Sensors.Moisture.Capacitive NuGet Package
Adding Meadow.Foundation.Sensors.Moisture.Capacitive NuGet Package
Step 4 - Calibrate soil moisture sensorBefore we use the soil moisture sensor, is important to first calibrate it by finding the edge values where the sensor is most dry (like at open air) and most wet (like dipped in water). To do that, we simply need to initialize an IAnalogInputPort
and read raw values in short time intervals. Paste the following code in your main class.
using System;using Meadow;using Meadow.Hardware;using Meadow.Devices;using System.Threading;using System.Threading.Tasks;namespace MoistureMeter{ public class AnalogReadApp : App<F7Micro, AnalogReadApp> { IAnalogInputPort analogIn; public AnalogReadApp() { analogIn = Device.CreateAnalogInputPort(Device.Pins.A00); StartReading(); } protected async Task StartReading() { float voltage; while (true) { voltage = await analogIn.Read(); Console.WriteLine("Voltage: " + voltage.ToString()); Thread.Sleep(1000); } } }}
In the code above, see how we initialize analogIn
by calling the CreateAnalogInputPort()
method passing the analog pin the sensor is connected to. In the StartReading()
method, the app goes inside an infinite loop and it will call the Read()
method every second and print it on the debug console.
When the sensor is in open air, the output console printed the following values:
Voltage: 2.842Voltage: 2.839Voltage: 2.841Voltage: 2.843Voltage: 2.835
For the minimal most dry value, lets set the value to 2.84
While dipping the sensor in a cup of water the voltage, the output console printed:
Voltage: 1.373Voltage: 1.365Voltage: 1.367Voltage: 1.372Voltage: 1.361
For the most wet value, lets set the value to 1.37
Step 5 - Write code for MoistureMeter projectNow that we have found out edge values for our moisture sensor, we can finally write the logic to our project. Copy the following code below and paste it to your MeadowApp class:
using System;using System.Threading;using System.Threading.Tasks;using Meadow;using Meadow.Devices;using Meadow.Foundation.Leds;using Meadow.Foundation.Sensors.Moisture;using Meadow.Hardware;namespace MoistureMeter{ public class MeadowApp : App<F7Micro, MeadowApp> { LedBarGraph ledBarGraph; Capacitive capacitive; public MeadowApp() { IDigitalOutputPort[] ports = { Device.CreateDigitalOutputPort(Device.Pins.D05), Device.CreateDigitalOutputPort(Device.Pins.D06), Device.CreateDigitalOutputPort(Device.Pins.D07), Device.CreateDigitalOutputPort(Device.Pins.D08), Device.CreateDigitalOutputPort(Device.Pins.D09), Device.CreateDigitalOutputPort(Device.Pins.D10), Device.CreateDigitalOutputPort(Device.Pins.D11), Device.CreateDigitalOutputPort(Device.Pins.D12), Device.CreateDigitalOutputPort(Device.Pins.D13), Device.CreateDigitalOutputPort(Device.Pins.D14) }; ledBarGraph = new LedBarGraph(ports); capacitive = new Capacitive ( analogPort: Device.CreateAnalogInputPort(Device.Pins.A00), minimumVoltageCalibration: 2.84f, maximumVoltageCalibration: 1.37f ); StartReading(); } async Task StartReading() { while (true) { float moisture = await capacitive.Read(); if (moisture > 1) moisture = 1f; else if (moisture < 0) moisture = 0f; ledBarGraph.Percentage = moisture; Console.WriteLine($"Moisture {moisture * 100}%"); Thread.Sleep(1000); } } }}
In MeadowApp's constructor, we first initialize both the LedBarGraph
passing an array of DigitalOutputPorts
from D05
to D14
and the soil moisture sensor passing the analog pin which is connected to and the minimum and maximum calibration values we found previous step.
In the StartReading()
method, the app goes inside an infinite while loop and it will activate the Capacitive
moisture sensor and it will assign the Percentage
property of the LedBarGraph
and it will light up accordingly every second.
Click the Run button in Visual Studio. It should look like to the following GIF:
Running MoistureMeter project
Running MoistureMeter project
Check out Meadow.Foundation!This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.
- It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
- The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.
Leave your feedback...