The Sensing Principle: Conductivity and Voltage Dividers
A contact water level sensor measures the electrical conductivity of a liquid. Pure water is actually a poor conductor; it is the dissolved ions (minerals, salts, chlorine) that allow current to flow. When you submerge two exposed electrodes into water, the liquid acts as a variable resistor. The deeper the submersion, the larger the surface area of the electrodes in contact with the water, which lowers the electrical resistance between the probes and allows more current to flow.
On the hardware side, this variable resistance is wired into a voltage divider circuit. The sensor's internal or external fixed resistor pulls the signal line toward ground, while the water's resistance pulls it toward the supply voltage. As the water level rises and resistance drops, the voltage at the signal node increases. Some modules include an LM393 comparator to flip this analog voltage into a clean digital HIGH/LOW signal at a set threshold, but for continuous level monitoring, we bypass the comparator and read the raw analog voltage directly.
Output Signals and Wiring Pinout
Before wiring, you must define what output you actually need. These sensors provide two distinct outputs, and conflating them is a common beginner mistake:
- Analog Output (AO): A variable voltage (typically 0V to VCC) proportional to the submerged surface area. Use this for measuring how full a tank is.
- Digital Output (DO): A 0V or VCC logic signal from an onboard comparator. Use this strictly for threshold alerts (e.g., 'tank is empty' or 'leak detected').
| Sensor Pin | ESP32 Pin | Function | Notes & Constraints |
|---|---|---|---|
| VCC | GPIO 25 (or 3V3) | Power Supply | 3.3V max. Use a GPIO to switch power and prevent electrolysis (see code below). |
| GND | GND | Ground Reference | Must share a common ground with the ESP32 and any external pumps. |
| AO (Analog) | GPIO 34 | Signal Out | ADC1_CH6. Input only. Do not use ADC2 pins (GPIO 25, 26, 27) if WiFi is active. |
| DO (Digital) | Not Connected | Comparator Out | Leave disconnected unless you specifically need a binary threshold alert. |
Raw ADC to Water Depth: The Conversion Math
The ESP32 features a 12-bit ADC, yielding raw integer values from 0 to 4095. However, the ESP32 ADC is notoriously non-linear at the extreme ends of its range (below 0.15V and above 3.0V). To get accurate readings, your sensor's voltage swing must stay within the 0.15V to 3.0V linear window.
Here is the exact math to convert the raw ADC reading into a usable physical percentage. First, convert the raw integer to voltage:
Voltage = (ADC_Raw / 4095.0) * 3.3
Because water conductivity varies wildly based on mineral content (tap water vs. rain water vs. hydroponic nutrient solution), you cannot use a universal constant to map voltage to inches or centimeters. You must calibrate the V_empty and V_full thresholds for your specific liquid.
Depth_Percent = ((Voltage - V_empty) / (V_full - V_empty)) * 100.0
Calibration Procedure
- Place the sensor in the completely empty tank. Record the ADC voltage. This is
V_empty(usually around 0.1V due to stray moisture or dust). - Fill the tank to your desired maximum level. Record the ADC voltage. This is
V_full(ideally around 2.8V to 3.0V). - Hardcode these two float values into your firmware. If
V_fullexceeds 3.1V, add a higher-value pull-down resistor to the sensor's signal line to shift the curve down into the ESP32's linear range.
Interference, Electrolysis, and Failure Modes
Contact sensors fail in the field for two primary reasons: electrolysis and electromagnetic interference (EMI).
The Electrolysis Problem
If you apply a continuous DC voltage to a contact water level sensor, the water undergoes electrolysis. Ions migrate to the electrodes, causing rapid oxidation. A cheap copper-trace PCB sensor will literally dissolve into green sludge within 48 hours of continuous 3.3V DC power. Even stainless steel probes will degrade over a few months.
The Fix: Never leave the sensor powered continuously. Use an ESP32 GPIO pin to switch the VCC of the sensor. Turn the power on for 10 milliseconds, take the ADC reading, and immediately turn it off. This reduces the electrolysis effect by over 99%.
// ESP32 Electrolysis Prevention Snippet
const int SENSOR_POWER = 25;
const int SENSOR_READ = 34;
void setup() {
pinMode(SENSOR_POWER, OUTPUT);
digitalWrite(SENSOR_POWER, LOW); // Keep off by default
Serial.begin(115200);
}
void loop() {
digitalWrite(SENSOR_POWER, HIGH); // Power the sensor
delay(10); // Wait for ADC capacitor settling
int raw = analogRead(SENSOR_READ);
digitalWrite(SENSOR_POWER, LOW); // Kill power immediately
float voltage = (raw / 4095.0) * 3.3;
Serial.printf("Voltage: %.2f V\n", voltage);
delay(60000); // Read once per minute
}
EMI from Pumps and Solenoids
If your ESP32 is mounted near a water pump or a solenoid valve, the inductive kickback from the pump's motor will inject high-frequency noise into your sensor's analog signal line, causing wild ADC fluctuations. To fix this, install a flyback diode (e.g., 1N4007) across the pump's terminals, and add a 0.1µF ceramic capacitor between the sensor's AO pin and GND at the ESP32 end to filter high-frequency noise.
Decision Matrix: Which Sensor to Buy
Do not default to the cheapest option. The environment and the required precision dictate the hardware. Use this decision path to select the correct probe.
| Application Scenario | Required Hardware | Estimated Cost | Why This Pick? |
|---|---|---|---|
| Flat surface leak detection (e.g., under a water heater) | PCB Trace Rain/Leak Sensor | $1 - $3 | Large flat surface area detects thin films of water. Not suitable for depth measurement. |
| Continuous tank level monitoring (10cm to 100cm depth) | Stainless Steel 2-Pin/3-Pin Probe | $8 - $15 | Resists corrosion far better than copper. The 3-pin version includes a ground reference to reduce EMI noise. |
| High-precision hydroponics or chemical dosing tanks | DFRobot SEN0114 or Industrial 4-20mA Probe | $19 - $45 | Includes onboard signal conditioning and temperature compensation for varying liquid conductivity. |
| Corrosive liquids (acids, high-salinity brine) | Non-Contact Ultrasonic (e.g., JSN-SR04T) | $12 - $20 | Contact sensors will degrade rapidly in highly corrosive or conductive electrolytes. Defer to ultrasonic. |






