

The Water Flow Rate Sensor YF-S401 is a compact and reliable device designed to measure the flow rate of water in a pipe. It operates by generating electrical pulses that correspond to the volume of water passing through the sensor. These pulses can be read and processed by a microcontroller or other electronic systems to calculate the flow rate and total volume of water.








The following table outlines the key technical details of the YF-S401 sensor:
| Parameter | Specification |
|---|---|
| Operating Voltage | 5V to 18V DC |
| Operating Current | ≤ 15 mA (at 5V DC) |
| Flow Rate Range | 1 to 30 L/min |
| Output Pulse Frequency | F = 7.5 * Q (Q = flow rate in L/min) |
| Maximum Water Pressure | 1.75 MPa |
| Operating Temperature | -25°C to 80°C |
| Accuracy | ±5% |
| Output Signal | Pulse signal (digital) |
The YF-S401 sensor has three wires for connection:
| Wire Color | Pin Name | Description |
|---|---|---|
| Red | VCC | Power supply (5V to 18V DC) |
| Black | GND | Ground connection |
| Yellow | Signal | Pulse output signal proportional to flow rate |
// YF-S401 Water Flow Rate Sensor Example Code
// This code reads pulses from the sensor and calculates the flow rate in L/min.
volatile int pulseCount = 0; // Variable to store pulse count
float flowRate = 0.0; // Variable to store flow rate in L/min
unsigned long lastTime = 0; // Variable to store the last time flow was calculated
void setup() {
pinMode(2, INPUT_PULLUP); // Set pin 2 as input with pull-up resistor
attachInterrupt(digitalPinToInterrupt(2), pulseCounter, RISING);
// Attach interrupt to count pulses on pin 2
Serial.begin(9600); // Initialize serial communication
}
void loop() {
unsigned long currentTime = millis(); // Get current time in milliseconds
if (currentTime - lastTime >= 1000) { // Calculate flow rate every second
noInterrupts(); // Disable interrupts to safely access pulseCount
int pulses = pulseCount; // Copy pulse count to a local variable
pulseCount = 0; // Reset pulse count
interrupts(); // Re-enable interrupts
flowRate = (pulses / 7.5); // Calculate flow rate in L/min
Serial.print("Flow Rate: ");
Serial.print(flowRate);
Serial.println(" L/min");
lastTime = currentTime; // Update the last time flow was calculated
}
}
// Interrupt service routine to count pulses
void pulseCounter() {
pulseCount++; // Increment pulse count on each rising edge
}
No Output Signal:
Inaccurate Readings:
Intermittent Signal Drops:
Q: Can the YF-S401 measure other liquids besides water?
A: The sensor is designed for water and may not provide accurate readings with other liquids. Additionally, using it with corrosive or viscous liquids may damage the sensor.
Q: How do I calculate the total volume of water?
A: Multiply the flow rate (L/min) by the time duration (in minutes) to get the total volume. Alternatively, integrate the pulse count over time.
Q: Can I use the YF-S401 with a 3.3V microcontroller?
A: Yes, but you may need a level shifter or voltage divider to safely interface the signal output with the microcontroller.