This build turns a soil moisture reading into an automatic watering decision. An Arduino Nano ESP32 checks a capacitive soil moisture sensor and switches a relay that controls a 12V pump, so the plant gets watered only when the soil is actually dry, with no code loop on your laptop needed once it's flashed.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Nano ESP32 - Soil Moisture Sensor Pump
What you need
- Arduino Nano ESP32
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Capacitive Soil Moisture Sensor
- Relay
- 12V Pump
- Vinyl Tube
- DC Power Jack
- Breadboard
- Jumper Wires
- Screw Terminal Expansion Board for Arduino Nano
- Breakout Expansion Board for Arduino Nano
- Power Splitter for Arduino Nano ESP32
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The sensor's analog output pin connects to A2 on the Arduino Nano ESP32, and the relay's control input connects to D2. The pump itself isn't driven directly by the microcontroller pin, since it needs more current and a separate 12V supply than a GPIO can provide; the relay acts as an electrically controlled switch, letting the low-power D2 pin toggle the higher-power 12V circuit that the pump and power adapter are on. A screw terminal expansion board is a handy option if you want the wiring to stay put instead of relying on breadboard friction.
How it works
The Arduino periodically reads the moisture value from the sensor. If that value is above a set threshold, the soil is considered dry and the board activates the relay, turning the pump on. If the value is at or below the threshold, the soil is wet enough, so the relay is deactivated and the pump turns off. This repeats continuously, so the pump responds automatically as the soil dries out or gets watered.
Code
The sketch sets the ADC attenuation so the analog input can read up to roughly 3.3V, then in the loop it calls analogRead() on SENSOR_PIN to get a moisture value. That value is compared against THRESHOLD: above it, digitalWrite() sets RELAY_PIN HIGH to turn the pump on, otherwise it's set LOW to turn it off. Each reading and the resulting action gets printed to the Serial Monitor, and the loop waits one second before repeating.
arduino-nano-esp32-soil-moisture-sensor-pump.ino
/*
* This Arduino Nano ESP32 code was developed by newbiely.com
*
* This Arduino Nano ESP32 code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-soil-moisture-sensor-pump
*/
#define RELAY_PIN D2 // The Arduino Nano ESP32 pin that connects to relay
#define SENSOR_PIN A2 // The Arduino Nano ESP32 pin that connects to AOUT pin of moisture sensor
#define THRESHOLD 1500 // => CHANGE YOUR THRESHOLD HERE
void setup() {
Serial.begin(9600);
// set the ADC attenuation to 11 dB (up to ~3.3V input)
analogSetAttenuation(ADC_11db);
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
int value = analogRead(SENSOR_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);
}
Things that go wrong
A lot of cheap capacitive soil moisture sensors are unreliable regardless of the version printed on them; one built around the TLC555I chip has tested reliably, so it's worth checking which chip is on your sensor if readings look erratic. The THRESHOLD value in the code is just a placeholder and needs to be set from an actual calibration of your sensor in dry versus wet soil, otherwise the pump may run constantly or never turn on at all.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-soil-moisture-sensor-pump

Top comments (0)