Home IoT Hub Series · 14
[Part 14] Is water leaking, and how much are we using? YF-S201 water-flow node
Count water flow pulses from a YF-S201 sensor and send usage to the hub.
This node measures water usage by counting pulses from a YF-S201 flow sensor. Every pulse means a small amount of water has passed through.
Install direction matters. Match the arrow on the sensor body to the water-flow direction, and protect the electronics from moisture.
![[Part 14] Is water leaking, and how much are we using? YF-S201 water-flow node hero image](/iot-lab/assets/water-flow-yfs201-hero.png)
This article is part of the Home IoT Hub system connected to the central NodeMCU server built in Part 2: Building the Brain. YF-S201 pulses are converted into water usage so the hub can track usage patterns and possible leaks. The collected data is sent over the local
IoT_Hub_Net network and updates slot 16 for water usage on the central server (/set?no16=value), becoming part of the decision pipeline for the 20-channel smart-home controller.Parts and cost
| Part | Qty | Unit price |
|---|---|---|
| Wemos D1 Mini | 1 pc | $1.50 |
| YF-S201 flow sensor | 1 pc | $2.20 |
| Jumper wires 3 pc | — | — |
| Total | $3.60 |
Pin wiring
| Sensor / module | Board pin |
|---|---|
| red (VCC) | 5V (VU) |
| black (GND) | GND |
| yellow (OUT) | D2 (GPIO4) |
Full code
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> const char* ssid = "IoT_Hub_Net"; const char* password = "iothub1234"; const char* hubURL = "http://192.168.1.1"; #define FLOW_PIN 4 // D2 (interrupt-capable pin) #define SLOT_NO 16 #define PULSE_PER_L 450 // YF-S201: 450 pulses/L #define INTERVAL 15000 volatile unsigned long pulseCount = 0; unsigned long lastSend = 0; void ICACHE_RAM_ATTR countPulse() { pulseCount++; } void setup() { Serial.begin(115200); pinMode(FLOW_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(FLOW_PIN), countPulse, FALLING); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to IoT_Hub_Net"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected!"); } void loop() { unsigned long now = millis(); if (now - lastSend > INTERVAL) { // note unsigned long pulses; noInterrupts(); pulses = pulseCount; pulseCount = 0; interrupts(); float liters = (float)pulses / PULSE_PER_L; Serial.print("Pulses: "); Serial.print(pulses); Serial.print(" -> Water: "); Serial.print(liters, 2); Serial.println(" L"); WiFiClient client; HTTPClient http; String url = String(hubURL) + "/set?no" + String(SLOT_NO) + "=" + String(liters, 1); http.begin(client, url); http.GET(); http.end(); Serial.println("HTTP -> " + url); lastSend = now; } delay(100); }
Upload and check
Connect the Wemos D1 Mini to the PC with a USB data cable, select the ESP8266 board and port in the Arduino IDE, upload the sketch, then open Tools → Serial Monitor and set the baud rate to 115200.
Serial Monitor
After uploading, open Tools → Serial Monitor in the Arduino IDE and set the baud rate to 115200. The following is an example Serial Monitor flow for checking normal operation. This is not a real screenshot. It shows pulse counting, noise filtering, and slot 16 water-use updates.
[BOOT] Home IoT Node #14 - Water flow YF-S201 [INT] pulse counter attached, debounce=50ms [SENSOR] pulses=38 flow=1.9L/min total=0.32L [FILTER] air_bubble_noise=PASS [HTTP] GET /set?no16=0.32 [HUB] 200 OK - slot no16 updated [CHECK] leak watch enabled when flow continues too long
- If
Connectedorconnectedappears, the Wi-Fi step passed. - If
HTTP 200or200 OKappears, the value reached the brain server. - If the value does not update, check both the slot number in the URL and the sensor wiring.
Build notes
- Run water for a short fixed time and watch pulse counts increase. Adjust the calibration factor later if you need more accurate liters.
- Power on the hub brain first so the node can join
IoT_Hub_Net. - Use the same slot number written in the source code.
- Open the serial monitor at 115200 baud and confirm
HTTP 200. - If the value does not appear on the hub, recheck wiring, GPIO number, and Wi-Fi connection.
Using the YF-S201 Water Flow Sensor
The YF-S201 is a Hall-effect flow meter: as water passes through, an internal impeller spins and generates pulses that are counted and converted into liters. Wiring and code were covered earlier, so here we focus on making the sensor genuinely useful inside your home IoT hub.
Troubleshooting checkpoints
The node code can look short, but real home installation is affected by placement, power, Wi-Fi quality, and sensor noise. Check these points before letting the node become a decision input for the brain server.
| Symptom | Likely cause | Field fix |
|---|---|---|
| Pulses appear when no water is used | Air bubbles or pressure changes briefly move the impeller | Discard pulses that arrive under 50 ms or isolated short bursts as noise. |
| Measured usage differs from real water volume | The YF-S201 factor changes with direction, pressure, and pipe size | Calibrate pulse-per-liter with a 1 L or 5 L measured container on the actual installation. |
| Small leaks appear at fittings | Thread sealing is insufficient or the joint is loose | Match the flow arrow, use enough PTFE tape, and check with tissue for the first 24 hours. |
| The sensor fails quickly on hot-water lines | Low-cost YF-S201 units are weak against hot water and pressure changes | Check temperature/pressure ratings and prefer cold-water measurement or a hot-water-rated sensor. |
Use Cases
Once you accumulate total liters, a single sensor unlocks several metrics.
- Water bill estimate: Multiply monthly cumulative usage by your local rate and show the projected bill on your dashboard.
- Leak detection: If a continuous trickle appears during hours nobody uses water (say 2-4 a.m.), flag it as a possible toilet or pipe leak and send an alert.
- Garden watering: Attach it to a hose to log how many liters you gave today and auto-close a valve when the target is reached.
- Shower habits: Log per-shower volume and duration for a family water-saving challenge.
Recommended Placement
- Follow the arrow: Match the arrow on the body to the real flow direction. Reversed, readings are wrong and the impeller barely turns.
- Mount horizontally: Lay it so the impeller axis is horizontal for stable rotation, even at low flow. Horizontal runs beat vertical pipe.
- After the meter: Install just after the water meter and before any branch to capture whole-house usage; for a single fixture, put it on that branch line.
- Avoid vibration and backflow: Skip spots right after a pump or a sharp bend where turbulence inflates error; give it a straight pipe section.
- Accessible and indoors: You'll need to clean and service it, so keep it within reach and safe from freezing.
Practical Tips and Cautions
- Calibration factor varies with flow: The datasheet's 'F=7.5×Q' is only an average; error grows at low flow. Fill a 1L cup or bottle at several flow rates to derive your own pulse-per-liter factor.
- Low-flow limits: Below roughly 1 L/min the impeller may not spin, so very faint flows go undetected. Keep this in mind for tiny leaks.
- Debris and limescale: Sand, rust, or scale can stiffen the impeller. Add a simple strainer upstream and clean it periodically.
- Waterproof the wiring: Seal the connector area with heat-shrink or epoxy, and route the joint so it sits no lower than the pipe to stop water wicking in.
FAQ
Q. Can I use it on hot water? A. The YF-S201 is generally rated up to about 80°C. Avoid the hottest section right after the heater, or choose a heat-rated variant (e.g., YF-S201C).
Q. Will high pressure damage it? A. It's fine within the rated pressure (around 1.75 MPa). Where water hammer is frequent, add pressure reduction or a shock arrestor upstream.
Q. Readings don't match my water meter. A. Usually a calibration issue. Compare the meter dial with the sensor's total over the same interval and re-tune the factor to shrink the error.
This article is part of the 20-channel Home IoT system built from the Part 2 central brain server and the distributed edge nodes in Parts 3-19. Use this map to check the slot and sensor flow across the series.
[Part 1] Home IoT Hub overview[Part 2] Central brain server · slots 1-20[Part 3] Stop guessing room comfort · slots 1/2[Part 4] Did I close the door? · slots 9[Part 5] Is the fridge really cooling? · slots 12[Part 6] Should I open the window? · slots 3[Part 7] Stop bathroom mold before it starts · slots 19[Part 8] Is the boiler actually running? · slots 5/17[Part 9] Did I leave the gas stove on? · slots 6[Part 10] Get warned before gas smell becomes obvious · slots 7[Part 11] Catch flame signs while away from home · slots 8[Part 12] Is bad air making you foggy? · slots 13/14[Part 13] Why is the power bill so high? · slots 15[Part 14] Is water leaking, and how much are we using? · slots 16[Part 15] Do not leave windows open in the rain · slots 18[Part 16] Are the lights off and the door locked? · slots 11/10[Part 17] Control the air conditioner from the IoT hub · slots 4[Part 18] Is the dog bowl empty? · slots 20[Part 19] Cool the room only when someone is there · slots 1/4[Part 20] The house finally fits on one screen · slots all