Smart home

Water leak detector ESP32 ESPHome

Alert Home Assistant on a water leak: probe, ESP32-C6, ESPHome, wiring, YAML and simple automations. Parts list, GPIO wiring, ESPHome YAML and Home Assista

Updated · Emmanuel SAUVAGE

  • LevelIntermediate
  • Time1–3 h
  • Budgetabout $12–30 depending on the seller

Rough estimate, check with your seller: prices vary by shop and period.

Project goal

You will lay a probe on the floor under a washing machine, a water heater or a sink, and get an alert in Home Assistant, the software that gathers your connected devices, as soon as water bridges the probe’s tracks. An ESP32, a small electronic board with Wi-Fi, checks it only every 30 seconds and powers it only during the reading, which delays the corrosion of the metal tracks.

Why build it

A slow leak under an appliance often goes unnoticed for days, long enough to ruin the cabinet or the floor. This build warns you before serious damage is done, and an automation can even switch off a smart plug, for example the washing machine’s, as soon as the alert fires.

Useful terms for this project: ESP32 (small Wi-Fi board) · ESPHome (prepares the board’s program) · GPIO (the board’s pins) · OTA (update without a USB cable)

Prerequisites

  • Home Assistant with ESPHome and a power outlet near the install spot (5 V USB supply).
  • A three-pin analog water sensor module (S, + and −), the “water sensor” type sold for microcontrollers.
  • A glass of water and a multimeter or the ESPHome log to calibrate the threshold.

Diagrams

Wiring diagram : Water leak detector
Wiring diagram : Water leak detectorGPIO18 → Water probe VCC (+); GND → Water probe GND (−); GPIO2 → Water probe AO (S)ESP32-C6 DevKitC-1Water probeGPIO18VCC (+)GNDGND (−)GPIO2AO (S)PowerGround (GND)Data or signal
Principle diagram : Water leak detector
Principle diagram : Water leak detectorWater probe → ESP32-C6 + ESPHome → Home AssistantWater probeBare traces: water bridges themPowered only while measuringAO on GPIO2ESP32-C6 + ESPHomeMeasures every 30 s (ADC)Compares with an adjustable threshold2.4 GHz Wi-Fi + encrypted APIHome Assistant“Water leak” entity (moisture)Notification and plug cut-off

Parts list

This ESP32 board is the base for every project in the series. ESPHome also works with other ESP32 boards: the board-choice guide helps you compare them. Which ESP32 board to choose?

ESP32-C6-DevKitC-1

Suggested board

ESP32-C6-DevKitC-1

check with the seller
  • Wi-Fi 6, Bluetooth 5, Zigbee and Thread (at hardware level)
  • Supported by ESPHome (ESP-IDF framework)
  • USB-C for flashing and power

Watch-outs

  • 2.4 GHz Wi-Fi only
  • Use a USB cable that carries data

Main components

ItemQuantityRole and precautions
ESP32-C6-DevKitC-11Control board (see the box above).
Analog water probe module (bare traces)1Analog AO output: the value changes when water bridges the traces. Bare traces eventually oxidise: plan to replace the probe. Main component: search for « water level sensor module analog Arduino » at your seller (affiliate link, check price and availability).
3-core extension wire1 to 3 mLets you put the probe on the floor and the board higher, away from splashes.

Small parts

ItemQuantityRole and precautions
USB-C data cable1For flashing and power. Charge-only cables do not carry data.
USB 5 V power supply (CE/UKCA-marked charger)1At least 1 A for the board alone; more if peripherals draw power from it.
Dupont jumper wires (male-female and female-female)1One mixed pack covers every prototype.
Breadboard1To test without soldering; replace with a soldered build if you need it permanent.
Heat-shrink tubing1Insulates solder joints and splices; avoid tape in a permanent build.
Vented plastic or 3D-printed enclosure1Protects the board; leave a cable gland and vents if the sensor measures air.

Amazon links are affiliate links: the site may earn a commission at no extra cost to you.

Step-by-step build

Step 1. Prepare ESPHome and create the device

  1. In Home Assistant, open the ESPHome Device Builder add-on (see the tutorial “Install ESPHome in Home Assistant (Device Builder)”). ESPHome is the software that turns a simple settings file into the program for your ESP32 board, without you having to write any code.
  2. Open “Secrets” (top right): this file keeps your passwords apart, so you can share your configurations without revealing them. Check that wifi_ssid (the name of your Wi-Fi network), wifi_password, api_encryption_key (the key that encrypts the link with Home Assistant), ota_password (for updates without a cable) and ap_password (for the board’s fallback Wi-Fi network) exist, and add any missing line. A valid API key is a long 32-byte string encoded in base64, which you can generate with “openssl rand -base64 32”.
  3. Click “New device”, name it “detecteur-fuite”, pick ESP32-C6, skip the flash for now, then replace the file content with the YAML from this page (“Edit”). YAML is a configuration file written in plain text: it describes, line by line, what the board must do.

What you should see: The file opens in the editor with syntax highlighting and no red underline.

Check: The “Validate” action (⋮ menu) answers “Configuration is valid”.

Step 2. Wire the probe

  1. With the USB power unplugged, connect the probe’s + pin to GPIO18, one of the metal pins lined up along the edge of the board (GPIO means “general purpose input output”). The probe is therefore not powered all the time: the board gives it current only during the reading, which slows the corrosion of its tracks.
  2. Then connect the − pin to a GND pin, the ground, and the probe’s output, marked S or AO (analogue output, which gives a varying voltage), to GPIO2.
  3. Extend the probe wire if the board must stay high.

What you should see: The probe is unpowered between measurements: any LED on it does not stay lit.

