← Beginner's guide to ESP32

Build an ESP32 plant moisture monitor

Find out when your plant is thirsty. A soil sensor feeds a web page that shows moisture as a percentage, and an LED lights up when it is time to water. You will learn analog readings and, just as important, how to calibrate a sensor.

What you will learn

Before you start: do steps 1 to 5 of the beginner's guide. This project reuses the web server from the relay switch.

Max line: push the sensor in only this far Electronics stay above the line, dry Sensing area is under the line Wi-Fiweb page shows 0 to 100 %
How to place the sensor. The line near the top of the board marks the maximum depth.

Parts you need

Some links are affiliate links, so I may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. Details. Buy buttons open a store search; check the product matches before you order.

Wire it up

Board unplugged from USB while you wire.

Use the right pin for Wi-Fi. On a classic ESP32, analog pins in the "ADC2" group (GPIO 0, 2, 4, 12 to 15, 25 to 27) do not work while Wi-Fi is on. This project uses Wi-Fi, so the sensor goes on an ADC1 pin: GPIO 32 to 39. We use GPIO 34.

Red = power, black = ground, yellow = sensor signal, blue = LED (optional). Add a 220 ohm resistor in series with the LED.
Part pinConnects to (classic ESP32)
Soil sensor VCC3V3
Soil sensor GNDGND
Soil sensor AOUT (analog out)GPIO 34
LED long leg (+), through a 220 ohm resistorGPIO 4
LED short leg (-)GND
Using an ESP32-S3 or ESP32-C3?
SignalClassic ESP32ESP32-S3ESP32-C3
Sensor AOUT (ADC1)GPIO 34GPIO 1GPIO 0
LEDGPIO 4GPIO 16GPIO 10

Change SOIL_PIN and LED_PIN at the top of the sketches. These are ADC1 pins on those chips.

Analog vs digital. A digital pin only knows HIGH or LOW. An analog pin measures a voltage between 0 and 3.3V and returns a number from 0 to 4095 (12 bits). A capacitive soil sensor outputs a voltage that gets lower as the soil gets wetter.

Make it last. Seal the top edge of the sensor board (where the electronics meet the sensing area) with a coat of clear nail polish or silicone, and never let water reach the electronics above the line.

Step 1: Calibrate the sensor

Every sensor and every board reads slightly different numbers, so you measure two reference points yourself: a dry reading (sensor in air) and a wet reading (sensor in water). Then the code can turn any reading into a percentage.

  1. Upload this short sketch and open the Serial Monitor at 115200 baud. It prints the raw reading twice a second.
  2. Dry value: hold the sensor in the air, away from your hand. Write down the steady number (something like 2800 to 3200).
  3. Wet value: stand the sensor in a glass of water up to the max line. Wait for the number to settle and write it down (something like 1200 to 1500).
  4. Keep both numbers. You will type them into the main sketch.
100 %0 %raw reading (what the sensor reports) 100020003000+ WET value (in water) = 100 % DRY value (in air) = 0 % your two numbers decide this line
Calibration draws a straight line between your two readings. Anything in between becomes a percentage.

Step 2: The monitor code

  1. Open a new sketch and replace everything with the code below. Fill in your Wi-Fi details and put your dry and wet numbers in AIR_VALUE and WATER_VALUE.
  2. Upload it, open the Serial Monitor at 115200 baud and press EN. It prints the address to open.

How the code works

Test it

  1. Put the sensor in dry air: the page should show close to 0% and the LED should be on.
  2. Put it in the glass of water up to the max line: it should read close to 100% and the LED goes off.
  3. Push it into the soil of your plant up to the line. Water the plant and watch the number rise over a minute or so as the water spreads.
  4. Open the page from your phone using the address in the Serial Monitor. It refreshes every 10 seconds.

Find your plant's sweet spot. Water a plant until you are happy with it, and note the percentage. Set DRY_BELOW a little under that number so the alert tells you when it is well below its comfortable level.

Troubleshooting

ProblemWhat to try
Reading is always 4095 or always 0The signal wire is loose or on the wrong pin. Check AOUT goes to GPIO 34 and VCC to 3V3.
Readings jump aroundKeep the sensor wires short and away from the Wi-Fi antenna. The code already averages; you can raise 16 to 32 readings.
Everything reads 0% or 100%Your calibration numbers are wrong. Re-do the dry and wet readings and update AIR_VALUE and WATER_VALUE.
Reading changes when the Wi-Fi connectsNormal on some boards. Use the readings taken while Wi-Fi is connected for calibration.
Analog reading does not work only on some pinsYou used an ADC2 pin. Move the sensor to GPIO 32 to 39.
Sensor value drifts over weeksThe edge is wet. Dry it and seal the top edge, or replace it.
Wi-Fi never connectsCheck name and password. The ESP32 needs a 2.4 GHz network.

Make it better

Open this project in the Build Finder →

Where to next

You have finished the five starter projects. Browse the Build Finder for what to build next, or try the more advanced touch control panel.  ·  ← Project 4: Wi-Fi clock

This guide was written from standard Arduino and ESP32 practice. The example calibration numbers are typical, not exact: use your own measurements. It has not been built on every ESP32 variant. Not affiliated with the part makers.