Build it step by step
0 of 8 doneWire the sensor
Connect power, ground and the divided signal.
Test the count
Blow gently through it and watch pulses print.
Fit it in the line
Put the arrow along the flow.
Calibrate
Pour 10 litres and set PULSES_PER_LITRE.
Fill in the Wi-Fi
Replace the placeholders.
Upload
Open the page at the board's address.
Test the leak rule
Temporarily change the night hours and run water for five minutes.
Mount
Keep the board dry.
How do you build a water use monitor?
A flow sensor on a garden or tank line counts litres and shows daily use and leaks on a web page. You need ESP32 DevKit V1, Water flow sensor YF-S201, 5 V 2 A USB adapter, Resistor 1 kΩ and Resistor 2 kΩ. It takes 8 steps and about 120 minutes.
- Level
- Intermediate
- Time
- About 120 min
- Parts
- 5
Parts list
- Development BoardsBuyESP32 DevKit V1 × 1
- SensorsBuy
- PowerBuy
- PassivesBuyResistor 1 kΩ × 1
- PassivesBuyResistor 2 kΩ × 1
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Flow sensor red wire | 5 V supply + | Power, 5 to 18 V |
| Flow sensor black wire | 5 V supply - and ESP32 GND | Ground |
| Flow sensor yellow wire | 1 kΩ resistor then ESP32 GPIO27, with 2 kΩ from GPIO27 to GND | Divider brings 5 V pulses down to about 3.3 V |
| 5 V supply | ESP32 VIN | Power the ESP32 too |
Code
The sensor's Hall switch makes about 450 pulses for each litre, but it varies between sensors, so calibrate: pour a measured 10 litres through it and set PULSES_PER_LITRE to the pulses counted divided by 10. The sensor works at 5 V and makes 5 V pulses, so a 1 kΩ and 2 kΩ divider reduces them to about 3.3 V for the ESP32. Fit it in a straight section of pipe with the arrow along the flow and use it only for low pressure garden or tank lines; it is not a billing meter. Flow for five minutes without a break at night, when nobody uses water, is a good sign of a leak. Keep the electronics dry away from the pipe. Arduino IDE with the ESP32 boards package, board "ESP32 Dev Module".
Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Counts pulses from a YF-S201 (about 450 pulses per litre), shows litres on a web page.
#include <WiFi.h>
#include <WebServer.h>
#include <time.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const float PULSES_PER_LITRE = 450.0; // calibrate with a measured bucket
const byte FLOW_PIN = 27;
volatile unsigned long pulses = 0;
unsigned long lastPulses = 0, litreTotalPulses = 0, lastCheck = 0;
unsigned long flowSeconds = 0; // consecutive seconds with flow
bool leakFlag = false;
int lastDay = -1;
WebServer server(80);
void IRAM_ATTR onPulse() { pulses++; }
void handleRoot() {
float litres = litreTotalPulses / PULSES_PER_LITRE;
String s = "<html><body style='font-family:sans-serif'><h3>Water today</h3><p>" + String(litres, 1) + " litres</p>";
s += leakFlag ? "<p style='color:red'>Possible leak: flow at night</p>" : "<p>No leak flagged</p>";
s += "</body></html>";
server.send(200, "text/html", s);
}
void setup() {
Serial.begin(115200);
pinMode(FLOW_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(FLOW_PIN), onPulse, RISING);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) delay(300);
configTime(19800, 0, "pool.ntp.org");
server.on("/", handleRoot);
server.begin();
Serial.println(WiFi.localIP());
}
void loop() {
server.handleClient();
if (millis() - lastCheck >= 1000) {
lastCheck = millis();
noInterrupts(); unsigned long p = pulses; interrupts();
unsigned long d = p - lastPulses; lastPulses = p;
litreTotalPulses += d;
time_t t = time(nullptr); struct tm* tm = localtime(&t);
if (tm->tm_year > 100) {
if (tm->tm_mday != lastDay) { lastDay = tm->tm_mday; litreTotalPulses = 0; leakFlag = false; } // new day
flowSeconds = d > 3 ? flowSeconds + 1 : 0;
bool night = tm->tm_hour >= 2 && tm->tm_hour < 4;
if (night && flowSeconds > 300) leakFlag = true; // five minutes of flow at 2 to 4 am
}
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| 10 litres poured | About 10 litres on the page |
| Tap off | No pulses |
| Flow for 5 minutes at night | Leak flag |
| New day | Total resets to zero |
If it does not work
No pulses
Check the divider and the 5 V supply on the sensor.
Reads high or low
Recalibrate the pulses per litre.
Page does not open
Check the IP address and the Wi-Fi.
Time wrong
The NTP server needs internet access.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the water use monitor?
ESP32 DevKit V1, Water flow sensor YF-S201, 5 V 2 A USB adapter, Resistor 1 kΩ and Resistor 2 kΩ.
How long does the water use monitor take to build?
About 120 minutes for a intermediate maker, in 8 steps.
How do I know the water use monitor works?
10 litres poured: About 10 litres on the page. Tap off: No pulses. Flow for 5 minutes at night: Leak flag.
No pulses: what should I check?
Check the divider and the 5 V supply on the sensor.
Reads high or low: what should I check?
Recalibrate the pulses per litre.
Page does not open: what should I check?
Check the IP address and the Wi-Fi.
Time wrong: what should I check?
The NTP server needs internet access.
