</>CoderDIY
← Back to list
Article

Build a Smart Water Leak Detector with ESP8266

Author: CoderDIY··Đọc bằng tiếng Việt
💧

A water leak under a sink, behind a washing machine, or near a water heater usually goes unnoticed until someone finds a wet floor or a stained ceiling downstairs — by then the damage is already significant. A cheap water leak sensor, paired with an ESP8266, can catch the very first drop and send an instant alert to your phone, even when nobody’s home.

How it works

  1. The water leak sensor is a set of two parallel exposed traces (similar to a soil moisture sensor, but laid flat on the floor) — when dry, the two traces are fully insulated from each other; once water bridges them, it conducts weakly, closing the circuit and sharply dropping the measured resistance.
  2. The ESP8266 continuously reads a digital or analog signal from the sensor; once the value crosses a detection threshold, it treats that as a leak.
  3. To avoid false alarms from momentary noise, the system requires the wet signal to stay consistent across several consecutive readings before confirming a real alert.
  4. Once a leak is confirmed, the ESP8266 sends an urgent message via the Telegram Bot API including the installation location (e.g. “Water leak: under the kitchen sink”), and can also trigger a local buzzer alarm so people in the house hear it immediately.
<rect x="10" y="20" width="140" height="60" rx="10" class="box" />
<text x="80" y="45" text-anchor="middle" class="label">Read sensor</text>
<text x="80" y="62" text-anchor="middle" class="label">continuously</text>

<rect x="170" y="20" width="140" height="60" rx="10" class="box" />
<text x="240" y="45" text-anchor="middle" class="label">Above wet threshold?</text>
<text x="240" y="62" text-anchor="middle" class="label">(several times in a row)</text>

<rect x="330" y="20" width="140" height="60" rx="10" class="box" />
<text x="400" y="45" text-anchor="middle" class="label">Trigger local buzzer</text>
<text x="400" y="62" text-anchor="middle" class="label">alarm</text>

<rect x="490" y="20" width="140" height="60" rx="10" class="box" />
<text x="560" y="45" text-anchor="middle" class="label">Send Telegram</text>
<text x="560" y="62" text-anchor="middle" class="label">with location</text>

<line x1="150" y1="50" x2="170" y2="50" class="arrow" />
<line x1="310" y1="50" x2="330" y2="50" class="arrow" />
<line x1="470" y1="50" x2="490" y2="50" class="arrow" />

<path d="M 560 80 L 560 190 L 80 190 L 80 80" class="arrow" fill="none" />
<text x="320" y="210" text-anchor="middle" class="sublabel">Keeps reading continuously for the earliest possible detection</text>
Leak detection loop: read the sensor, confirm across several readings to avoid false alarms, then trigger the buzzer and send an alert as soon as it's confirmed.

Parts list

  • An ESP8266 (NodeMCU or Wemos D1 Mini, with built-in WiFi)
  • A water leak sensor module (a cheap, easy-to-find open-trace pair sensor)
  • A small buzzer (optional) for local alarming
  • A 5V power supply (a small USB adapter near an outlet at the install site)
  • A cable long enough to place the sensor at the lowest point (floor level, under a pipe) while keeping the board mounted higher, away from moisture
<rect x="235" y="70" width="170" height="100" rx="10" class="box" />
<text x="320" y="125" text-anchor="middle" class="label">ESP8266</text>

<rect x="20" y="30" width="150" height="70" rx="10" class="box" />
<text x="95" y="70" text-anchor="middle" class="label">Water sensor</text>

<rect x="470" y="30" width="150" height="70" rx="10" class="box" />
<text x="545" y="70" text-anchor="middle" class="label">Buzzer</text>

<line x1="170" y1="65" x2="235" y2="110" class="wire" />
<text x="180" y="95" text-anchor="middle" class="sublabel">Out → A0</text>

<line x1="405" y1="110" x2="470" y2="65" class="wire" />
<text x="455" y="95" text-anchor="middle" class="sublabel">D5</text>
The water sensor, placed flush with the floor, connects to analog pin A0; the buzzer connects to D5 to sound the alarm when water is detected.

Sample code

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

const char* WIFI_SSID = "your-wifi";
const char* WIFI_PASS = "your-wifi-password";
const char* BOT_TOKEN = "TELEGRAM_BOT_TOKEN";
const char* CHAT_ID = "TELEGRAM_CHAT_ID";
const char* LOCATION_NAME = "Under the kitchen sink";

const int SENSOR_PIN = A0;
const int BUZZER_PIN = D5;
const int WATER_THRESHOLD = 500; // tune to your actual sensor, lower value means wetter

WiFiClientSecure secureClient;
UniversalTelegramBot bot(BOT_TOKEN, secureClient);

bool leakActive = false;
int consecutiveWetReads = 0;
const int CONFIRM_READS = 3; // requires 3 consecutive readings to confirm, avoids false alarms

void setup() {
  Serial.begin(115200);
  pinMode(BUZZER_PIN, OUTPUT);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  secureClient.setInsecure();

  bot.sendMessage(CHAT_ID, "Water leak sensor at " + String(LOCATION_NAME) + " is ready.", "");
}

void loop() {
  int reading = analogRead(SENSOR_PIN);
  bool wetNow = reading < WATER_THRESHOLD;

  if (wetNow) {
    consecutiveWetReads++;
  } else {
    consecutiveWetReads = 0;
  }

  if (consecutiveWetReads >= CONFIRM_READS && !leakActive) {
    leakActive = true;
    digitalWrite(BUZZER_PIN, HIGH);
    bot.sendMessage(CHAT_ID, "WATER LEAK ALERT: " + String(LOCATION_NAME), "");
    Serial.println("Leak confirmed, alert sent!");
  } else if (!wetNow && leakActive) {
    leakActive = false;
    digitalWrite(BUZZER_PIN, LOW);
    bot.sendMessage(CHAT_ID, "Leak cleared (or dried up): " + String(LOCATION_NAME), "");
  }

  delay(2000); // check every 2 seconds, fast enough for early detection
}

Common pitfalls

  • Mounting the ESP8266 board too low, right near the floor risks the board itself getting damaged once water spreads — only the sensor should sit at floor level; mount the main board higher, away from where water could reach.
  • No confirmation across multiple readings (CONFIRM_READS) causes false alarms from momentary humidity or dust on the sensor — always require the signal to stay consistent across several consecutive readings.
  • Bare metal sensor traces oxidize over time, gradually reducing sensitivity — choose a sensor with an anti-oxidation coating, or check it periodically, especially in consistently damp spots.
  • A single sensor covering the whole house misses leaks elsewhere — for homes with multiple risk spots (kitchen, bathroom, washing machine, water heater), install several independent units, each labeled with its own location in the alert message.

Extensions

Integrate with an electronic solenoid valve to automatically shut off the main water supply the moment a leak is detected, check out the smart mailbox notifier with ESP8266 project for another take on sending Telegram alerts from home sensors, or publish state over MQTT to show it on a shared Home Assistant dashboard.

Tags:#esp8266#sensors#safety

Comments