Inquire: Call 0086-755-23203480, or reach out via the form below/your sales contact to discuss our design, manufacturing, and assembly capabilities.
Quote: Email your PCB files to Sales@pcbsync.com (Preferred for large files) or submit online. We will contact you promptly. Please ensure your email is correct.
Notes: For PCB fabrication, we require PCB design file in Gerber RS-274X format (most preferred), *.PCB/DDB (Protel, inform your program version) format or *.BRD (Eagle) format. For PCB assembly, we require PCB design file in above mentioned format, drilling file and BOM. Click to download BOM template To avoid file missing, please include all files into one folder and compress it into .zip or .rar format.
Water Flow Sensor Arduino: YF-S201 Complete Tutorial
Measuring liquid flow accurately has been one of those challenges that kept appearing across my automation projects. Whether designing water dispensers, irrigation controllers, or industrial monitoring systems, the Water Flow Sensor Arduino combination using the YF-S201 consistently delivers reliable results without breaking the budget.
The YF-S201 sits in a sweet spot between professional flow meters costing hundreds of dollars and unreliable DIY alternatives. This Hall effect sensor outputs clean digital pulses that any microcontroller can count, making flow measurement accessible to hobbyists and engineers alike. After integrating dozens of these into various PCB designs, I can walk you through exactly how to get accurate readings from this sensor.
What is the YF-S201 Water Flow Sensor?
The YF-S201 is a turbine-style flow sensor designed to measure the volume of liquid passing through a pipe. Inside the plastic housing sits a pinwheel rotor with an embedded magnet that spins as water flows through the sensor. A Hall effect sensor detects each rotation and outputs a corresponding electrical pulse.
What makes this sensor practical for Arduino projects is its simple three-wire interface. Unlike analog sensors requiring ADC conversion and complex calibration, the YF-S201 outputs digital pulses with frequency proportional to flow rate. Count the pulses over time, apply a conversion factor, and you have flow rate in liters per minute.
The sensor mounts inline with your water pipe using standard 1/2 inch (G1/2) threaded connections. An arrow molded into the body indicates the required flow direction, which must be followed for accurate measurements.
YF-S201 Technical Specifications
Understanding the electrical and mechanical limits prevents problems during installation:
Parameter
Value
Operating Voltage
5V – 18V DC
Maximum Current
15 mA @ 5V
Flow Rate Range
1 – 30 L/min
Working Pressure
≤ 1.75 MPa (254 PSI)
Working Temperature
-25°C to +80°C
Output Signal
Square wave pulse
Pulse Frequency
F = 7.5 × Q (L/min)
Pulses Per Liter
450 pulses
Accuracy
±10% (typical)
Pipe Diameter
1/2 inch (G1/2)
Body Material
Plastic (food-grade available)
The accuracy specification deserves attention. Out of the box, the YF-S201 delivers approximately ±10% precision. For applications requiring better accuracy, individual calibration against a known volume is necessary. More on that later.
YF-S201 Pinout and Wiring Configuration
The YF-S201 uses a simple three-wire interface with color-coded leads:
Wire Color
Function
Arduino Connection
Red
VCC (Power)
5V
Black
GND (Ground)
GND
Yellow
Signal (Pulse Output)
Digital Pin 2 or 3
The signal output is internally pulled HIGH through a 10kΩ resistor on most modules, so external pull-ups are typically unnecessary. The output swings between ground and VCC with each magnet rotation past the Hall sensor.
Why Use Interrupt Pins?
Connecting the signal wire to an interrupt-capable pin (Digital 2 or 3 on Arduino Uno) is critical. At maximum flow rate, the sensor outputs approximately 225 Hz (30 L/min × 7.5). Polling this signal in the main loop would miss pulses during any processing delay. Hardware interrupts ensure every pulse gets counted regardless of what else the code is doing.
Wiring YF-S201 to Arduino
The basic connection requires just three wires:
YF-S201 Wire
Arduino UNO
Arduino Mega
Arduino Nano
Red (VCC)
5V
5V
5V
Black (GND)
GND
GND
GND
Yellow (Signal)
D2
D2, D3, D18, D19, D20, D21
D2, D3
For Arduino Uno and Nano, only pins 2 and 3 support external interrupts. The Mega provides additional interrupt pins, useful when multiple flow sensors are needed.
Understanding the Flow Calculation Formula
The relationship between pulse frequency and flow rate is defined by the manufacturer:
Pulse Frequency (Hz) = 7.5 × Q
Where Q is the flow rate in liters per minute. Rearranging gives us:
Q (L/min) = Pulse Frequency / 7.5
Another way to look at it: the sensor outputs 450 pulses per liter of water. This means each pulse represents approximately 2.25 milliliters (1000 mL / 450 pulses = 2.22 mL).
For volume calculation:
Volume (Liters) = Total Pulse Count / 450
Or more directly:
Volume (mL) = Total Pulse Count × 2.25
Basic Water Flow Sensor Arduino Code
Here’s a tested starting point for measuring flow rate:
// Water Flow Sensor YF-S201 – Basic Flow Rate Measurement
totalLiters += (flowRate / 60.0); // Convert L/min to L/sec, add to total
// Display results
Serial.print(“Flow Rate: “);
Serial.print(flowRate, 2);
Serial.print(” L/min | Total: “);
Serial.print(totalLiters, 3);
Serial.println(” L”);
previousTime = millis();
}
}
The volatile keyword on pulseCount is essential. It tells the compiler this variable can change at any time (via interrupt), preventing optimization that might cache the value incorrectly.
Advanced Code with LCD Display
For standalone applications without a computer, adding an LCD provides real-time feedback:
The default calibration factor of 7.5 works reasonably well, but individual sensors vary. For applications requiring better than ±10% accuracy, calibration against a known volume is essential:
Calibration Procedure
Connect a container with volume markings (or use a known volume container)
Modify code to display raw pulse count
Pass exactly 1 liter of water through the sensor
Record the actual pulse count
Your calibration factor = (Actual Pulses / 60)
For example, if you count 438 pulses for 1 liter:
Calibration Factor = 438 / 60 = 7.3
Replace 7.5 in your code with your measured calibration factor.
Measured Pulses/Liter
Calibration Factor
420
7.0
435
7.25
450
7.5 (default)
465
7.75
480
8.0
Common YF-S201 Variants and Alternatives
Several flow sensor variants use similar Hall effect technology:
Model
Flow Range
Pipe Size
Pulses/Liter
YF-S201
1-30 L/min
1/2″
450
YF-S402
0.3-6 L/min
1/8″
4380
YF-B1
1-25 L/min
1/2″
660
YF-B5
1-30 L/min
3/4″
21
FS300A
1-60 L/min
3/4″
330
FS400A
1-60 L/min
1″
330
The same code structure works for all variants—just adjust the calibration factor according to the specific sensor’s pulse-per-liter specification.
Troubleshooting Common Issues
From debugging numerous flow sensor installations, these problems appear most frequently:
No Pulses Detected: Verify wiring connections. Check that water is actually flowing—the sensor requires minimum 1 L/min to generate pulses. Confirm the signal wire connects to an interrupt-capable pin.
Inconsistent Readings: Ensure the sensor is installed with correct flow direction (follow the arrow). Air bubbles in the line cause erratic readings. The pipe must be completely filled with water for accurate measurement.
Readings Much Higher or Lower Than Expected: The calibration factor varies between individual sensors. Calibrate using a known volume. Also verify the interrupt is triggering on RISING edge, not FALLING or CHANGE.
Pulses Counted When No Flow: Check for electrical noise on the signal line. Add a 0.1µF capacitor between signal and ground for hardware debouncing. Ensure proper grounding between sensor and Arduino.
Flow Rate Shows Zero With Water Running: The minimum detectable flow is 1 L/min. Below this threshold, the rotor may not spin fast enough to generate consistent pulses.
Practical Applications for Water Flow Sensor Arduino Projects
Based on deployment experience, these applications work particularly well with the YF-S201:
Smart Irrigation System
Monitor water usage for garden beds or greenhouse zones. Trigger alerts when flow drops (indicating clogged lines) or exceeds normal patterns (suggesting leaks).
Water Dispenser Machine
Control dispensing by volume rather than time. Combined with a solenoid valve, you can deliver precise amounts regardless of pressure variations.
Aquarium Water Change Monitor
Track water volume during partial water changes. Prevent overfilling or draining by setting volume limits with automatic shutoff.
Coffee Machine Dosing
Measure water volume for consistent coffee brewing. The food-grade plastic variants are suitable for potable water applications.
Leak Detection System
Monitor unexpected flow during periods when no water should be running. Send alerts via WiFi (using ESP8266 or ESP32) when anomalies are detected.
Pool/Spa Flow Monitoring
Ensure circulation pumps are functioning correctly by monitoring flow rate. Alert on pump failures or filter clogs that reduce flow.
Saving Volume Data Across Power Cycles
For applications tracking total consumption, losing data on power loss is problematic. Using Arduino’s EEPROM provides non-volatile storage:
#include <EEPROM.h>
// Add to setup()
EEPROM.get(0, totalLiters);
if (isnan(totalLiters)) totalLiters = 0;
// Add periodic save in loop() (every 100 liters to preserve EEPROM life)
static float lastSaved = 0;
if (totalLiters – lastSaved >= 100) {
EEPROM.put(0, totalLiters);
lastSaved = totalLiters;
}
Note: EEPROM has limited write cycles (~100,000). Don’t save on every pulse or every second—save only when significant volume accumulates.
Useful Resources and Downloads
Datasheets and Documentation
YF-S201 Datasheet: Complete specifications and mechanical drawings
Hall Effect Sensor Theory: Understanding the detection principle
Arduino Interrupt Reference: Official documentation on attachInterrupt()
Arduino Libraries
YFS201 Library (GitHub galihru/YFS201): Simplified interface for flow measurement
LiquidCrystal_I2C: For LCD display integration
EEPROM Library: Built-in library for data persistence
Hardware Resources
YF-S201 Water Flow Sensor: Standard 1/2″ version
YF-S402 Flow Sensor: Smaller 1/8″ version for low-flow applications
I2C LCD Display Module: 16×2 character display for standalone projects
Solenoid Valve G1/2: For automated flow control applications
Frequently Asked Questions
What is the minimum flow rate the YF-S201 can measure?
The YF-S201 requires a minimum flow rate of 1 liter per minute to generate reliable pulses. Below this threshold, the water current isn’t strong enough to spin the internal rotor consistently. For applications requiring lower flow measurement, consider the YF-S402 which measures down to 0.3 L/min, though it has a smaller pipe diameter.
Can I use the YF-S201 with liquids other than water?
The YF-S201 is designed for water and compatible with most non-corrosive liquids. It works with beverages, coolants, and other water-based fluids. Avoid using it with aggressive chemicals, oils, or solvents that may damage the plastic housing or internal components. For petroleum-based liquids or corrosive chemicals, use sensors specifically rated for those applications.
Why do I need to connect the signal wire to an interrupt pin?
At maximum flow rate (30 L/min), the sensor outputs approximately 225 pulses per second. If you poll the signal in your main loop using digitalRead(), any delay from LCD updates, serial communication, or other processing will cause missed pulses and inaccurate readings. Hardware interrupts count every pulse regardless of what else the code is doing, ensuring accurate measurement.
How can I improve the accuracy beyond the ±10% specification?
Individual calibration is the key to better accuracy. Pass a known volume (measured with a graduated container or kitchen scale where 1 liter = 1 kg) through the sensor and count the actual pulses. Calculate your specific calibration factor and use it in place of the default 7.5. This typically achieves ±3-5% accuracy. Also ensure no air bubbles exist in the line and maintain consistent water pressure during calibration.
Can I use multiple flow sensors with one Arduino?
Yes, but with limitations. Arduino Uno only has two interrupt pins (D2 and D3), so maximum two sensors can be read reliably. Arduino Mega provides six interrupt pins, allowing more sensors. For each sensor, create separate pulse counter variables and interrupt service routines. Alternatively, use pin change interrupts (PCINT) which work on any digital pin, though they require more complex code.
Final Thoughts
The Water Flow Sensor Arduino combination using the YF-S201 provides an accessible entry point into liquid flow measurement. The sensor’s digital output eliminates the complexity of analog signal conditioning, while hardware interrupts ensure reliable pulse counting even during processor-intensive operations.
Success with the YF-S201 comes down to proper interrupt configuration, correct flow direction installation, and calibration for your specific application. The default ±10% accuracy works fine for monitoring and alerting applications, while individual calibration enables precision dosing and billing applications.
For projects requiring higher accuracy or different flow ranges, the same coding principles apply to other Hall effect flow sensors—just adjust the calibration factor accordingly. Whether building a smart irrigation system, water dispenser, or industrial monitoring application, the techniques covered here provide a solid foundation for reliable flow measurement.
Inquire: Call 0086-755-23203480, or reach out via the form below/your sales contact to discuss our design, manufacturing, and assembly capabilities.
Quote: Email your PCB files to Sales@pcbsync.com (Preferred for large files) or submit online. We will contact you promptly. Please ensure your email is correct.
Notes: For PCB fabrication, we require PCB design file in Gerber RS-274X format (most preferred), *.PCB/DDB (Protel, inform your program version) format or *.BRD (Eagle) format. For PCB assembly, we require PCB design file in above mentioned format, drilling file and BOM. Click to download BOM template To avoid file missing, please include all files into one folder and compress it into .zip or .rar format.