ESP32 - Water Sensor - Pump

In this tutorial, we will learn how to use an ESP32 to activate a pump for draining water when it is detected by a water sensor. In other words, we will be discovering how to create an automatic drainage system using an ESP32, a water sensor, and a pump

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Water level sensor
1×Relay
1×12V Pump
1×Vinyl Tube
1×12V Power Adapter
1×(Optional) DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to Water Sensor and Pump

If you do not know about water sensor and pump (pinout, how it works, how to program ...), learn about them in the following tutorials:

How the Automatic Drainage System Works

ESP32 periodically reads the value from the water sensor, then:

  • If the value is greater than a pre-defined threshold, ESP32 turns on the relay to activate the pump.
  • Otherwise, ESP32 turns off the pump.

Wiring Diagram

ESP32 water sensor controls Pump Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-water-sensor-pump */ #define RELAY_PIN 13 // The ESP32 pin GPIO13 that connects to the relay to control the pump #define POWER_PIN 32 // The ESP32 pin GPIO32 that provides the power to the water sensor #define SIGNAL_PIN 36 // The ESP32 pin GPIO36 that reads the value from the water sensor #define THRESHOLD 1000 // The threshold for water detectiion void setup() { Serial.begin(9600); pinMode(RELAY_PIN, OUTPUT); // configure D2 pin as an OUTPUT pinMode(POWER_PIN, OUTPUT); // configure D7 pin as an OUTPUT digitalWrite(POWER_PIN, LOW); // turn the water sensor OFF digitalWrite(RELAY_PIN, LOW); // turn the pump OFF } void loop() { digitalWrite(POWER_PIN, HIGH); // turn the water sensor's power ON delay(10); // wait 10 milliseconds int value = analogRead(SIGNAL_PIN); // read the analog value from sensor digitalWrite(POWER_PIN, LOW); // turn the water sensor's power OFF if (value > THRESHOLD) { Serial.print("The water is detected"); digitalWrite(RELAY_PIN, HIGH); // turn the pump ON } else { digitalWrite(RELAY_PIN, LOW); // turn the pump OFF } delay(1000); // pause for 1 sec to avoid reading sensors frequently to prolong the sensor lifetime }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
Arduino IDE Upload Code
  • Put the water sensor into the water
  • See the pump's state

Code Explanation

Read the line-by-line explanation in comment lines of source code!

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES