Limited Time Your first expert consultation is free! Book now Book now

Back to MakerSpaceIoT Projects

Water Leak Detection with ESP32 and Blynk

Heba 20

1. Project Overview

A small Wi-Fi leak alarm built around an ESP32. A water level sensor board sits on the floor under a water heater, a sink or a washing machine. When water touches its exposed traces, the ESP32 lights a red LED, sounds a buzzer, and sends a notification to your phone through the Blynk app. It is a suggested, reproducible design, not a tested product, and it is a hobby alarm, not a certified leak-protection system.

Cover photo: a water level sensor board like the one in the parts list. Photo: "Waterlevelsensor" by Lukas Ruge on Flickr, dedicated to the public domain under CC0 1.0 (flickr.com/photos/169136682@N02/47604601021; licence: creativecommons.org/publicdomain/zero/1.0). The diagram, tables, code and text on this page were written for this page.

Sources: the official Blynk documentation for the template, datastream, event and notification setup and for the plan limits (docs.blynk.io/en/blynk.console/limits and docs.blynk.io/en/blynk.console/templates/events). No photos or text were copied from Blynk.

2. How It Works

Every 2 seconds the ESP32 powers the sensor from GPIO25, reads its analog output on GPIO34, then cuts the power again. Powering the sensor only while it is read slows the corrosion of its exposed traces. When the reading passes LEAK_THRESHOLD, the ESP32 turns the LED and the buzzer on and logs a "leak_detected" event to Blynk, which pushes a notification to your phone (and an email if you enable it). The event is logged only once per leak, when the reading crosses the limit, so the project stays far below the Blynk free plan limits. When the sensor dries, the LED and buzzer turn off.

3. Components Required

You also need your own computer with Arduino IDE 2, a 2.4 GHz Wi-Fi network (the ESP32 does not use 5 GHz), and a free Blynk account.

Click here to open the components table

4. Circuit Diagram

Wire the circuit as in the diagram. Water sensor: + to GPIO25, - to GND, S to GPIO34. Red LED: long leg through the 220 ohm resistor to GPIO27, short leg to GND. Buzzer: VCC to VIN (5V), S to GPIO26, GND to GND.

Click here to open the wiring diagram

Diagram colors: red = 5V (VIN), orange = sensor power from GPIO25, black = GND, other colors = signals. This is a teaching diagram with simplified pin positions, not a photo of a tested build.

5. Wiring Table

Read each row from the From pin to the To pin and check power, ground and signals before you connect the USB cable. All GND pins join the ESP32 GND.

Click here to open the wiring table

6. Blynk Setup

The phone notification uses the free Blynk plan. Set it up before uploading the code:

  1. Create a free account at blynk.cloud and open Blynk.Console.
  2. Go to Developer Zone, My Templates, + New Template: name it "Water Leak Detector", hardware ESP32, connection Wi-Fi.
  3. In the template open Datastreams, + New Datastream, Virtual Pin: pin V0, data type Integer, min 0, max 4095. This shows the live sensor reading.
  4. Open Events, + New Event: name "Leak detected", code leak_detected. In the event's Notifications tab turn on push notifications (and email if you like) and choose yourself as the recipient.
  5. Go to Devices, + Add New Device, From template, and pick the template. Open the new device and copy its Auth Token from Device Info. The Template ID and Template Name are at the top of the template page.
  6. Copy the three values into the code: BLYNK_TEMPLATE_ID, BLYNK_TEMPLATE_NAME and BLYNK_AUTH_TOKEN.
  7. Install the Blynk app on your phone, log in, and add a Value Display or Gauge widget linked to V0. When a leak is logged, the phone shows a notification.

Free plan limits, from the Blynk documentation: up to 5 devices, 10 templates, 50 datastreams per template, 5 events per template, 100,000 device messages per month and 1 user. Event limits: at most 100 events per device per day, and 1 event per second of the same type. This project uses 1 datastream and 1 event and logs at most 1 event per leak, so it fits easily.

7. Connection Steps

Wire and program the project in this order with the USB cable unplugged until the wiring is checked. The table has the same steps with a check for each.

  1. Install the ESP32 board package and the Blynk library in Arduino IDE 2.
  2. Set up the Blynk template, the V0 datastream and the leak_detected event as in section 6.
  3. Wire the circuit as in the wiring table, with a common GND.
  4. Fill in your Wi-Fi name and password and the three Blynk values, choose ESP32 Dev Module and the right port, then upload.
  5. Open the Serial Monitor at 115200 baud with the sensor in the air: the reading should be near 0.
  6. Dip the sensor traces in a cup of water, keeping the electronics dry: the reading jumps, the LED and buzzer turn on and a phone notification arrives.
  7. Dry the sensor and set LEAK_THRESHOLD between the dry and wet readings, then upload again.

