Soil Moisture Sensor Explained: How It Works and How to Wire It to an ESP32

A resistive soil moisture sensor (the FC-28/YL-69 style module) measures how wet or dry soil is by passing a small current between two exposed metal prongs pushed into the soil and reading how well the soil conducts it — moist soil has more dissolved minerals and conducts better (lower resistance), dry soil conducts poorly (higher resistance). The probe connects to a small control board built around an LM393 comparator, which outputs a variable analog voltage (AO) proportional to moisture level and a simple HIGH/LOW digital signal (DO) that trips at a threshold set by an onboard potentiometer. It runs on 3.3–5V, which makes it compatible with both ESP32 (3.3V logic) and Arduino (5V logic) boards without a level shifter.

How does a resistive soil moisture sensor work?

The two prongs act as electrodes. The control board runs a small voltage across them and measures the resulting current, which depends on the electrical resistance of whatever is between them. Water containing dissolved salts and minerals conducts electricity far better than dry soil particles do, so wetter soil produces lower resistance and a higher analog reading (or lower, depending on how the board's output is scaled), while dry soil produces higher resistance and the opposite. The LM393 comparator chip on the control board is what turns that continuous analog signal into a clean digital HIGH/LOW trigger, and a small onboard potentiometer sets the resistance threshold at which DO flips.

Analog output vs digital output — which should you use?

AO (analog output) gives a continuously variable voltage, which you read with analogRead() on an ADC-capable pin. This is what you want for anything that needs a moisture percentage rather than a yes/no answer — irrigation logic that scales watering duration to how dry the soil actually is, or a dashboard that logs moisture trends over time. DO (digital output) only tells you whether moisture has crossed the threshold set by the potentiometer — useful when all you need is a single trigger, like "turn the pump on when the soil crosses this line," without writing any analog-reading or calibration code at all. Most builds that want real automation, not just an on/off alarm, use AO.

How do you wire the soil moisture sensor to an ESP32?

ESP32 pin Sensor pin Signal
3.3V VCC power
GND GND ground
GPIO34 (ADC1) AO analog moisture reading
GPIO27 (digital) DO threshold trigger (optional, only if you use DO)

Four wires cover it, and you only need the DO connection at all if your code is going to use the threshold trigger. On an ESP32, use one of the ADC1 pins (GPIO32–39) for AO rather than an ADC2 pin, since ADC2 shares hardware with the WiFi radio and can return unreliable readings while WiFi is active — a real problem for a project whose entire point is reporting moisture over a network.

Why does my soil moisture sensor corrode over time?

The two exposed metal prongs are doing electrolysis every time current passes through them while they're sitting in damp soil, and that slowly oxidizes and corrodes the metal — the same electrochemical process that eats away at any metal electrode carrying current through a conductive, moist medium. Over weeks of continuous power, the prongs visibly degrade and the readings drift and become unreliable. Two practical fixes exist: only power the sensor briefly right before each reading (write HIGH to a control pin, read AO, then write LOW again) instead of leaving VCC connected permanently, or switch to a capacitive soil moisture sensor for any installation meant to run for months unattended — capacitive sensors measure moisture through a change in capacitance under an insulated PCB coating rather than bare exposed metal, so there's no exposed electrode to corrode. Compoden stocks the resistive module described in this article at ₹35 (add to cart) for short, powered-briefly readings and calibration-heavy projects, and a capacitive version for installs that need to run unattended for a long time.

Watch it built live: a real ESP32 automatic watering build

Rather than describe a generic build, we typed one real sentence into Compoden's AI build assistant on the storefront and let it do the part-picking, pricing, and explaining — unscripted, screenshotted as it happened:

"I want to build an automatic plant watering project using a soil moisture sensor and an ESP32."

Compoden AI build assistant screenshot: automatic plant watering build with soil moisture sensor, ESP32, and pump parts tray

The assistant put together a full six-part automatic watering system, not just a sensor and a board: "This build creates an automatic plant watering system. It uses a sensor to check how dry the soil is. When the soil is too dry, a small pump turns on to water your plant." For the moisture sensor itself it picked the capacitive version rather than the resistive module this article is about, and it explained why unprompted — a soil sensor needs to sit in the soil continuously to be useful for automatic watering, and continuous power through exposed metal prongs is exactly the corrosion scenario described above, so the capacitive part was the better match for a permanently-installed watering system. It also flagged the real electrical detail that matters here: the relay module driving the pump runs 5V logic while the ESP32 runs 3.3V, so the control signal between them needs care.

Watch the firmware get generated

We then asked Compoden's build companion, Soldr, to write the actual ESP32 firmware for the same kind of project — reading soil moisture and printing it to the serial monitor:

Soldr.ai firmware editor screenshot: generated ESP32 firmware for reading a soil moisture sensor

The generated sketch (targeting a generic ESP32-WROOM-32 board in this run) defines the sensor pin as an analog input, sets up a serial baud rate, and includes calibration constants for a dry reading and a wet reading — exactly the dry-air/water-cup calibration approach described above, needed to map the raw ADC value onto a usable 0–100% scale. Soldr was explicit that it hadn't verified the exact pin numbers against a specific board yet, and offered to regenerate the sketch correctly wired once you tell it which board you actually own — a useful honesty check before you wire anything up for real.

What is a soil moisture sensor actually good for?

Anything that needs to know whether soil is wet or dry without a human checking it: automatic plant watering systems that only run a pump when needed, garden or small-farm irrigation automation, indoor plant moisture alerts that message you before a plant dries out, and IoT dashboards that log soil conditions over days or weeks. Paired with an ESP32's WiFi, the same sensor that triggers a local pump can also push readings to a phone or a web dashboard.

Get everything in this build

The exact six parts Soldr picked for the automatic watering build above, at today's live price and stock — each links straight to checkout, or add the whole tray in one click.

Part Price
ESP32-C3 Super Mini Development Board ₹290 Add to cart →
Capacitive Soil Moisture Sensor V2.0 ₹55 Add to cart →
Mini Peristaltic Pump 5V DC 100ml/min ₹120 Add to cart →
5V Single Channel Relay Module ₹45 Add to cart →
5V 2A Micro-USB Power Adapter ₹140 Add to cart →
23 AWG Multi-Strand Breadboard Wire ₹10 Add to cart →
Total ₹660 Add all 6 to cart →

Prefer the classic two-prong resistive sensor this article covers instead of the capacitive version above? It's in stock at ₹35 (add to cart). Prices and stock verified live at the time this was written; Compoden's storefront always reflects the current price at checkout.

Built and Backed by Compoden

Every part above ships from Compoden's own India stock, tested for compatibility before it's carded together — not sourced individually and hoped to work. Delivery in 3–7 days across India, with COD and UPI available at checkout. If a part in this build doesn't perform as described, Compoden's support team will help you troubleshoot or replace it.

FAQ

Can I use a resistive soil moisture sensor with an ESP32?
Yes. It runs on 3.3–5V, so it works directly off the ESP32's 3.3V rail with no level shifter needed. Connect VCC and GND, then AO to any ADC1 pin (GPIO32–39) for analog readings, or DO to any digital GPIO if you only need a threshold trigger.

Why do my soil moisture readings drift over a few weeks?
The exposed metal prongs corrode from electrolysis when current runs through them while sitting in damp soil. Powering the sensor only briefly right before each reading, instead of leaving it powered continuously, slows this considerably. For a permanent installation, a capacitive sensor avoids the issue entirely since it has no exposed electrode.

Should I read the analog (AO) or digital (DO) pin?
Use AO if you want an actual moisture percentage or plan to calibrate against dry and wet readings. Use DO if all you need is a single on/off trigger at a fixed threshold, set by the onboard potentiometer, without writing any analog-reading code.

How do I calibrate the analog reading into a percentage?
Take a raw analogRead() value with the sensor in dry air, then another with the probe in a cup of water. Those two readings become your 0% and 100% endpoints, and you map any future reading between them (with map() or equivalent) to get a usable moisture percentage.

Back to blog