Arduino Nano ESP32 - Soil Moisture Sensor

This tutorial provides instructions on how to use Arduino Nano ESP32 to read the soil moisture from sensor. In detail, we will learn:

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Capacitive Soil Moisture Sensor
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Adapter for Arduino Nano

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. We appreciate your support.

Buy Note: Many soil moisture sensors available in the market are unreliable, regardless of their version. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of Soil Moisture Sensor Sensor

capacitive moisture sensor vs resistive moisture sensor

There are two types of moisture sensors:

  • Resistive moisture sensor
  • Rapacitive moisture sensor.

Both sensors output the soil moisture value. However, their working principles are different. We highly recommend using the capacitive moisture sensor, because of the following reasons:

  • The resistive soil moisture sensor corrodes over time. The electrical current that flows between the sensor's probes causes electrochemical corrosion.
  • The capacitive soil moisture sensor does NOT corrode over time. The sensor's electrodes are not exposed and no electrical current folows between them.

The below image shows the corrosion on a resistive soil moisture sensor.

resistive soil moisture sensor corrosion

The rest of this tutorial uses the capacitive soil moisture sensor.

Capacitive Soil Moisture Sensor Pinout

A capacitive soil moisture sensor has three pins:

  • GND pin: connect this pin to GND (0V)
  • VCC pin: connect this pin to VCC (5V or 3.3v)
  • AOUT pin: Analog signal output pin outputs the voltage in inverse proportion to the soil moisture level. Connect this pin to an ESP32's analog input pin.
capacitive soil moisture sensor pinout

How It Works

The more the water presents in the soil, the lower the voltage in the AOUT pin is

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and soil moisture sensor

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-soil-moisture-sensor */ #define AOUT_PIN A6 // Arduino Nano ESP32 pin A6 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 value: "); Serial.println(value); delay(500); }

Detailed Instructions

  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino Nano ESP32 board
  • 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 value: 3126 Moisture value: 3041 Moisture value: 2967 Moisture value: 2840 Moisture value: 2791 Moisture value: 2719 Moisture value: 2657 Moisture value: 2656 Moisture value: 2643 Moisture value: 2634 Moisture value: 2642 Moisture value: 2624
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

  • Do not use pure water for testing because it doesn't conduct electricity, so it won't impact sensor readings.
  • Sensor readings never drop to zero. It's normal for values to be between 3100 and 2600, although this may change due to factors such as the depth of sensor placement, soil or water composition, and power supply voltage.
  • Avoid burying the circuit part (located on top of the sensor) in soil or water, as this could potentially harm the sensor.

Calibration for Capacitive Soil Moisture Sensor

The measured value from the moisture sensor is relative. It depends on the soil's composition and water. In practice, we need to do calibration to determine a threshold that is a border between wet and dry.

How to do calibration:

  • Run the above code on Arduino Nano ESP32
  • Plant the moisture sensor into the soil
  • Plant the moisture sensor into the soil
  • Irrigate the soil slowly
  • Watch Serial Monitor.
  • Write down a value at the time you feel that the soil changes its moisture from dry to wet. This value is called THRESHOLD.

Determine if the soil is wet or dry

After the calibration, update the THRESHOLD value you wrote down to the below code. The below code determines if the soil is wet or dry

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-soil-moisture-sensor */ #define AOUT_PIN A6 // Arduino Nano ESP32 pin A6 that connects to AOUT pin of moisture sensor #define THRESHOLD 2800 // 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 result on Serial Monitor.

COM6
Send
The soil is DRY (3126) The soil is DRY (3041) The soil is DRY (2967) The soil is DRY (2840) The soil is WET (2791) The soil is WET (2719) The soil is WET (2657) The soil is WET (2643) The soil is WET (2624)
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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!