ESP32 - Soil Moisture Sensor

This tutorial instructs you how to use ESP32 to read the soil moisture from sensor. In detail, we will learn:

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Capacitive Soil Moisture Sensor
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

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 in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables.

Introduction to 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

  • How to connect ESP32 and soil moisture sensor using breadboard
ESP32 soil moisture sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and soil moisture sensor

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-soil-moisture-sensor */ #define AOUT_PIN 36 // ESP32 pin GPIO36 (ADC0) that connects to AOUT pin of moisture sensor void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); } void loop() { int value = analogRead(AOUT_PIN); // read the analog value from sensor Serial.print("Moisture value: "); Serial.println(value); delay(500); }

Quick Instructions

  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to upload code to 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: 2383 Moisture value: 2395 Moisture value: 1260 Moisture value: 1325 Moisture value: 1271 Moisture value: 1261 Moisture value: 1254 Moisture value: 1241 Moisture value: 1244 Moisture value: 1380 Moisture value: 1368 Moisture value: 1279 Moisture value: 1268 Moisture value: 1259 Moisture value: 1243 Moisture value: 1265 Moisture value: 1308 Moisture value: 1487 Moisture value: 1646
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

  • Avoid using pure water for testing, as it doesn't conduct electricity, thus won't affect sensor readings.
  • The sensor readings never reach zero. It's normal for the values to range between 3100 and 2600, although this may vary depending on some factors: the depth of sensor embedding, the composition of the soil or water, voltage of power supply.
  • Do not bury the circuit part (located on top of the sensor) in soil or water, as this could potentially damage 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 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 ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-soil-moisture-sensor */ #define AOUT_PIN 36 // ESP32 pin GPIO36 (ADC0) that connects to AOUT pin of moisture sensor #define THRESHOLD 1488 // CHANGE YOUR THRESHOLD HERE void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); } 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 (2126) The soil is DRY (1841) The soil is DRY (1667) The soil is DRY (1540) The soil is WET (1481) The soil is WET (1419) The soil is WET (1357) The soil is WET (1243) The soil is WET (1224)
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

This tutorial uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a soil moisture sensor. The ESP32 ADC is good for projects that do NOT need high accuracy. However, for projects that need precise measurements, please note:

  • The ESP32 ADC is not perfectly accurate and might need calibration for correct results. Each ESP32 board can be a bit different, so you need to calibrate the ADC for each individual board.
  • Calibration can be difficult, especially for beginners, and might not always give the exact results you want.

For projects that need high precision, consider using an external ADC (e.g ADS1115) with the ESP32 or using an Arduino, which has a more reliable ADC. If you still want to calibrate the ESP32 ADC, refer to ESP32 ADC Calibration Driver

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

Function References

※ OUR MESSAGES