What you will build
A plant buddy that shows soil moisture as a percentage and a simple status (Thirsty / OK / Wet) on an OLED, and on a phone dashboard at http://xbot-plant.local. This is also the sensing half of Project 07, Automatic Plant Watering.
How it works
A capacitive soil sensor outputs a voltage that falls as the soil gets wetter. The ESP32 reads it on GPIO 34 (ADC1, so it keeps working with Wi-Fi on), averages 20 samples to remove noise, and converts it to 0–100 % using two calibration values you measure: the reading in dry air and the reading in a glass of water.
Parts
| Part | Qty | Purpose | Required |
|---|---|---|---|
| ESP32-DevKitC V4 + breadboard + USB cable | 1 | Controller + Wi-Fi | Yes |
| Capacitive soil moisture sensor v1.2/v2.0 | 1 | Moisture | Yes |
| 0.96" SSD1306 OLED, I²C | 1 | Display | Yes |
| Potted plant + glass of water | — | Calibration and testing | Yes |
Use a capacitive sensor, not the two-prong resistive type: resistive probes corrode within weeks.
Build steps
- Sensor: VCC → 3V3, GND → GND, AOUT → GPIO 34.
- OLED: VCC → 3V3, GND → GND, SDA → GPIO 21, SCL → GPIO 22.
- Install Adafruit SSD1306 and Adafruit GFX. Add the same
secrets.has Project 05. - Upload and open the Serial Monitor at 115200 baud. It prints the raw reading.
- Calibrate: hold the sensor in air and note the raw value →
RAW_DRY. Dip it in water up to the line (never above) →RAW_WET. Update the code and upload again. - Push the sensor into the soil up to the line, near the roots but not touching them.
- Checkpoint: water the plant and watch the percentage rise within a minute.
Try it in the browser
Playground Simulation. A virtual plant with a soil-moisture slider drives the virtual sensor; the OLED and the status word change as you drag. Labelled as simulated values, not a real measurement.
Open Simulator PlaygroundCode
Download plant-moisture-monitor.ino
// Plant Moisture Monitor — XBOTNOW
// Board: ESP32-DevKitC V4 · Core: esp32 v3.x
// Libraries: Adafruit SSD1306, Adafruit GFX
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include "secrets.h"
const int SOIL_PIN = 34;
int RAW_DRY = 3000; // calibrate: sensor in air
int RAW_WET = 1300; // calibrate: sensor in water
const int THIRSTY_BELOW = 30, WET_ABOVE = 75; // percent
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
WebServer server(80);
int raw = 0, pct = 0;
unsigned long lastRead = 0;
const char* status() { return pct < THIRSTY_BELOW ? "Thirsty" : pct > WET_ABOVE ? "Wet" : "OK"; }
int readSoil() {
long sum = 0;
for (int i = 0; i < 20; i++) { sum += analogRead(SOIL_PIN); delay(2); }
return sum / 20;
}
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(SSD1306_WHITE);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) delay(300);
MDNS.begin("xbot-plant");
Serial.printf("Dashboard: http://xbot-plant.local (%s)\n", WiFi.localIP().toString().c_str());
server.on("/data", [] {
server.send(200, "application/json",
String("{\"raw\":") + raw + ",\"pct\":" + pct + ",\"status\":\"" + status() + "\"}");
});
server.on("/", [] {
server.send(200, "text/html",
"<meta name=viewport content='width=device-width'><body style='font-family:sans-serif;text-align:center'>"
"<h2>My plant</h2><p style='font-size:3em'><span id=p>--</span>%</p><p id=s></p><script>"
"setInterval(async()=>{const d=await(await fetch('/data')).json();p.textContent=d.pct;s.textContent=d.status},3000)"
"</script>");
});
server.begin();
}
void loop() {
server.handleClient();
if (millis() - lastRead < 3000) return;
lastRead = millis();
raw = readSoil();
pct = constrain(map(raw, RAW_DRY, RAW_WET, 0, 100), 0, 100);
Serial.printf("raw=%d pct=%d %s\n", raw, pct, status());
oled.clearDisplay();
oled.setTextSize(3); oled.setCursor(0, 4); oled.printf("%d%%", pct);
oled.setTextSize(2); oled.setCursor(0, 44); oled.print(status());
oled.display();
}
Troubleshooting
| Symptom | Likely cause | Check |
|---|---|---|
| Always 0 % or 100 % | Calibration values swapped or wrong | Re-measure RAW_DRY (higher) and RAW_WET (lower) |
| Reading jumps around | Loose soil contact | Press soil firmly around the sensor |
| Reading drifts over days | Sensor edge exposed or water above the line | Keep electronics above the soil; seal the top edge with nail varnish or heat-shrink |
Safety and limitations
Keep the ESP32 and the top of the sensor dry. Readings vary with soil type; treat the percentage as relative, not lab-grade volumetric water content.
Checklist
Next project
Automatic Plant Watering System: add a pump so the plant waters itself.