Check: The signal wire is on GPIO2, an analog input pin of the board.

Step 3. Flash the board and add it to Home Assistant

  1. Connect the board to your computer with the USB-C data cable: “flashing” means writing the program into the board’s memory, and this first write must be done over a cable. The board can already be wired or sitting on the breadboard, but any external peripheral with its own power supply must stay switched off.
  2. In ESPHome click “Install”, then “Plug into this computer” (Chrome or Edge over HTTPS) or “Manual download” and upload the file with ESPHome Web (web.esphome.io). If the port is missing, follow the flashing tutorial.
  3. After the reboot open Settings → Devices & services: the ESPHome device is discovered; click “Configure” and paste the API encryption key.

What you should see: The “Logs” show the Wi-Fi connection and then “Successfully connected to Home Assistant”.

Check: The device appears under Devices & services → ESPHome with its entities; later updates need no cable and go over Wi-Fi (this is OTA, “Over The Air”).

Step 4. Calibrate dry and wet values

  1. Calibrating means teaching the board to tell “dry” from “wet”. In Home Assistant, open “Leak probe level”, the voltage read on the probe, and note the value with the probe dry (for example 0.0 V).
  2. Dip the probe tip in a little water and note the wet value (for example 1.8 V).
  3. Set the “upper” threshold halfway and the “lower” one a bit below in the YAML, then reinstall over OTA.

What you should see: “Water leak” turns to “Wet” with a little water and back to “Dry” once the probe is dried.

Check: Test three times: the changes are clean, without flicker.

Step 5. Install and test in real conditions

  1. Lay the probe flat at the lowest point under the appliance you want to watch, tracks facing up, because water always runs to the lowest spot, then fix the wire so it does not move the probe.
  2. Pour a little water next to it: the alert must arrive in under a minute.
  3. Wipe, dry the probe and check the return to normal.

What you should see: A phone notification arrives after the detection.

Check: Repeat this test every three months: it also reveals an oxidised probe.

ESPHome code (YAML)

YAML is a configuration file written in plain text: it tells ESPHome what the board must do. Paste this one into ESPHome after creating the device. It targets an ESP32-C6 board; values to personalise are marked with “CHANGE” comments.

esphome:
  name: detecteur-fuite
  friendly_name: Water leak detector

esp32:
  board: esp32-c6-devkitc-1   # XIAO ESP32C6: use board: seeed_xiao_esp32c6
  framework:
    type: esp-idf             # required for ESP32-C6

logger:                       # serial logs, also visible over Wi-Fi

api:
  encryption:
    key: !secret api_encryption_key

ota:
  - platform: esphome
    password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid       # 2.4 GHz network only
  password: !secret wifi_password
  ap:                           # fallback hotspot if the Wi-Fi is unreachable
    ssid: "detecteur-fuite fallback"
    password: !secret ap_password

captive_portal:

switch:
  - platform: gpio
    id: probe_power
    pin: GPIO18                  # powers the probe only while measuring (limits corrosion)
    restore_mode: ALWAYS_OFF
    internal: true

sensor:
  - platform: adc
    id: leak_level
    pin: GPIO2                   # ADC1 channel on the ESP32-C6
    name: "Leak probe level"
    update_interval: never       # read on demand by the interval below
    attenuation: 12db            # full 0-3.3 V range
    accuracy_decimals: 2
    entity_category: diagnostic

  - platform: wifi_signal
    name: "WiFi signal"
    update_interval: 120s
    entity_category: diagnostic

interval:
  - interval: 30s
    then:
      - switch.turn_on: probe_power
      - delay: 300ms             # let the probe settle before reading
      - component.update: leak_level
      - switch.turn_off: probe_power

binary_sensor:
  - platform: analog_threshold
    name: "Water leak"
    device_class: moisture
    sensor_id: leak_level
    threshold:
      upper: 1.2                 # CHANGE after measuring your probe dry and wet
      lower: 0.8                 # hysteresis: back to dry below this value

YAML comments are in English, as in the ESPHome documentation. This code has not been run on your hardware: use “Validate” before flashing and adjust pins and names.

Home Assistant integration

  • The “Water leak” entity (moisture class) behaves like a leak detector: it triggers standard alerts and turns red on the dashboard card.
  • Typical automation: on detection, send a priority phone notification and cut the smart plug of the washing machine or dishwasher.
  • Add “Leak probe level” to a dashboard to follow probe drift and spot oxidation.

Common problems and troubleshooting

SymptomFix
Permanent false alarmThe threshold is too low or the traces are damp or oxidised. Dry and clean the probe, re-measure the dry value and raise “upper”.
The value never changesThe S/AO pin is not on GPIO2, or the probe is not powered while measuring. Check GPIO18 on the + and raise the delay to 500 ms.
Late alertReduce the 30 s interval to 10 s; the probe stays lightly stressed thanks to the pulsed supply.
The probe corrodes after a few weeksThis is normal for bare traces: replace it or move to a stainless-contact sensor or a sealed Zigbee leak detector.

FAQ

Why power the probe from a GPIO?

A continuous current in water electrolyses the traces. Powering it only 300 ms every 30 s greatly cuts the time under voltage and slows oxidation. The module’s current must stay below the pin’s limit.

Can it run on a battery?

Not with this build: Wi-Fi stays on. For a battery a commercial Zigbee leak sensor is a better fit.

Can I fit several?

Yes: an ESP32 handles several analog inputs (GPIO0 to GPIO6); repeat the sensor block and power each probe from its own GPIO.

Related tutorials

These step-by-step guides explain the gestures common to every project in the series.

Other ESP32 projects

Sources and documentation

Cette page vous a-t-elle été utile ?

Have a question about this guide or need advice? Ask us here

Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.