Build an ESP32 Water Leak Detector with an Automatic Shutoff Valve
- ESP32 Dev Board (WROOM-32)
- Motorized Ball Valve (normally closed, 1/2 inch or 3/4 inch to match supply line)
- Water Leak Sensor Probes / Rope Sensor Cable
- Relay Module (for valve motor switching)
- 12V/24V Power Supply (match the valve's rated voltage)
- Loud Piezo Buzzer Module
- Battery Backup Module (18650 with charge/boost circuit, for power-outage operation)
- Waterproof Project Enclosure
This site's home energy monitor project covers watching whole-house power with CT clamps, but water is the utility that causes genuinely expensive damage fastest when something goes wrong — a washing machine supply hose failing overnight, a water heater developing a slow leak, a frozen pipe cracking. A commercial smart leak detector will notify your phone; this project goes a step further and physically shuts the water off the moment a leak is detected, which is the difference between a notification you see in the morning and a flooded basement you prevented entirely.
Sensor Placement Strategy
Resist the urge to buy a single sensor and place it under one appliance — the value of this system scales with coverage, and leak sensor probes are cheap relative to a single flood. Prioritize placement in this rough order: the main water heater's drip pan, under the washing machine (supply hose failures are extremely common), under every sink's P-trap, next to the dishwasher, and at the lowest point of any basement or crawlspace prone to groundwater intrusion. A simple two-wire conductivity probe (water bridges the gap between two exposed contacts) is all a leak sensor needs to be — no moving parts, nothing to calibrate, and failure mode is simply "doesn't detect," which is exactly why multiple sensors matter more than one expensive one.
Wiring Multiple Sensor Zones
ZoneESP32 GPIOWiring approach Water heaterGPIO32 (ADC or digital, pulled HIGH through resistor)Probe pulls the pin LOW when water bridges the contacts Washing machineGPIO33Same pull-up/bridge approach Under sinks (multiple, wired in parallel per branch if zone-level detail isn't needed)GPIO25Parallel probes on one pin tells you "a leak somewhere in this group" — wire each sink to its own pin instead if you want to know exactly which oneRunning each sensor as its own normally-open loop back to the ESP32, rather than a single loop touching every sensor in series, is what lets the system report which specific zone triggered rather than just "a leak happened somewhere in the house" — worth the extra wire runs for anything beyond the smallest installation.
The Shutoff Valve
A motorized ball valve rated for potable water, sized to match the home's main supply line diameter, replaces a section of pipe at the point where water enters the house (or at a specific branch, if you only want to protect part of the home rather than shutting off everything). Choose a normally-closed valve — one that needs continuous power to stay open and defaults to closed if power is lost — for the safest failure mode: a power outage during a leak event still shuts the water off rather than leaving it flowing uncontrolled. The valve is driven through a relay sized for its motor current, triggered by the ESP32 the instant any sensor zone reports water.
Professional Installation for the Valve Itself
Unlike most of this site's ESP32 projects, the plumbing portion of this build is not something to improvise if you're not already comfortable sweating copper or working with the specific fitting type your home uses. A leaking or improperly sealed valve installation causes exactly the problem this project exists to prevent. Many makers build and test the full electronics and sensor logic themselves, then have a plumber install just the physical valve into the supply line — after which wiring the valve's motor leads to the relay output is straightforward low-voltage work.
Notification and Logic
Publish sensor state changes over MQTT to Home Assistant (covered at length elsewhere on this site) rather than building a standalone notification system from scratch — this gets you push notifications, a dashboard showing which zone triggered and when, and the ability to layer in automations like "also turn off the water heater" without touching the ESP32 firmware again. Keep the leak-to-shutoff logic itself running locally on the ESP32 rather than depending on a round-trip to Home Assistant or the cloud: a leak detection should close the valve within the same second it's detected, regardless of whether WiFi or a home automation server happen to be up at that moment.
Power Outage Resilience
A water leak caused by a frozen or burst pipe often coincides with exactly the kind of weather event that also knocks out power. A small 18650-based UPS module keeps the ESP32 and the sensor-reading logic alive through a brief outage; whether the valve itself can still be commanded closed during an outage depends on whether its motor also has backup power, which is worth sizing into the battery backup capacity if home power reliability is a real concern in your area.
Testing Before You Trust It
Test every sensor zone with an actual small amount of water (not just a multimeter continuity check) before considering the system live, and test that the valve physically closes completely under real house water pressure, not just that the relay clicks. The entire point of this build is unattended operation catching a leak you're not home for — it's worth the hour of deliberate testing to be confident it'll actually work at 3am when nobody's watching.
This project sits at the more consequential end of what a maker ESP32 build can do: get the sensor logic and valve failure mode right, and it genuinely prevents the kind of water damage that costs thousands of dollars and weeks of remediation. Get the failure mode backwards — a normally-open valve that stays open on power loss — and the system silently does nothing exactly when a power outage and a leak happen together, which is worth double-checking before the whole thing goes behind drywall.