ESP8266 - Soil Moisture Sensor

This tutorial instructs you how to use a moisture sensor with ESP8266. Specifically, we will look at:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Capacitive Soil Moisture Sensor
1×Jumper Wires
1×Breadboard
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) Screw Terminal Expansion Board for ESP8266

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Soil Moisture Sensor Sensor

capacitive moisture sensor vs resistive moisture sensor

There are two types of moisture sensors: the resistive moisture sensor and the capacitive moisture sensor.

Both sensors offer soil moisture information. However, their methods of operation differ. We suggest using the capacitive moisture sensor due to the following:

  • The resistive soil moisture sensor is prone to corrosion over time as electrical current passes between its probes, leading to electrochemical corrosion.
  • The capacitive soil moisture sensor, on the other hand, corrodes much slower than the resistive soil moisture sensor. That is because its electrodes are not exposed and is comparatively resistant to corrosion.

This is an illustration of a resistive soil moisture sensor that has been damaged by corrosion.

resistive soil moisture sensor corroded

The remainder of this tutorial will be utilizing the capacitive soil moisture sensor.

Capacitive Soil Moisture Sensor Pinout

The capacitive soil moisture sensor has three pins:

  • GND pin: This must be connected to GND (0V).
  • VCC pin: This must be connected to VCC (5V or 3.3v).
  • AOUT pin: This is the analog signal output pin which produces a voltage that is proportional to the soil moisture level. This should be connected to an analog input pin on an ESP8266.
capacitive soil moisture sensor pinout

How It Works

The amount of water present in the soil has a inverse proportion to the voltage level of the AOUT pin.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and soil moisture sensor

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

ESP8266 Code for reading value from soil moisture sensor

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-soil-moisture-sensor */ #define AOUT_PIN A0 // The ESP8266 pin ADC0 that connects to AOUT pin of moisture sensor void setup() { Serial.begin(9600); } void loop() { int value = analogRead(AOUT_PIN); // read the analog value from sensor Serial.print("Moisture: "); Serial.println(value); delay(500); }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it with Arduino IDE.
  • Click the Upload button on Arduino IDE to compile and upload the code to ESP8266.
  • Bury the sensor in soil, then pour water into the soil. Or slowly submerge it into a cup of salt water.
  • Check out the result on Serial Monitor. It looks like the below:
COM6
Send
Moisture: 581 Moisture: 581 Moisture: 575 Moisture: 566 Moisture: 556 Moisture: 547 Moisture: 539 Moisture: 530 Moisture: 521 Moisture: 513 Moisture: 506 Moisture: 500 Moisture: 495 Moisture: 492 Moisture: 490 Moisture: 489 Moisture: 488
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

  • Don't use pure water for testing because it doesn't conduct electricity, so it won't change the sensor readings.
  • The sensor readings will never be zero. It's usual for the values to be between 500 and 600, although this might change based on factors such as how deep the sensor is placed, the type of soil or water, and the voltage of the power supply.
  • Avoid burying the circuit part (found on top of the sensor) in soil or water, as it could harm the sensor.

Calibration for Capacitive Soil Moisture Sensor

The value obtained from the moisture sensor is not absolute. It is based on the soil's composition and water content. In order to accurately determine a boundary between wet and dry, we must perform calibration.

Instructions for Calibration:

  • Execute the code on ESP8266
  • Place the moisture sensor in the soil
  • Gradually add water to the soil
  • Monitor the Serial Monitor
  • Note the value when the soil changes from dry to wet. This is referred to as the THRESHOLD.

ESP8266 code determines if the soil is wet or dry

Once the calibration is done, update the THRESHOLD value that you noted down in the code below. This code will be used to decide if the soil is wet or dry.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-soil-moisture-sensor */ #define AOUT_PIN A0 // The ESP8266 pin ADC0 that connects to AOUT pin of moisture sensor #define THRESHOLD 530 // CHANGE YOUR THRESHOLD HERE void setup() { Serial.begin(9600); } void loop() { int value = analogRead(AOUT_PIN); // read the analog value from sensor if (value > THRESHOLD) Serial.print("The soil is DRY ("); else Serial.print("The soil is WET ("); Serial.print(value); Serial.println(")"); delay(500); }

The output seen on the Serial Monitor.

COM6
Send
The soil is DRY (581) The soil is DRY (575) The soil is DRY (566) The soil is DRY (547) The soil is DRY (539) The soil is WET (521) The soil is WET (513) The soil is WET (492) The soil is WET (488)
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

This tutorial uses the analogRead() function to get data from an ADC (Analog-to-Digital Converter) that's connected to a sensor or another part. The ESP8266's ADC works well for projects where you don't need very precise readings. But remember, the ESP8266's ADC isn't very accurate for detailed measurements. If your project needs to be very precise, you might want to use a separate ADC like the ADS1115 with the ESP8266, or use Arduino like the Arduino Uno R4 WiFi, which has a more reliable ADC.

Video Tutorial

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!