YF-S201 Water Flow Sensor with Arduino Tutorial

Introduction

In this tutorial, we learn how to interface YF-S201 Water Flow Sensor with Arduino Tutorial, Monitoring water flow is essential in various applications, ranging from measuring water consumption to controlling irrigation systems. The YF-S201 Water Flow Sensor, in combination with an Arduino Uno, offers a reliable and accurate solution for measuring water flow rates. In this tutorial, we will explore how to interface the YF-S201 Water Flow Sensor with Arduino, understand its working principle, and build a basic water flow monitoring system.

Hardware Required

To utilize the YF-S201 Water flow Sensor with Arduino Tutorial, you will need the following components:

Components#Buy From Amazon
Arduino UNO1Buy Now
YF-S201 Water flow Sensor1Buy Now
Jumper Wires3Buy Now
Breadboard1Buy Now

Understanding the YF-S201 Water Flow Sensor

The YF-S201 Water Flow Sensor is a hall-effect sensor designed to measure the rate of water flow passing through it. It utilizes the principle of the Hall effect, which involves the detection of changes in a magnetic field caused by the flow of fluid. This sensor generates electrical pulses proportional to the volume of water passing through it, allowing us to calculate the flow rate.

Pinout

YF-S201-Water-Flow-Sensor

Pin Configuration

ColourPin typeFunction
RedPowerTo provide power to the module
BlackGroundConnected to the ground terminal
YellowSignalAnalog output from the sensor

Circuit Setup

  1. Connect the VCC pin of the YF-S201 sensor to the 5V pin on the Arduino Uno.
  2. Connect the GND pin of the sensor to the GND pin on the Arduino Uno.
  3. Connect the signal pin of the sensor to any digital input pin on the Arduino Uno.
  4. Connect a 10K ohm pull-up resistor between the signal pin of the sensor and the 5V pin on the Arduino Uno.

Circuit Diagram

The following circuit shows you the connection of the YF-S201 Water Flow Sensor with Arduino Please make the connection carefully

Interfacing-YF-S201-Transparent-Water-Flow-Sensor-with-Arduino

Working Principle

The YF-S201 Water Flow Sensor contains a small turbine wheel that spins as water flows through it. The turbine wheel is equipped with a magnet, and the sensor has a hall-effect sensor adjacent to it. As the wheel spins, the magnet induces changes in the magnetic field around the hall-effect sensor, generating electrical pulses. The frequency of these pulses is directly proportional to the flow rate of the water.

Coding and Data Interpretation

To interface the YF-S201 Water Flow Sensor with Arduino, follow these steps:

  1. Set up the necessary variables: Declare variables to store the flow sensor pin number, pulse count, time interval, and flow rate.
  2. Configure the pinMode: In the setup() function, configure the digital input pin connected to the sensor as an input using the pinMode() function.
  3. Attach an interrupt: Use the attachInterrupt() function to enable an interrupt on the digital input pin. This will count the pulses generated by the sensor.
  4. Calculate flow rate: In the interrupt service routine (ISR), update the pulse count and calculate the flow rate based on the known characteristics of the sensor.
  5. Display results: Print or display the flow rate using the Serial Monitor or an LCD display connected to the Arduino Uno.

Circuit Connections

ArduinoWater Flow Sensor
+5VVCC Pin
GNDGND Pin
D2Signal Pin

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow

Code

//For more Projects: www.arduinocircuit.com

byte statusLed    = 13;

byte sensorInterrupt = 0;  // 0 = digital pin 2
byte sensorPin       = 2;

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;

volatile byte pulseCount;  

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

unsigned long oldTime;

void setup()
{
  
  // Initialize a serial connection for reporting values to the host
  Serial.begin(9600);
   
  // Set up the status LED line as an output
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached
  
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

/**
 * Main program loop
 */
void loop()
{
   
   if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(sensorInterrupt);
        
    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    
    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = millis();
    
    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    flowMilliLitres = (flowRate / 60) * 1000;
    
    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitres += flowMilliLitres;
      
    unsigned int frac;
    
    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(int(flowRate));  // Print the integer part of the variable
    Serial.print("L/min");
    Serial.print("\t"); 		  // Print tab space

    // Print the cumulative total of litres flowed since starting
    Serial.print("Output Liquid Quantity: ");        
    Serial.print(totalMilliLitres);
    Serial.println("mL"); 
    Serial.print("\t"); 		  // Print tab space
	Serial.print(totalMilliLitres/1000);
	Serial.print("L");
    
    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;
    
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }
}

/*
Insterrupt Service Routine
 */
void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

Applications

The YF-S201 Water Flow Sensor can be utilized in various applications, including:

  1. Water consumption monitoring: Measure the amount of water consumed in residential or industrial settings to promote water conservation.
  2. Irrigation systems: Monitor and control the flow of water in agricultural irrigation systems, ensuring optimal water distribution.
  3. Leak detection: Detect leaks in plumbing systems by monitoring abnormal water flow rates.
  4. Aquariums and hydroponics: Monitor and maintain water flow rates in aquariums or hydroponic systems for optimal plant growth and fish health.
  5. Industrial processes: Measure water flow rates in industrial processes to optimize production efficiency and resource utilization.

Conclusion

The YF-S201 Water Flow Sensor, in conjunction with Arduino, provides a practical solution for water flow monitoring and control. By following this tutorial and understanding its working principle, you can easily integrate the sensor into your projects and gather valuable data about water flow rates. Whether it’s for water conservation, irrigation systems, or industrial applications, the YF-S201 Water Flow Sensor proves to be a versatile tool in accurately measuring water flow.

Leave a Comment


error: