Soil Moisture Sensor Interfacing with NodeMCU

Soil Moisture Sensor Interfacing with NodeMCU

Introduction

Soil Moisture
Soil Moisture Sensor
Soil moisture is basically the content of water present in soil. This can be measured using a soil moisture sensor which consists of two conducting probes that act as a probe. It can measure the moisture content in the soil based on the change in resistance between the two conducting plates.
The resistance between the two conducting plates varies in an inverse manner with the amount of moisture present in the soil.
For more information about soil moisture sensor and how to use it, refer the topic Soil Moisture Sensor in the sensors and modules section.

Interfacing Diagram

Soil Moisture Sensor Interfacing with NodeMCU
Soil Moisture Sensor Interfacing with NodeMCU

Example

Measuring soil moisture in terms of percentage.

Here, the analog output of soil moisture sensor is processed using ADC. The moisture content in terms of percentage is displayed on the serial monitor.
The output of the soil moisture sensor changes in the range of ADC value from 0 to 1023. This can be represented as moisture value in terms of percentage using formula given below.
Analog Output = ADC Value / 1023
Moisture in percentage = 100 – (Analog output * 100)
For zero moisture, we get maximum value of 10-bit ADC, i.e. 1023. This in turn gives ~0% moisture.
NodeMCU ADC can be used to measure analog voltage from soil moisture sensor. To know about ADC of NodeMCU refer NodeMCU ADC with ESPlorer IDE and NodeMCU ADC with Arduino IDE.
We can write codes for NodeMCU Dev Kit in either Lua Script or C/C++ language. We are using ESPlorer IDE for writing Lua scripts and Arduino IDE for writing code in C/C++. To know more refer Getting started with NodeMCU using ESPlorer IDE (which uses Lua scripting for NodeMCU) and Getting started with NodeMCU using Arduino IDE (which uses C language based Arduino sketches for NodeMCU).

Let’s write Lua script for NodeMCU to measure Soil Moisture using ADC on it.

Lua Script for Soil Moisture

sensor_pin = 0;  --Connect Soil moisture analog sensor pin to A0 of NodeMCU

while true do
  local moisture_percentage = ( 100.00 - ( (adc.read(sensor_pin)/1023.00) * 100.00 ) )
  print(string.format("Soil Moisture(in Percentage) = %0.4g",moisture_percentage),"%")
  tmr.delay(100000);
end

ESPlorer Serial Output Window

ESPlorer Serial monitor output window for Soil Moisture
ESPlorer Serial Output window

Now let’s write program of Soil Moisture for NodeMCU using Arduino IDE

Arduino Sketch for Soil Moisture

const int sensor_pin = A0;  /* Connect Soil moisture analog sensor pin to A0 of NodeMCU */

void setup() {
  Serial.begin(9600); /* Define baud rate for serial communication */
}

void loop() {
  float moisture_percentage;

  moisture_percentage = ( 100.00 - ( (analogRead(sensor_pin)/1023.00) * 100.00 ) );

  Serial.print("Soil Moisture(in Percentage) = ");
  Serial.print(moisture_percentage);
  Serial.println("%");

  delay(1000);
}

Arduino Serial Output Window

Arduino Serial monitor output window for Soil Moisture
Arduino Serial Output Window


Supporting Files
Source Code

No comments:

Post a Comment