ESP8266 Water Level Sensor Project
This project demonstrates how to use an ESP8266 microcontroller (such as NodeMCU or Wemos‑D1) to monitor water levels using an analog water level sensor. The ESP8266 reads the sensor’s analog output, converts it into a usable level value, and can send data over Wi‑Fi, trigger alerts, or control pumps and valves. This system is ideal for water‑tank monitoring, irrigation control, and flood detection in agricultural, industrial, and environmental applications.
How the Water Level Sensor Works
A typical water level sensor module uses exposed conductive traces or electrodes that act as a variable resistor. When the sensor is immersed, water bridges the traces, reducing the resistance and increasing the analog output voltage. The ESP8266’s ADC pin then reads this voltage (0–1024 on the ESP8266 10‑bit ADC) and maps it to a meaningful level such as millimeters, percentage, or state (low/medium/high).
Components Needed
- ESP8266 development board (e.g. NodeMCU‑v2 or Wemos‑D1)
- Water level sensor module (analog style)
- LEDs (green, yellow, red, optional for visual indication)
- 220 Ω resistors (for LEDs)
- Relay module (optional, for pump or valve control)
- Jumper Wires
- Breadboard
- Water tank or container for testing
Circuit Diagram
Circuit Setup
1. Connect Water Level Sensor to ESP8266
Most water level sensor modules expose three pins:
- VCC → 3.3 V or 5 V on the ESP8266 (check module datasheet; many accept 3.3–5 V).
- GND → GND on the ESP8266.
- S (Signal / Analog output) → A0 (analog pin) on the ESP8266.
2. Optional Indicator Circuits
- Green LED (for low/normal level): connect anode to GPIO 5 through a 220 Ω resistor; cathode to GND.
- Yellow LED (for medium): connect to GPIO 4 (with 220 Ω); red LED (for high/overflow) to GPIO 13 (with 220 Ω).
- Optional relay: connect the relay control pin to GPIO 12 (or another GPIO) and power the relay as per its module instructions for pump or valve control.
Instructions
1. Software Setup
Configure the Arduino IDE for ESP8266 (e.g. NodeMCU‑v2). Initialize serial communication at 9600 baud for debugging and add Wi‑Fi configuration if you plan to send data to cloud services (ThingSpeak, Blynk, or MQTT).
2. Sensor Readings in loop()
In the loop() function, read the analog value from A0 using analogRead(A0), which returns 0–1023. The raw value will be low when the sensor is dry and higher as more of the sensor is submerged. You can map this value to a percentage (0–100%) or to a linear scale (e.g. 0–40 mm) after calibration.
3. Calibration and Logic
Fill the tank in steps (e.g. 0 mm, 10 mm, 20 mm, 30 mm, 40 mm) and note the sensor values at each step. Use these values to set thresholds (e.g. 300 for low, 600 for medium, 900 for high). In the code, light LEDs or trigger a relay when the level crosses these thresholds.
4. Safety and Operational Considerations
Ensure the sensor traces are not shorted by tools or metal objects. Power the system from a stable 3.3–5 V source and avoid long or noisy analog wires that can cause fluctuations. For long‑term deployment, mount the sensor vertically in the tank and keep it away from strong vibrations or turbulent flow for more stable readings.
C++ Example Code (ESP8266 Water Level Sensor)
#include <ESP8266WiFi.h>
// Replace with your Wi‑Fi credentials if needed
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// Sensor and LED pins
#define WATER_PIN A0
#define LED_LOW 5 // GPIO 5
#define LED_MEDIUM 4 // GPIO 4
#define LED_HIGH 13 // GPIO 13
#define RELAY_PIN 12 // optional pump/valve control
// Example calibration thresholds (update for your tank)
const int LOW_LEVEL = 300;
const int MEDIUM_LEVEL = 600;
const int HIGH_LEVEL = 900;
void setup() {
Serial.begin(9600);
// Optional: connect to Wi‑Fi
// WiFi.begin(ssid, password);
// while (WiFi.status() != WL_CONNECTED) delay(1000);
pinMode(LED_LOW, OUTPUT);
pinMode(LED_MEDIUM, OUTPUT);
pinMode(LED_HIGH, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(LED_LOW, LOW);
digitalWrite(LED_MEDIUM, LOW);
digitalWrite(LED_HIGH, LOW);
digitalWrite(RELAY_PIN, LOW);
Serial.println("=== ESP8266 WATER LEVEL SENSOR READY ===");
}
void loop() {
// Read sensor value
int raw = analogRead(WATER_PIN);
// Convert to 0–100% (optional, for easier interpretation)
int levelPercent = map(raw, 0, 1023, 0, 100);
// Reset all LEDs
digitalWrite(LED_LOW, LOW);
digitalWrite(LED_MEDIUM, LOW);
digitalWrite(LED_HIGH, LOW);
digitalWrite(RELAY_PIN, LOW);
if (raw < LOW_LEVEL) {
// Level too low
digitalWrite(LED_LOW, HIGH);
Serial.println(F("LOW LEVEL"));
} else if (raw < MEDIUM_LEVEL) {
// Normal / medium level
digitalWrite(LED_MEDIUM, HIGH);
Serial.println(F("MEDIUM"));
} else {
// High / overflow risk
digitalWrite(LED_HIGH, HIGH);
digitalWrite(RELAY_PIN, HIGH); // e.g., stop pump or open valve
Serial.println(F("HIGH LEVEL"));
}
// Send data to Wi‑Fi service here if needed
// e.g., HTTP POST or MQTT publish
// Print raw and percentage to Serial Monitor
Serial.print("Raw: ");
Serial.print(raw);
Serial.print(" Level: ");
Serial.print(levelPercent);
Serial.println("%");
delay(2000);
}
Applications
Water Management Systems: Monitor water levels in tanks or reservoirs and automatically trigger refill or drain operations based on thresholds.
Irrigation and Agriculture: Use the sensor in overhead tanks for drip‑ or sprinkler‑based systems and activate the pump only when the water level is above a safe minimum.
Industrial Applications: Prevent overflow or dry‑run conditions in industrial process tanks by integrating the ESP8266 with pumps, valves, or PLCs. Detect early‑stage flooding in basements, pump‑rooms, or sump pits and send alerts via Wi‑Fi or SMS integrations.
Notes
Analog Output Mapping
The water level sensor provides an analog output proportional to immersion depth. The ESP8266 maps this 0–1023 value to a percentage or linear scale using the map() function, making it easy to set user‑friendly thresholds (e.g. 0–100%) instead of raw sensor numbers.
Serial and Debugging
Use the Serial Monitor (9600 baud) to observe raw sensor values, percentage levels, and LED/relay status. This helps you calibrate the sensor and fine‑tune the thresholds for your specific tank geometry and sensor orientation.
Remote Monitoring Extension
You can extend this project by connecting the ESP8266 to Wi‑Fi and sending readings to a cloud dashboard (ThingSpeak, Blynk, or Node‑RED), or by using an OLED or LCD to show the level locally, or by integrating the system with a home‑automation platform to send alerts when the water level is too low or dangerously high.