DEV Community

Cover image for ESP32 S3 - Soil Moisture Sensor Pump
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

ESP32 S3 - Soil Moisture Sensor Pump

This build turns an ESP32 S3 into a tiny irrigation controller: a capacitive soil moisture sensor tells it how dry the soil is, and a relay lets it switch a 12V water pump on or off in response. It's a solid first project for mixing analog sensing with real-world actuation, and a good base for anything from a single houseplant to a multi-zone garden setup.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 S3 - Soil Moisture Sensor Pump

What you need

Wiring

ESP32 S3 - Soil Moisture Sensor Pump wiring diagram

The sensor's analog output goes to an ADC-capable GPIO on the ESP32 S3 (GPIO6 in the sketch), and the relay's control pin goes to a digital output GPIO (GPIO7). The relay module itself is powered from the ESP32 S3's 3.3V or 5V rail, whatever it needs, while the pump runs on a separate 12V supply wired through the relay's COM and NO terminals.

Keep the pump on its own 12V adapter, never on the ESP32 S3's USB power - a pump draws far more current than USB can safely deliver. Double-check COM and NO wiring too, since reversed polarity can damage the motor.

How it works

A capacitive soil moisture sensor measures volumetric water content by sensing changes in capacitance rather than conductivity, which makes it more durable and resistant to corrosion than older resistive sensors. Its analog output voltage drops as the soil gets wetter, so a lower ADC reading means wetter soil.

The relay sits between the low-power ESP32 S3 and the high-current 12V pump circuit, acting as a safe switch. The ESP32 S3 reads the sensor, compares it to a threshold, and activates the relay to run the pump when soil is dry, deactivating it once the soil is wet enough.

Code

The sketch sets the ADC attenuation to 11dB so it can read up to about 3.3V from the sensor, then loops forever: read the moisture value with analogRead(), compare it to THRESHOLD, and drive RELAY_PIN high or low accordingly, printing the reading and pump state to serial every second.

You need to calibrate THRESHOLD yourself using dry and wet readings from your actual sensor and soil before relying on it, since raw ADC values vary between sensors.

esp32-s3-soil-moisture-sensor-pump.ino

/*
 * This ESP32 S3 code was developed by newbiely.com
 *
 * This ESP32 S3 code is made available for public use without any restriction
 *
 * For comprehensive instructions and wiring diagrams, please visit:
 * https://newbiely.com/tutorials/esp32-s3/esp32-s3-soil-moisture-sensor-pump
 */

/*
 * This ESP32-S3 code is created by esp32io.com
 *
 * This ESP32-S3 code is released in the public domain
 *
 * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-soil-moisture-sensor-pump
 */

#define RELAY_PIN    7 // ESP32-S3 pin GPIO7 that connects to relay
#define MOISTURE_PIN 6 // ESP32-S3 pin GPIO6 (ADC1) that connects to AOUT pin of moisture sensor
#define THRESHOLD 1500   // => CHANGE YOUR THRESHOLD HERE

void setup() {
  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);
  // set the ADC attenuation to 11 dB (up to ~3.3V input)
  analogSetAttenuation(ADC_11db);
}

void loop() {
  int value = analogRead(MOISTURE_PIN); // read the analog value from soild moisture sensor

  if (value > THRESHOLD) {
    Serial.print("The soil moisture is DRY => activate pump");
    digitalWrite(RELAY_PIN, HIGH);
  } else {
    Serial.print("The soil moisture is WET => deactivate the pump");
    digitalWrite(RELAY_PIN, LOW);
  }

  Serial.print(" (");
  Serial.print(value);
  Serial.println(")");

  delay(1000);
}
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

A lot of cheap capacitive soil moisture sensors are unreliable regardless of version; sensors built around the TLC555I chip have tested reliably, so it's worth checking what chip is on yours if readings look erratic. If the pump never turns on or off, check the relay wiring first, especially COM versus NO and the relay's power rail.

If the pump refuses to run at all, confirm it's on its own 12V adapter rather than being starved of current from the USB port.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp32-s3/esp32-s3-soil-moisture-sensor-pump

Top comments (0)