Click here to open the connection steps table

8. Source Code

Upload this code to the ESP32 (file name water_leak_esp32.ino). Pins: sensor power on GPIO25, sensor signal on GPIO34, LED on GPIO27, buzzer on GPIO26. Fill in your Wi-Fi and the three Blynk values first. LEAK_THRESHOLD is a default: read the values in the Serial Monitor dry and in water, and set the limit between them.

// water_leak_esp32.ino
// ESP32 + water level sensor + Blynk: phone notification, LED and buzzer on leak.
// Written by WadiMakers for this guide; not run on hardware yet.

#define BLYNK_TEMPLATE_ID "TMPLxxxxxx"      // from the Blynk template page
#define BLYNK_TEMPLATE_NAME "Water Leak Detector"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"  // from the device page

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

char ssid[] = "YOUR_WIFI_NAME";      // 2.4 GHz network
char pass[] = "YOUR_WIFI_PASSWORD";

const int SENSOR_POWER_PIN = 25;  // sensor + pin: powered only while reading
const int SENSOR_PIN = 34;        // sensor S pin (analog input, ADC1)
const int LED_PIN = 27;           // red LED through 220 ohm
const int BUZZER_PIN = 26;        // active buzzer S pin

const int LEAK_THRESHOLD = 1500;        // raw ADC 0-4095, adjust after testing
const unsigned long READ_EVERY_MS = 2000;

BlynkTimer timer;
bool leakActive = false;

void checkWater() {
  digitalWrite(SENSOR_POWER_PIN, HIGH);  // power the sensor
  delay(10);                             // let it settle
  int level = analogRead(SENSOR_PIN);    // about 0 dry, higher in water
  digitalWrite(SENSOR_POWER_PIN, LOW);   // power off again: less corrosion

  Serial.print("Water level: ");
  Serial.println(level);
  Blynk.virtualWrite(V0, level);         // show the reading in the app

  if (level > LEAK_THRESHOLD && !leakActive) {
    leakActive = true;
    digitalWrite(LED_PIN, HIGH);
    digitalWrite(BUZZER_PIN, HIGH);
    Blynk.logEvent("leak_detected");     // phone notification, once per leak
  } else if (level <= LEAK_THRESHOLD && leakActive) {
    leakActive = false;
    digitalWrite(LED_PIN, LOW);
    digitalWrite(BUZZER_PIN, LOW);
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(SENSOR_POWER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(SENSOR_POWER_PIN, LOW);
  digitalWrite(LED_PIN, LOW);
  digitalWrite(BUZZER_PIN, LOW);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(READ_EVERY_MS, checkWater);
}

void loop() {
  Blynk.run();
  timer.run();
}

Still to verify on real hardware: the pins above with your exact modules, the buzzer polarity (some modules sound on LOW), the LEAK_THRESHOLD value, and the Blynk library version.

9. Testing & Troubleshooting

  • No notification: check the Template ID, name and Auth Token, that the event code is exactly leak_detected, and that notifications are on in the event settings and allowed for the app on the phone.
  • Reading always 0: check S on GPIO34 and that the sensor + pin is on GPIO25; try touching the traces with wet fingers.
  • Reading always high: the traces may be wet or bridged; dry them and check the wiring.
  • Buzzer silent or always on: check the S pin on GPIO26 and the module type (active, not passive); some modules sound on LOW, so swap HIGH and LOW in the code.
  • Device offline in the app: use a 2.4 GHz network and check the Wi-Fi name and password.
  • Readings drift down over weeks: normal corrosion; clean the traces gently and make sure the sensor is powered only while reading.

10. Improvements

  • Add more sensor boards in other rooms (one ADC pin for each) and show all of them in the Blynk app.
  • Log the readings to a Google Sheet or a MQTT broker.
  • Add an LCD 1602 to show the water level on the device.
  • Switch a small 5V pump or a relay module when a leak is confirmed (only with a safe low-voltage load, or have a qualified person handle mains).

11. Safety

The code, wiring, pin choices and threshold are not verified until the project is built and tested. This is a low-voltage USB-powered project and must not be connected to mains voltage; keep the ESP32, breadboard and USB power supply away from the water itself and let only the sensor board touch it. The exposed traces corrode with time, so the sensor suits leak alarms and tank demos, not continuous underwater use. It is a hobby alarm and must not be the only protection against water damage. Fritzing-style parts are not used; the diagram was drawn for this page.

Share
H
About the authorHeba
Share a useful question or note and the project team can reply in the discussion.

Comments & discussion (0)