Devices & Components
Arduino Uno Rev3
YF-S201 water Flow Sensor
Software & Tools
Arduino IDE
Project description
Code
Program of YF-S201 Sensor and Arduino
arduino
1//Coded and Tested By: 2// Sheekar Banerjee, AI-ML-IOT Solution Engineer and Researcher 3 4/* 5Arduino Water flow meter 6YF- S201 Hall Effect Water Flow Sensor 7Water Flow Sensor output processed to read in litres/hour 8*/ 9volatile int flow_frequency; // Measures flow sensor pulses 10unsigned int l_hour; // Calculated litres/hour 11unsigned char flowsensor = 2; // Sensor Input 12unsigned long currentTime; 13unsigned long cloopTime; 14void flow () // Interrupt function 15{ 16flow_frequency++; 17} 18void setup() 19{ 20pinMode(flowsensor, INPUT); 21digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up 22Serial.begin(9600); 23attachInterrupt(0, flow, RISING); // Setup Interrupt 24 sei(); // Enable interrupts 25currentTime = millis(); 26cloopTime = currentTime; 27} 28void loop () 29{ 30currentTime = millis(); 31 // Every second, calculate and print litres/hour 32if(currentTime >= (cloopTime + 1000)) 33{ 34 cloopTime = currentTime; // Updates cloopTime 35 36 // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min. 37 l_hour = (flow_frequency * 60 / 7.5); 38 // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour 39 40 flow_frequency = 0; // Reset Counter 41 Serial.print(l_hour, DEC); // Print litres/hour 42 Serial.println(" L/hour"); 43} 44}
Downloadable files
Circuit Diagram of YF-S201 Water Flow Sensor and Arduino
Circuit Diagram of YF-S201 Water Flow Sensor and Arduino
Circuit Diagram of YF-S201 Water Flow Sensor and Arduino
Circuit Diagram of YF-S201 Water Flow Sensor and Arduino
Comments
Only logged in users can leave comments