What you will learn
- The difference between a digital pin (on or off) and an analog pin (a range of values).
- Why raw sensor numbers mean nothing until you calibrate them.
- Why some ESP32 pins stop working when Wi-Fi is on (and how to avoid the trap).
Before you start: do steps 1 to 5 of the beginner's guide. This project reuses the web server from the relay switch.
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.
| Part pin | Connects to (classic ESP32) |
|---|---|
| Soil sensor VCC | 3V3 |
| Soil sensor GND | GND |
| Soil sensor AOUT (analog out) | GPIO 34 |
| LED long leg (+), through a 220 ohm resistor | GPIO 4 |
| LED short leg (-) | GND |
Using an ESP32-S3 or ESP32-C3?
| Signal | Classic ESP32 | ESP32-S3 | ESP32-C3 |
|---|---|---|---|
| Sensor AOUT (ADC1) | GPIO 34 | GPIO 1 | GPIO 0 |
| LED | GPIO 4 | GPIO 16 | GPIO 10 |
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.
- Upload this short sketch and open the Serial Monitor at 115200 baud. It prints the raw reading twice a second.
- Dry value: hold the sensor in the air, away from your hand. Write down the steady number (something like 2800 to 3200).
- 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).
- Keep both numbers. You will type them into the main sketch.
Step 2: The monitor code
- 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_VALUEandWATER_VALUE. - Upload it, open the Serial Monitor at 115200 baud and press EN. It prints the address to open.
How the code works
analogRead()returns 0 to 4095. We take 16 readings and average them, because a single analog reading on the ESP32 is noisy.map(raw, AIR_VALUE, WATER_VALUE, 0, 100)rescales your two calibration points to 0 and 100.constrain()keeps the answer within 0 to 100 if the sensor reads outside your calibration.- The status text and bar colour come from simple
ifchecks againstDRY_BELOWandWET_ABOVE. Adjust those two numbers for the plant: cacti like it much drier than ferns. - The LED check runs every two seconds using
millis(), so the web server stays responsive.
Test it
- Put the sensor in dry air: the page should show close to 0% and the LED should be on.
- Put it in the glass of water up to the max line: it should read close to 100% and the LED goes off.
- 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.
- 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
| Problem | What to try |
|---|---|
| Reading is always 4095 or always 0 | The signal wire is loose or on the wrong pin. Check AOUT goes to GPIO 34 and VCC to 3V3. |
| Readings jump around | Keep 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 connects | Normal on some boards. Use the readings taken while Wi-Fi is connected for calibration. |
| Analog reading does not work only on some pins | You used an ADC2 pin. Move the sensor to GPIO 32 to 39. |
| Sensor value drifts over weeks | The edge is wet. Dry it and seal the top edge, or replace it. |
| Wi-Fi never connects | Check name and password. The ESP32 needs a 2.4 GHz network. |
Make it better
- Add the OLED from project 4 to show the percentage next to the plant.
- Send a phone notification when it turns dry (services like ntfy.sh or IFTTT).
- Add a small pump and relay (see project 2) to water the plant automatically.
- Run it on a battery with deep sleep, waking every hour to check.
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