Arduino Projects

Water Flow Sensor with Arduino to Measure Flow Rate & Volume

Learn how to interface water flow sensor with Arduino to measure water flow rate an calculate the volume of water passed.

Introduction:

Have you ever wondered how to accurately measure the flow of water and calculate the volume passing through a sensor? Look no further! In this Arduino project, we will learn what is a water flow sensor, how a water flow sensor works, and how to interface it with Arduino to measure the water flow rate and calculate the volume of the water passed through the sensor.

This project offers a fantastic opportunity to explore the realm of sensor-based measurements, combining the power of Arduino with the practicality of water flow sensors. Whether you are a beginner or an experienced enthusiast, this guide will equip you with the knowledge and skills needed to embark on your water flow measurement adventures. So, let’s start by learning what is water flow sensor and how it works.

What is a Water Flow Sensor and how it works

A water flow sensor is a device that measures the flow rate or volume of water passing through it. It is a vital component in various applications, including industrial systems, irrigation systems, vending machines, water dispensers, smart home automation, and Tankless water heaters. Understanding the functionality of a water flow sensor is crucial for effectively monitoring and controlling water flow.

water flow sensors for diy projects

The operation of a water flow sensor is based on the principle of measuring the velocity of water passing through it. Most water flow sensors utilize a paddlewheel or turbine mechanism. As water flows through the sensor, the paddlewheel or turbine rotates, generating electrical signals proportional to the flow rate.

These electrical signals are then processed by the sensor’s internal circuitry and converted into measurable data. The output can be in the form of analog voltage, frequency, or digital pulses, depending on the sensor type. This data can be interfaced with microcontrollers like Arduino for further analysis and control.

Water flow sensors may also incorporate additional features such as temperature sensors or pressure sensors to provide more comprehensive measurements. These sensors can be calibrated to ensure accurate readings and compensate for any environmental factors that may affect the measurements.

In this project we are using YF-S201 hall effect flow sensor, there are other sensors like FS300A, FS400A which works in the same way but has different flow rate capacities.

YF-S201 Hall effect water flow sensor

The YF-S201 Hall effect water flow sensor operates on the principle of the Hall effect, which detects the presence and strength of a magnetic field. Inside the sensor, a small paddlewheel or turbine rotates as water flows through it. The rotation of the paddlewheel generates a magnetic field variation, which is sensed by the Hall effect sensor which is packed and sealed for waterproofing to stay dry from water. This generates a corresponding electrical signal that is proportional to the flow rate of the water.

yf-s201 water flow sensor for arduino

The YF-S201 Hall effect water flow sensor for arduino simplifies water flow measurement with its three-wire configuration. The sensor’s wires include red for power (5-24VDC), black for ground, and yellow for Hall effect pulse output. By accurately counting the pulses from the sensor’s output, you can easily determine the water flow rate. Each pulse corresponds to approximately 2.25 milliliters of water. It’s important to note that while this sensor is reliable for basic measurement tasks, it may not provide precision accuracy due to variations in pulse rate caused by factors like flow rate, fluid pressure, and sensor orientation. For applications requiring better than 10% precision, meticulous calibration is recommended. Nonetheless, the YF-S201 Hall effect water flow sensor is an excellent choice for straightforward measurement needs.

For more details refer the YF-S201 flow sensor datasheet: Here.

Specifications:

  • Working Voltage: DC 4.5V~24V
  • Normal Voltage: DC 5V~18V
  • Output: 5V TTL
  • Accuracy: ±10%
  • Max. Working Current: 15mA (DC 5V)
  • Load capacity: ≤ 10 mA (DC 5V)
  • Flow Rate Range: 1~30L/min
  • Pulses per Litre: 450
  • Load Capacity: ≤10mA (DC 5V)
  • Operating Temperature: ≤80℃
  • Liquid Temperature: ≤120℃
  • Operating Humidity: 35%~90%RH
  • Allowing Pressure: ≤1.75MPa
  • External threads: 1/2″
  • Durability: Over 300000 cycles

Working of YFS201 Water Flow Sensor

YF-S201 water flow sensor to measure the flow rate and volume of the liquid works on the principle of Hall Effect. The Hall effect is a phenomenon that occurs when a magnetic field is applied perpendicular to the direction of an electric current. It generates a voltage difference perpendicular to both the current and magnetic field.

working of water flow sensor with internal parts

This effect is utilized in YF-S201, to detect changes in magnetic fields and convert them into electrical signals for water flow measurements. In YF-S201 Hall effect water flow sensor, the rotation of the paddlewheel or turbine creates a magnetic field variation that is sensed by the Hall effect sensor, generating pulses proportional to the flow rate. These pulses are sent to microcontrollers like Arduino to calculate the Flow rate.

The pulse output is a square wave whose frequency is proportional to the flow rate. The conversion factor from frequency (Hz) to flow (L/min) varies between models and depends on the pressure, density and even the flow itself.

In the case of the ½” sensor, the average conversion factor provided by the manufacturer is:

f(Hz)=7.5 x Q(L/min)

We will call the conversion factor K, where K=7.5 for the ½” sensor, K=5.5 for the ¾” sensor and 3.5 for the 1” sensor, working with these values ​​does not guarantee precision, but they can be useful. For simple applications, if we need greater accuracy, we need to calibrate and calculate this factor.

Interfacing YF-s201 water flow sensor with Arduino

Required Components

