Arduino Projects Water Flow Meter

Arduino UNO connected to a Water Flow Sensor (YF-S201).
This sensor outputs pulses proportional to the water flow rate. The Arduino will count the pulses and calculate flow rate and volume.

Circuit

Required of Arduino Projects Water Flow Meter

  • Arduino Uno
  • Flow Sensor
  • Connecting wires
  • LCD Display
  • 1K Ohm Resistor

Code for Water Flow Sensor (YF-S201)

// Water Flow Sensor with Arduino UNO
// Sensor: YF-S201
// Pin Connections: Red = VCC (5V), Black = GND, Yellow = Signal (D2)

volatile int pulseCount;  
float flowRate;  
unsigned int flowMilliLitres;  
unsigned long totalMilliLitres;  
unsigned long oldTime;  

const int flowSensorPin = 2;  // Signal pin connected to D2 (Interrupt Pin)

void setup() {
  Serial.begin(9600);

  pinMode(flowSensorPin, INPUT_PULLUP);  
  pulseCount = 0;  
  flowRate = 0.0;  
  flowMilliLitres = 0;  
  totalMilliLitres = 0;  
  oldTime = 0;

  // Attach interrupt on D2
  attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING);
}

void loop() {
  unsigned long currentTime = millis();

  // Process every 1 second
  if (currentTime - oldTime > 1000) {
    detachInterrupt(digitalPinToInterrupt(flowSensorPin));

    // Flow rate in Litres/minute
    flowRate = ((1000.0 / (currentTime - oldTime)) * pulseCount) / 7.5;

    oldTime = currentTime;

    // Convert to millilitres (mL)
    flowMilliLitres = (flowRate / 60) * 1000;

    // Accumulate total volume
    totalMilliLitres += flowMilliLitres;

    // Print values
    Serial.print("Flow Rate: ");
    Serial.print(flowRate);
    Serial.print(" L/min");

    Serial.print("  Current Flow: ");
    Serial.print(flowMilliLitres);
    Serial.print(" mL/s");

    Serial.print("  Total Volume: ");
    Serial.print(totalMilliLitres);
    Serial.println(" mL");

    // Reset pulse counter
    pulseCount = 0;

    attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, FALLING);
  }
}

// Interrupt Service Routine (ISR)
void pulseCounter() {
  pulseCount++;
}

Explanation

  1. Sensor Output
    • The sensor has a Hall-effect sensor inside that generates pulses when water flows.
    • The frequency of pulses is proportional to the flow rate.
    • For YF-S201:
      Flow (L/min) = Pulse frequency (Hz) / 7.5
  2. Connections
    • Red โ†’ 5V
    • Black โ†’ GND
    • Yellow โ†’ D2 (digital pin with interrupt)
  3. Code Working
    • Uses interrupt on pin D2 to count pulses.
    • Calculates flow rate every 1 second.
    • Tracks total water volume in mL.

Output of Arduino Projects Water Flow Meter:

In Serial Monitor

Flow Rate: 2.53 L/min  Current Flow: 42 mL/s  Total Volume: 168 mL
Flow Rate: 2.50 L/min  Current Flow: 41 mL/s  Total Volume: 209 mL
Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off