Blog about Web Programming and Electronics
💬 👤
back Back to blog
Arduino & Electronics

How to Build Smart Garden Watering with ESP32 and WiFi

Automatic garden or houseplant watering is one of the most popular and practical projects with the ESP32 microcontroller. By using a soil moisture sensor and controlling a water pump via a relay, we can provide plants with the exact amount of water only when they really need it. In this article, we will look at the basic concept of operation, the required equipment, and a simple component connection.

Required Components for the Basic Project

To build a working prototype of an automatic watering system, we need a few basic electronic building blocks.

Component Usage Note
ESP32 Development Board Main microcontroller with WiFi connectivity Allows status monitoring and control over the network
Capacitive Moisture Sensor Measuring soil moisture A much better choice than resistive sensors (does not corrode)
Relay Module (5V or 3.3V) Switching the water pump on and off Galvanic isolation of the control part from the power part
Submersible Water Pump Pumping water from the reservoir Usually a 5V or 12V DC version for small systems

Basic Operating Logic and Connection

The soil moisture sensor works by changing the voltage at its analog output according to the water content in the soil. The ESP32 reads this voltage through its ADC (Analog-to-Digital Converter) input and converts the value into a digital number between 0 and 4095.

  1. Connect the sensor VCC to the 3.3V power supply of the ESP32 board.
  2. Connect the sensor GND to the common ground (GND).
  3. Connect the sensor AOUT (analog output) to a GPIO pin (e.g., GPIO 34).
  4. Connect the relay control pin (IN) to the selected digital output (e.g., GPIO 26).

Sample Code for Testing Operation

The code below shows a simple example of reading an analog value and turning on the relay for a few seconds when the moisture drops below the set threshold.

const int sensorPin = 34; // Analog pin for the moisture sensor
const int relayPin = 26; // Digital pin for controlling the relay

// Set threshold for dry soil (depending on sensor calibration)
int thresholdValue = 2800; 

void setup() {
  Serial.begin(115200);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH); // Relay off at startup (active LOW)
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  Serial.print("Soil moisture value: ");
  Serial.println(sensorValue);

  if (sensorValue > thresholdValue) {
    // Soil is dry - turn on the pump for 5 seconds
    Serial.println("Soil is dry! Turning on watering...");
    digitalWrite(relayPin, LOW); 
    delay(5000);
    digitalWrite(relayPin, HIGH);
  }

  delay(10000); // Check status every 10 seconds
}

Difference Between Prototype and Final Product

Although this simple prototype is great for learning and understanding basic electronics, long-term operation in the garden requires much more. For a reliable system in practice, we need pump dry-run protection, advanced calibration based on soil type, a waterproof enclosure, a stable power supply, and a connection to a web interface for setting schedules.

back Back to blog