Product NameQuantityamazon logoamazon logo india
Arduino Microcontroller1https://amzn.to/3H4cKxZhttps://amzn.to/3638aTS
YF-S201 Hall effect water flow sensor1https://amzn.to/42TlD9yhttps://amzn.to/41VeJ2j
16X2 LCD display with I2C adapter1https://amzn.to/3WnDDXhhttps://amzn.to/3Ou14ME
5V power supply (USB or External).1https://amzn.to/3s1a8g3https://amzn.to/364yInH
Few Connecting Wireshttps://amzn.to/3H2BV4ehttps://amzn.to/3J0WVu2
You can buy the required components from the given best buy links. We choose the components according to value for money.

Circuit Diagram

Now connect all the required components by following the below circuit diagram

interfacing water flow sensor with arduino to measure flow rate on lcd display

 

As you can see from the above schematic diagram water flow sensor is powered from the 5v and GND pins of Arduino using Red and Black wires respectively. The Output yellow wire is connected to the digital pin 2 of Arduino. We use this pin because in the program we are going to use the external interrupt. Arduino Uno only has external interrupts on pins 2 and 3. We can use the same connection if we are working with an Arduino Nano, Mini, Mega or Micro, since all these boards have an external interrupt on pin 2.

The LCD display is also powered from 5v and GND pins of Arduino respectively, and I2C pins SDA and SCL are connected to A4 and A5 of Arduino respectively. Thats it for connections now we can move to the coding part.

Source code to measure Flow rate and Volume of water

Connect Arduino board to the computer where Arduino IDE is installed, Open new project, Copy the below code and paste it in the workspace. Install the required libraries from the below list. finally Compile and Upload the code.

Required Libraries:

  • 16X2 LCD display with I2C Adapter : Here
//Source code for measuring water Flow rate and calculating volume of water passed
#include <LiquidCrystal_I2C.h>
volatile  int  NumPulses ;  //variable for the number of pulses received 
int  PinSensor  =  2 ;     //Sensor connected to pin 2 
float  factor_conversion = 7.5 ;  //to convert from frequency to flow rate 
float  volume = 0 ; 
long dt = 0 ; //time variation for each loop 
long t0 = 0 ; //millis() from the previous loop 
  
// Create the lcd object address 0x27 and 16 columns x 2 rows 
LiquidCrystal_I2C lcd (0x27, 16,2);  //

//---Function executed in interrupt--------------- 
void  PulseCount  ( )   
{  
  NumPulses ++ ;   //increment the pulse variable 
} 

//---Function to obtain pulse frequency-------- 
int  GetFrequency ( )  
{ 
  int  frequency ; 
  NumPulses  =  0 ;    //We set the number of pulses to 0 
  interrupts ( ) ;     // We enable the interruptions 
  delay ( 1000 ) ;    //sample for 1 second 
  noInterrupts ( ) ;  // We disable the interruptions 
  frequency = NumPulses ;  //Hz(pulses per second) 
  return  frequency ; 
}

void  setup ( )  
{ 
  
  Serial . begin ( 9600 ) ;  
   // Initialize the LCD connected 
  lcd.init();
  // Turn on the backlight on LCD. 
  lcd. backlight ();
  pinMode ( PinSensor ,  INPUT ) ;  
  attachInterrupt ( 0 , PulseCount , RISING ) ; //(Interrupt 0(Pin2),function,rising edge) 
  Serial . println  ( "Send 'r' to reset the volume to 0 Liters" ) ;  
  t0 = millis ( ) ; 
} 

void  loop  ( )     
{ 
  if  ( Serial . available ( ) )  { 
    if ( Serial . read ( ) == 'r' ) volume = 0 ; //reset the volume if we receive 'r' 
  } 
  float  frequency = GetFrequency ( ) ;  //we obtain the frequency of the pulses in Hz 
  float  flow_L_m = frequency / factor_conversion ; //calculate the flow in L/m 
  dt = millis ( ) - t0 ;  //calculate the time variation 
  t0 = millis ( ) ; 
  volume = volume + ( flow_L_m / 60 ) * ( dt / 1000 ) ;  // volume(L)=flow(L/s)*time(s)

   //-----Send through the serial port--------------- 
  Serial.print("Flow: ");  
  Serial.print(flow_L_m,3);  
  Serial.print("L/min\tVolume: ");  
  Serial.print(volume,3) ;  
  Serial.println( "L" ) ; 
//-----send to LCD display ------
lcd.print("Flow: ");
lcd.print(flow_L_m,2);
lcd.print("L/m");
lcd.setCursor (0, 1);
lcd.print("Volume: ");
lcd.print(volume,2);
lcd.print("L"); 
}

After uploading the code to the Arduino, open the serial monitor with baud rate 9600, where we can see the flow rate and the volume, to reset the volume we must send the letter r and the volume will start from 0.

water flow sensor output on serial monitor

The LCD display also displays the water flow rate and volume of the water passed as shown in the below image.

water flow sensor output water flowrate and volume on LCD display

Caliberate Arduino water flow sensor

Sometimes, the measured value of the flow may not be perfectly accurate. To verify the accuracy, one method is to maintain a nearly constant flow and fill a container for exactly one minute. The amount of water collected should align with the expected flow rate in liters per minute. For instance, if the desired flow rate is 8 liters per minute, the container should contain approximately 8 liters after one minute.

If the measured value does not match the expected value, one approach to improve accuracy is to adjust the conversion factor. By increasing or decreasing the conversion factor, a more precise measurement can be achieved, bringing the measured value closer to the actual flow rate. Iterative adjustments to the conversion factor allow for fine-tuning the measurement and obtaining a more accurate result.

Be Part of Our Creative Journey: Subscribe to Circuit Schools YouTube Channel for Inspiring Projects! Join the Discussion in the Comment Section below if you have any doubts or questions!

CircuitSchools Staff

We at CircuitSchools publish on basics of electronics and electric components and everything related to evolution of electrical technology with complete analysis of development boards and modules along with latest projects with innovative ideas.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button