ESP8266 - Water Leak Detector

In this guide, we will learn how to use an ESP8266 and a water leak sensor to detect water leaks.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Water Leak Detector
1×Breadboard
1×Jumper Wires
1×(Optional) ESP8266 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 the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Water Leak Detector

The water leak detector, also called a water leak sensor, helps us discover unwanted water early to prevent damage.

Water Leak Detector Pinout

The water leak detector includes two wires:

Water Leak Sensor Pinout

We don't have to distinguish between the two wires of the water leak detector, just like we don't need to differentiate between a switch and a button.

How the Water Leak Detector Works

When water is present, the circuit becomes complete. When there is no water, the circuit remains incomplete.

To connect the water leak detector to an ESP8266, attach one wire to the GND and the other to a digital input pin on the ESP8266 set as a pull-up. When water is present, the pin reads LOW. Without water, it reads HIGH.

※ NOTE THAT:

The water leak detector cannot sense pure water because it is "non-conductive." To fix this, sprinkle some salt near the sensor. This will mix with the water and allow the detector to recognize the water.

Wiring Diagram between Water Leak Detector and ESP8266

The wiring diagram between ESP8266 NodeMCU and water leak detector

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program ESP8266 to read value from Water Leak Detector

  • Configure the ESP8266 pin as a digital input with the pinMode() function. For example, use this function for pin D7.
pinMode(D7, INPUT_PULLUP);
  • Checks the condition of the ESP8266 pin using the digitalRead() function.
int water_state = digitalRead(D7);

ESP8266 Code - Detecting Water Leakage

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-water-leak-detector */ #define WATER_SENSOR_PIN D7 // ESP8266 pin D7 connected to water sensor's pin int water_state; // current state of water sensor int prev_water_state; // previous state of water sensor void setup() { Serial.begin(9600); pinMode(WATER_SENSOR_PIN, INPUT_PULLUP); // set ESP8266 pin to input pull-up mode water_state = digitalRead(WATER_SENSOR_PIN); // read state } void loop() { prev_water_state = water_state; // save the last state water_state = digitalRead(WATER_SENSOR_PIN); // read new state if (prev_water_state == HIGH && water_state == LOW) { Serial.println("Water leakage is detected!"); } }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • To upload the code to your ESP8266 board, click the Upload button in Arduino IDE.
  • Pour water close to the water leak detector.
  • See the results on the Serial Monitor. They will display like this:
COM6
Send
The water leak is detected The water leak is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!