ESP32 Automatic Plant Watering with Blynk and a Soil Moisture Sensor
This project reads a soil-moisture sensor with an ESP32 and provides visibility through Blynk. It uses a local threshold rule to decide when watering is needed, while the dashboard shows moisture and pump state. Keeping the core watering logic on the ESP32 means it can continue the local rule during a temporary cloud outage.
Soil sensors vary widely. Capacitive modules, resistive probes and boards with different output circuits can produce different raw readings and have different corrosion behavior. Calibrate your exact sensor in the actual growing medium before treating an ADC number as a percentage.
Hardware and safety
Use an ESP32, a soil-moisture sensor with an output compatible with the ESP32 ADC, a suitable DC pump, a correctly rated MOSFET/driver or relay interface, an external pump supply and appropriate tubing/reservoir. Never power a pump directly from an ESP32 GPIO. Add inductive-load protection as required by the driver design, keep water away from exposed electronics, and choose a supply rated for the pump's startup current.
Check the sensor output voltage before connecting it to an ESP32 analog input. A 5 V sensor output can exceed the ESP32 input limit; use an appropriate 3.3 V supply or level conditioning. If the pump uses a separate supply, follow the driver/module wiring instructions and grounding requirements.
1. Calibrate the sensor instead of guessing the percentage
Record the raw ADC value from your sensor in two repeatable conditions: relatively dry soil and thoroughly watered soil after excess water drains. Call these DRY_RAW and WET_RAW. Many capacitive probes produce a lower ADC count when wetter, but this is not universal—measure your actual module.
A percentage calculated from those two points is only a rough relative moisture indicator. Soil type, compaction, temperature, salinity, probe placement and sensor aging all affect it. Don't present the percentage as a scientifically calibrated volumetric water-content measurement unless you have calibrated against an appropriate reference method.
2. Configure Blynk datastreams
Create datastreams for V1 moisture percentage, V2 pump state, V0 automatic/manual mode, and V3 manual pump request. Bind dashboard widgets to those channels and label their meanings clearly. The mode switch determines who owns the pump request; the manual control is used only in manual mode. Dashboard widgets should not be treated as proof that the physical pump is running.
Treat the device auth token like a password. Confirm current Blynk account features and event/retention limits in official documentation before promising a specific plan entitlement.
3. Firmware pattern with hysteresis and a run-time limit
The sketch below illustrates a simple automatic rule. Replace template/device credentials, calibrate DRY_RAW and WET_RAW, verify the pump driver polarity and adjust the threshold to the plant and soil. It deliberately turns the pump off after a maximum continuous run. A production design should also handle sensor disconnection, reservoir empty detection and a minimum interval between watering cycles.
The code's map() function assumes DRY_RAW and WET_RAW are the endpoints you measured; verify that your probe's wet/dry direction matches them. The run-time limit reduces the chance of a continuous pump command but is not a complete dry-run or overflow safeguard. Add independent water-level sensing and a physical maximum-run protection if the application requires it.
Offline behavior and troubleshooting
The watering rule runs locally but this example uses Blynk calls in a simple teaching sketch; it is not a fully hardened production controller. Decide how reconnecting to Blynk should affect mode and manual requests, and avoid restoring cloud state blindly. Local watering should have maximum-run limits, cool-down intervals and safe behavior if the sensor is disconnected.
Percentage seems reversed: remeasure DRY_RAW/WET_RAW and fix the mapping. Pump won't run: test sensor thresholds, driver polarity and separate supply with the pump disconnected from water hazards. Pump cycles rapidly: widen the hysteresis gap and sample at a sensible interval. Pump runs too long: verify the time limit and add independent hardware/process protections where necessary.
Next steps
- Blynk 2.0 Complete Guide for ESP32 and ESP8266
- ESP32 MQTT Plant Watering System
- Blynk Relay Control with ESP32: Safe Boot and State Synchronization
Official reference: Blynk documentation. Calibrate the sensor and validate pump behavior in a supervised low-voltage test before leaving the system unattended.