#376IntermediateIoT

ESP32 IoT Plant Watering (Soil Sensor + Pump)

Let the pump start by itself when the soil in your pot dries out, and watch the soil moisture live on your phone. This project is the step-by-step guide to the Soil moisture + pump IoT template in the app, built with an ESP32, a soil moisture sensor, a relay and a mini water pump. Go to Workshop › Devices › Add a device; Ohmi prepares the code with your own device key. The code on this page is an example: the key and Wi-Fi details are placeholders.

0.0(0)
ESP32 IoT Plant Watering (Soil Sensor + Pump)
If you do not want your plants to go thirsty while you are away, this project is for you. The sensor measures how wet the soil is; when the moisture drops below the threshold you set, a relay starts the pump, and it stops once the soil is wet enough. The moisture builds up in a chart on your phone, and you can change the threshold or run the pump from there too. The pump runs on its own batteries, not the board, so you also learn to keep water and electronics apart and to wire safely.
IoT · remote controlYour first device is free

Control this device from the app

Connect your ESP32 or Raspberry Pi Pico W to Wi-Fi, then switch it on and off and watch live readings from your phone. Ohmi prepares the code; you just enter your Wi-Fi details and upload it.

Template in the app: Soil moisture + pump

  • Where is it in the app?Add a new device from the Tools tab or Workshop › Devices and pick this template.
  • Arduino libraryOpen the Library Manager in the Arduino IDE, search for BlueGraysIoT and install it.
Download the app for free

The free plan includes 1 device, 1 chart and 24 hours of history. Premium adds more devices, more charts and longer history.

Circuit Diagram

ESP32 IoT Plant Watering (Soil Sensor + Pump) - Circuit

Source Code

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   ESP32 IoT Plant Watering (Soil Sensor + Pump)
4// ─────────────────────────────────────────
5// The "Soil moisture + pump" IoT template from the BlueGrays app: the soil
6// moisture sensor measures every 2 seconds and sends the value to the app,
7// the relay starts the pump when the moisture drops below the watering
8// threshold and stops it 10 points above it; you can also control the pump
9// and the threshold from your phone. Ohmi prepares the real code in the
10// app with your own device key (Workshop › Devices › Add a device); the
11// code here is an example, the device key and Wi-Fi details are
12// placeholders. Library Manager: BlueGraysIoT (with its dependencies). The
13// board only joins 2.4 GHz Wi-Fi networks. The relay switches on LOW.
14// Safety: power the pump from its own battery holder (4 x AA), not from
15// the board; keep the board, relay and batteries away from water in a
16// closed box; never run the pump dry.
17//
18// Pins:
19//   GPIO34 -> Soil moisture sensor AO
20//   3V3, GND -> Soil moisture sensor VCC, GND
21//   GPIO26 -> Relay module IN
22//   5V, GND -> Relay module VCC, GND
23//   Relay COM -> battery holder (+), NO -> pump (+), pump (-) -> battery holder (-); the pump
24//   runs on its own 4 x AA
25//   Pico W: moisture sensor AO GP26, relay IN GP15
26
27/*
28 * Soil moisture + pump · BlueGrays IoT
29 * BlueGrays · bluegrays.com
30 * Do not share this code: it contains your device key and Wi-Fi password.
31 */
32
33#include <BlueGraysIoT.h>
34
35const char *DEVICE_KEY = "YOUR_DEVICE_KEY";
36const char *WIFI_NAME = "YOUR_WIFI_NAME";
37const char *WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
38
39BlueGraysIoT bg(DEVICE_KEY);
40
41const int RELAY_PIN = 26;
42const bool RELAY_ACTIVE_LOW = true;  // most relay modules switch on LOW
43void setRelay(bool on) { digitalWrite(RELAY_PIN, (on != RELAY_ACTIVE_LOW) ? HIGH : LOW); }
44const int SOIL_PIN = 34;
45const int SOIL_DRY = 3200;  // reading in dry soil
46const int SOIL_WET = 1300;  // reading in wet soil
47int threshold = 30; bool pump = false; unsigned long lastReadMs = 0;
48
49void setup() {
50  Serial.begin(115200);
51  pinMode(RELAY_PIN, OUTPUT);
52  setRelay(false);
53  analogReadResolution(12);
54  bg.onSwitch("pompa", [](bool on) { pump = on; setRelay(on); });
55  bg.onNumber("esik", [](float v) { threshold = (int)v; });
56  bg.setDeadband("toprak", 1.0);
57  bg.setDebug(Serial);
58  bg.setLanguage(BG_LANG_EN);
59  bg.begin(WIFI_NAME, WIFI_PASSWORD);
60}
61
62void loop() {
63  bg.loop();  // avoid long delay() calls here
64  if (millis() - lastReadMs >= 2000) {
65    lastReadMs = millis();
66    int pct = constrain(map(analogRead(SOIL_PIN), SOIL_DRY, SOIL_WET, 0, 100), 0, 100);
67    bg.send("toprak", pct);
68    // Turn the pump on below the threshold, off 10 points above it
69    if (pct < threshold && !pump) { pump = true; setRelay(true); bg.send("pompa", true); }
70    if (pct > threshold + 10 && pump) { pump = false; setRelay(false); bg.send("pompa", false); }
71  }
72}
73

FAQ

Can I upload the code on this page straight to my board?

You can, but it will not connect: the device key and Wi-Fi details in it are placeholders. In the app go to Workshop › Devices › Add a device and pick the Soil moisture + pump template; Ohmi prepares the code with your key, network name and password. Copy that code and upload it. You can see the full code later in Device › Code and key.

Does it work with a Pico W?

Yes. Pick Pico W as the board when you add the device; the code stays the same and only the pins and calibration values change: the moisture sensor goes to GP26 (an analog input) and the relay to GP15. In Arduino IDE install the "pico" package from Boards Manager. The circuit and photo on this page were made with an ESP32.

Why does the pump have its own batteries?

The current the pump draws and the noise its motor makes can reset the board. That is why the pump runs from a 4 x AA battery holder and only passes through the relay's COM and NO terminals; it has no wire connection to the board. Keep the board, relay and battery holder away from water in a closed box, and keep the wire joints dry. The pump must only run under water; running dry, it heats up and fails within minutes. Take the batteries out while you fit the hose or lift the pump out of the water.

When does the pump run, and what happens if I switch it by hand?

The board measures the moisture every 2 seconds. When it drops below the Watering threshold in the app (30% at first) the pump turns on, and it turns off 10 points above the threshold; the gap stops the pump from clicking on and off. You can tap the Pump switch by hand too, but the automatic rule wins: if you turn it off while the soil is below the threshold, it comes back on within 2 seconds, and if you turn it on while the soil is 10 points above, it turns off right away. Set the threshold to suit your pot.