ESP32 Plant Watering: Soil Moisture Project
In this article
Yes, you can build an ESP32-based automatic plant watering system that uses a soil moisture sensor to decide when to water. This project is popular with hobbyists because the ESP32 is cheap, has built-in Wi-Fi, and can be programmed with the Arduino IDE or MicroPython. You will need an ESP32 board, a capacitive soil moisture sensor, a relay or MOSFET, a small water pump, and a power supply. The system reads the sensor, compares the value to a threshold you set, and turns the pump on or off accordingly.
Why Use an ESP32 Instead of an Arduino Uno?
The ESP32 costs about the same as an Arduino Uno clone but includes far more features. It has dual-core 32-bit processors running at 240 MHz, 520 KB of SRAM, and built-in Wi-Fi and Bluetooth. That means your watering system can log data to a cloud service like ThingSpeak, send you a Telegram message when the soil is dry, or be controlled from a web page on your phone. An Uno would need extra shields or modules to do the same.
Another advantage is the ESP32's analog-to-digital converter (ADC) which reads the soil moisture sensor. The ADC on the ESP32 is 12-bit, giving values from 0 to 4095, compared to the Uno's 10-bit (0–1023). That gives you finer resolution for detecting small changes in moisture.
How the Soil Moisture Sensor Works
Most hobbyist soil moisture sensors fall into two types: resistive and capacitive. The resistive type (often a fork-shaped probe) measures the resistance between two exposed electrodes. Wet soil conducts more electricity, so the resistance drops. The capacitive type uses two traces on a PCB that form a capacitor; water changes the dielectric constant, which changes the capacitance. Capacitive sensors are generally more durable because the metal traces are coated, so they don't corrode as quickly.
Both types output an analog voltage that varies with moisture. You connect the sensor's analog output to an ESP32 GPIO pin (e.g., GPIO34) and read it with analogRead(). Typically, a dry reading is around 3000–3500, and a wet reading is around 1000–1500, but these values vary by sensor and soil type. You must calibrate for your specific sensor and soil.
Complete Wiring Diagram
Here is a simple wiring setup for a capacitive sensor and a submersible pump:
- ESP32 3.3V → sensor VCC
- ESP32 GND → sensor GND
- Sensor AO → ESP32 GPIO34
- ESP32 GPIO26 → relay IN (through a transistor or optocoupler if needed)
- Relay COM → pump positive wire
- Relay NO → pump negative wire (or pump power supply)
- Pump power supply → relay and pump
Important: The pump should NOT be powered from the ESP32's 3.3V or 5V pin unless it is a very small pump that draws less than 250 mA. Most submersible pumps draw 300–500 mA, so use a separate 5V or 12V supply and a relay or MOSFET to switch it. The ESP32 and the pump should share a common ground.
If you use a relay module, check if it is active-low or active-high. Many inexpensive relay modules are active-low, meaning you must set the GPIO pin LOW to turn the relay on. In your code, you can invert the logic accordingly.
Programming the ESP32
You can write the firmware in the Arduino IDE or MicroPython. Here is a basic Arduino sketch outline:
- Include the WiFi and HTTP libraries if you want remote monitoring.
- Define the sensor pin and relay pin.
- In
setup(), set the relay pin as OUTPUT and turn it off. - In
loop(), read the sensor value withanalogRead(sensorPin). - Compare the reading to a threshold (e.g., 2500). If it's above (dry), turn the relay on for a few seconds.
- Wait a day or several hours before the next check to avoid overwatering.
Here is a minimal code snippet:
const int sensorPin = 34;
const int relayPin = 26;
int threshold = 2500;
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
}
void loop() {
int moisture = analogRead(sensorPin);
if (moisture > threshold) {
digitalWrite(relayPin, HIGH); // or LOW if active-low
delay(5000); // water for 5 seconds
digitalWrite(relayPin, LOW);
}
delay(3600000); // wait 1 hour
}Note: The delay blocks the loop, so for a real project you should use millis() or a timer library to avoid blocking Wi-Fi operations.
Calibration and Threshold Setting
Calibration is essential because the sensor's absolute values vary with soil type, temperature, and sensor model. To calibrate:
- Insert the sensor into a pot of dry soil and record the reading.
- Water the soil thoroughly, wait a few minutes, and record the wet reading.
- Set your threshold somewhere between the two, for example halfway.
- Test over a few days and adjust based on plant health.
Keep in mind that the sensor reading drifts over time due to ion accumulation in the soil. Recalibrate every few months. Also, the sensor should be placed near the plant's root zone, not too close to the surface where it dries faster.
Common Problems and How to Fix Them
- Sensor reads 4095 or 0 always: Check wiring. A floating pin (not connected) gives random values. If you use a resistive sensor, it might be corroded.
- Pump doesn't turn on: Verify the relay logic (active-low vs active-high). Test the relay with a simple LED first.
- Pump runs constantly: Your threshold is too low, or the sensor is in a spot that stays dry. Move the sensor or lower the threshold.
- Wi-Fi disconnects: The ESP32's power supply may be unstable. Use a separate 5V supply for the pump and a good 3.3V regulator for the ESP32.
- Battery drains fast: If you run on batteries, use deep sleep mode. The ESP32 can wake every hour, read the sensor, water if needed, and sleep again.
Cost and Alternatives
A basic ESP32 board costs around $5–$10, a capacitive sensor $5–$10, a relay module $2–$5, and a small submersible pump $5–$10. Total is roughly $20–$35, not including a power supply and enclosure. That is cheaper than many commercial smart planters.
Alternatives include using an Arduino Uno (simpler but no Wi-Fi), a Raspberry Pi Pico W (similar to ESP32 but less community support), or a commercial system like a smart irrigation controller. For a single houseplant, you could even use a simple analog timer and a pump, but that won't adjust to actual soil moisture.
Frequently Asked Questions
Can I water multiple plants with one ESP32?
Yes, you can connect multiple soil moisture sensors to different ADC pins (e.g., GPIO34, 35, 36, 39) and control multiple pumps or a single pump with a valve manifold. In code, you'd read each sensor and water each plant separately. Just be careful not to exceed the ESP32's GPIO limits and power supply capacity.
How often should the ESP32 check the soil?
For most houseplants, checking every 1–6 hours is enough. More frequent checks (every minute) are unnecessary and waste battery. If you're using deep sleep, you can wake every hour, read the sensor, and go back to sleep. The sensor itself can be powered off between readings to save power.
Do I need a real-time clock (RTC) for scheduling?
Not necessarily. If you just want to water when dry, you don't need an RTC. But if you want to water at specific times (e.g., morning), you can get the time from the internet via NTP when the ESP32 connects to Wi-Fi. That avoids extra hardware.
What if the sensor gets wet? Is it safe?
Capacitive sensors are coated and can handle being buried in wet soil. Resistive sensors are not coated and will corrode over time. To protect the electronics, you can coat the traces with nail polish or epoxy, but that changes the readings. It's better to buy a capacitive sensor.
Can I use the ESP32's 5V pin to power the sensor?
The ESP32's 5V pin (if present on your board) is usually connected to the USB 5V input, so it can power a sensor that needs 5V. However, the sensor's analog output will then be 0–5V, which is too high for the ESP32's ADC (max 3.3V). You'll need a voltage divider or use a 3.3V sensor. Most capacitive sensors work at 3.3V, so it's simpler to power them from the 3.3V pin.