Two metal contacts can reveal water because ordinary tap water conducts electricity. But leaving a direct-current probe energized continuously also encourages electrochemical changes at the contacts. This experiment applies a short measuring pulse and spends most of its time with the drive turned off. You can observe the tradeoff between sampling interval, sensitivity and electrode exposure.
The current path
GPIO means general-purpose input/output. GPIO25 drives electrode A through 100 kΩ. Water adds a variable resistance between electrodes A and B. A 1 MΩ pull-down connects electrode B to ground, and GPIO34 measures the resulting voltage. An ADC, or analog-to-digital converter, converts that voltage into a number. The sensor branch stays entirely within the board’s 3.3 V logic supply.
With a dry gap there is almost no conducting path and the pull-down keeps SENSE low. With water bridging the gap, SENSE rises by an amount depending on conductivity, spacing and immersed area. For an illustrative 100 kΩ water path, the ideal midpoint is 3.3 × 1,000/(100 + 100 + 1,000) = 2.75 V. Treat that as a circuit calculation, not a prediction for your particular water.
Parts to gather
- Classic ESP32-DevKitC V4 / ESP32-WROOM-32 with MicroPython 1.26
- Two separate stainless-steel electrodes held a few millimetres apart
- 100 kΩ resistor, 1 MΩ resistor, 680 Ω resistor and red LED
- Shallow plastic tray, a little tap water and dry supports for the electronics

Complete connections
| Connection | Purpose |
|---|---|
| GPIO25 → 100 kΩ → electrode A | This is the pulse source and current-limiting path. |
| Electrode B → SENSE → GPIO34 | Water bridges the two electrodes; they do not touch. |
| SENSE → 1 MΩ → GND | The pull-down creates a low dry reading. |
| GPIO26 → 680 Ω → LED anode; cathode → GND | Board powered by USB; keep it and the cable outside the tray. |
Why pulse the probe?
The 100 kΩ series resistor bounds drive current below approximately 3.3/100,000 = 33 µA even if a downstream fault bypasses the normal pull-down. During an ordinary near-zero-resistance wet bridge, the 1 MΩ pull-down also remains in series, giving only about 3 µA. High resistance makes the measurement sensitive to interference and input loading, so the program uses a settling interval and the median of several readings.
About 98 ms of nominal active time in a roughly five-second cycle is around a 2% duty fraction, plus interpreter overhead. Duty fraction means the portion of time the source is on. Reduced exposure may reduce corrosion; it does not eliminate it, and this circuit does not reverse polarity. For continuous conductivity instrumentation, the Analog Devices reference uses alternating excitation with very low direct-current offset to address polarization. Our short, pulsed demonstration retains a direct-current component and does not claim that protection.
Calibrate the actual tray
- Run the program with WET_ON and DRY_OFF left as None. It prints raw readings while keeping the LED off.
- Keep the electrodes separated and dry. Record the stable raw range.
- Add just enough tap water to bridge the gap. Record the wet range. Keep all board electronics outside the water.
- If there is a clear gap, choose DRY_OFF above the dry range and WET_ON below the wet range, with DRY_OFF smaller than WET_ON. Enter the two measured thresholds and restart.
- Remove and dry the electrodes. Verify that the indication clears. Residue may keep the reading elevated; clean the contacts and compare.
Complete MicroPython program
"""Conductivity demo, NOT flood protection. ESP32 GPIO25 drives via 100k.
Second electrode -> GPIO34, with 1M to GND. Never apply external voltage.
"""
WET_ON = None # set after measuring dry and wet readings
DRY_OFF = None
def decision(raw, wet, wet_on, dry_off):
if wet_on is None or dry_off is None:
return False
if not 0 <= dry_off < wet_on <= 65535:
raise ValueError("Require 0 <= DRY_OFF < WET_ON <= 65535")
if raw >= wet_on:
return True
if raw <= dry_off:
return False
return wet
def main():
from machine import Pin, ADC
from time import sleep_ms
drive = Pin(25, Pin.OUT, value=0)
adc = ADC(Pin(34)); adc.atten(ADC.ATTN_11DB)
led = Pin(26, Pin.OUT, value=0)
wet = False
decision(0, False, WET_ON, DRY_OFF)
try:
while True:
drive.on()
try:
sleep_ms(80)
samples = []
for _ in range(9):
samples.append(adc.read_u16()); sleep_ms(2)
raw = sorted(samples)[4]
finally:
drive.off()
wet = decision(raw, wet, WET_ON, DRY_OFF)
led.value(wet)
print("raw=%d wet=%s calibrated=%s" % (raw, wet, WET_ON is not None and DRY_OFF is not None))
sleep_ms(4900)
finally:
drive.off(); led.off()
if __name__ == "__main__":
main()
Test the misleading cases
Distilled water may conduct too weakly to reach your threshold. A finger, damp residue or ionic contamination can conduct more strongly than expected. Try spacing the electrodes farther apart and explain the changed reading before recalibrating. The ESP32 ADC here provides a threshold signal, not a calibrated water-resistance meter.
Unplug electrode A and the circuit will look dry. A broken wire is therefore indistinguishable from a clean dry tray in this simple topology. The program’s finally blocks turn off drive when a software exception or normal interruption unwinds the loop, but they cannot guarantee shutdown after every possible hardware failure. This is a conductivity demonstration, not flood protection. Do not attach it to a pump, mains appliance or unattended water-supply control.
Challenge: does doubling the five-second interval improve detection?
It reduces how often you sample and the average energized time, but a new wet event can wait longer before the next sample. Better electrode life and faster detection are separate design goals that must be balanced.
Sources and related learning
- Analog Devices: why conductivity instruments use low-offset alternating excitation
- MicroPython classic ESP32 GPIO and ADC
- MicroPython pin control
Explore the Tricky Circuit interactive lessons. This article presents a design and software checks; no physical build results are claimed.



