Waterflow sensor

HI,

I only have basic knowledge in arduino and I want to use a hall effect sensor to measure water flow through my laser tube. I have found many codes for the purpose but I find it hard to understand. Can any one give me a well explained code and also explain equations used in calculating flow rate

this is the equation that I found
Sensor Frequency (Hz) = 7.5 * Q (Liters/min)

and then this one in most codes(why the need of q in denominator)

Pulse frequency x 60) / 7.5Q, = flow rate

Thanks

Example code
http://www.seeedstudio.com/wiki/G1/2_Water_Flow_sensor

// reading liquid flow rate using Seeeduino and Water Flow Sensor from Seeedstudio.com
// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com
// http:/themakersworkbench.com http://thebestcasescenario.com http://seeedstudio.com

volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;                               
int hallsensor = 2;    //The pin location of the sensor

void rpm ()     //This is the function that the interupt calls 
{ 
 NbTopsFan++;  //This function measures the rising and falling edge of the 

hall effect sensors signal
} 
// The setup() method runs once, when the sketch starts
void setup() //
{ 
 pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
 Serial.begin(9600); //This is the setup function where the serial port is 

initialised,
 attachInterrupt(0, rpm, RISING); //and the interrupt is attached
} 
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop ()    
{
 NbTopsFan = 0;   //Set NbTops to 0 ready for calculations
 sei();      //Enables interrupts
 delay (1000);   //Wait 1 second
 cli();      //Disable interrupts
 Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate 

in L/hour 
 Serial.print (Calc, DEC); //Prints the number calculated above
 Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line
}

I think Q is similar to what others call the meter K factor

Flow sensors have historically sensed a metal turbine, or sometimes a magnet connected to a turbine or paddle wheel. When it passes the sensor a pulse is made. Each pulse represents a volume of the fluid if operating within the sensors range. At slow flow, some may partly bypass the turbine and not cause it to move enough to accurately measure the flow. Fast flows may cause turbulence and even damage the turbine. The meter should say what the max flow is, I advise not going much over it. There is no harm with a low flow rate, but the readings may start to go wrong at about 10% of the rated max.

How much accuracy you want will determine how you will measure the pulse times, and if you need to calibrate the flow meter. Most are not very accurate because that requires machining the turbine and the flow cross-section precisely. AthulSNair has shown an Interrupt method, which is most likely what you want to do. I would like to point out that the atmega328p is equipped with Input CaPture hardware (ICP1) which can improve the time measurement. When an Interrupt starts it takes about 50 machine cycles to save registers before it can run the interrupt code, and other interrupts can occur just before the pin interrupt and inject 100 machine cycles (or more) into the measurement. ICP removes all those timing errors since it instantly captures the timer value into a separate holding register when the pin changes level. That ICP holding register means the microcontroller can take the time it needs and still get the time of the pulse to within a machine cycle (16MHz).

@Nick_Pyner

I already got several programs

So this is what I think about the math behind the operation of sensor , please correct me if I'm wrong.

For every liter water flow , sensor will produce 4.5 Pulses(as per your program, some other programs use 7.5) and by counting number of pulses for a minute and dividing it by 4.5 will give the amount of water in liters passed per minute.

By divding it with 60 will give number of liters per hour, right??

 // 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;

can you please explain this step?

I found another code

water flow sensor

/*
YF‐ S201 Water Flow Sensor
Water Flow Sensor output processed to read in litres/hour
Adaptation Courtesy: www.hobbytronics.co.uk
*/
volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
   flow_frequency++;
}
void setup()
{
   pinMode(flowsensor, INPUT);
   digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
   Serial.begin(9600);
   attachInterrupt(0, flow, RISING); // Setup Interrupt
   sei(); // Enable interrupts
   currentTime = millis();
   cloopTime = currentTime;
}
void loop ()
{
   currentTime = millis();
   // Every second, calculate and print litres/hour
   if(currentTime >= (cloopTime + 1000))
   {
      cloopTime = currentTime; // Updates cloopTime
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
      flow_frequency = 0; // Reset Counter
      Serial.print(l_hour, DEC); // Print litres/hour
      Serial.println(" L/hour");
   }
}

as per his equn
Pulse frequency (Hz) = 7.5Q, Q is flow rate in Litres/minute
Flow Rate (Litres/hour) = (Pulse frequency x 60 min) / 7.5Q

I have a doubt in it, Why is there Q in denominator ??

AthulSNair:
For every liter water flow , sensor will produce 4.5 Pulses(as per your program, some other programs use 7.5) and by counting number of pulses for a minute and dividing it by 4.5 will give the amount of water in liters passed per minute.

By divding it with 60 will give number of liters per hour, right??

No, you multiply by 60. But if you really need to know the flow per hour, why don't you count the pulses per hour?

 // 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;



can you please explain this step?

It is what it says, the count time may not be exactly a second it may be slightly over

void loop() {
      if((millis() - oldTime) > 1000)

doesn't say "if it is a second, do", it says"if it is over a second, do", and it may be 1.002 second.

Pulse frequency (Hz) = 7.5Q, Q is flow rate in Litres/minute
Flow Rate (Litres/hour) = (Pulse frequency x 60 min) / 7.5Q
I have a doubt in it, Why is there Q in denominator ??

Good question - the equn is nonsense and, on the strength of that, I wouldn't have anything to do with the programme, or Hobbytronics.

Since

Hz=7.5Q

then Q=Hz/7.5 and so if Hz=75 then Q=10 l/min and so how hard can it be to determine that

flow rate l/hr = Q*60 = 600?

But (75 * 60)/(7.5 * 10) = 60

Exactly what you want to do is not clear. If you want rate of flow, using the interrupt pin like everybody else does is probably a good idea. If you just want flow, then simply counting the pulses should suffice.

Further, I submit that

   // Every second, calculate and print litres/hour

is probably a sign that the programmer has no actual experience of these devices. The output varies so much that reading it at one second intervals is a futile exercise, even on a serial monitor, but the overall output over a sensible period is remarkably accurate. You may be better off displaying an average over ten seconds, or having a ten second count window. You could use the same readings to count for one hour update, which would be very accurate.

@Nick_Pyner

To avoid confusion, that equation is correct without Q in the denominator, right?

Pulse frequency (Hz) = 7.5Q, Q is flow rate in Litres/minute
Flow Rate (Litres/hour) = (Pulse frequency x 60 min) / 7.5

and by doing this I would get amount of liters passed through the tube per hour

can you take a look at these sites to make sure i haven't missed anything, this where I got the program
water flow sensor

This is from arduino forum itself which gives the same explanation in the program
water flow sensor

// reading liquid flow rate using Seeeduino and Water Flow Sensor from Seeedstudio.com
// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com
// http:/themakersworkbench.com http://thebestcasescenario.com http://seeedstudio.com

volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;                               
int hallsensor = 2;    //The pin location of the sensor

void rpm ()     //This is the function that the interupt calls 
{ 
 NbTopsFan++;  //This function measures the rising and falling edge of the 

hall effect sensors signal
} 
// The setup() method runs once, when the sketch starts
void setup() //
{ 
 pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
 Serial.begin(9600); //This is the setup function where the serial port is 

initialised,
 attachInterrupt(0, rpm, RISING); //and the interrupt is attached
} 
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop ()    
{
 NbTopsFan = 0;      //Set NbTops to 0 ready for calculations
 sei();            //Enables interrupts
 delay (1000);      //Wait 1 second
 cli();            //Disable interrupts
 Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate 

in L/hour 
 Serial.print (Calc, DEC); //Prints the number calculated above
 Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line
}

Nick_Pyner:
Good question - the equn is nonsense and, on the strength of that, I wouldn't have anything to do with the programme, or Hobbytronics.

Since

Hz=7.5Q

then Q=Hz/7.5 and so if Hz=75 then Q=10 l/min and so how hard can it be to determine that

flow rate l/hr = Q*60 = 600?

But (75 * 60)/(7.5 * 10) = 60

this may be a stupid question if Hz = 75 the Q(liter per hour) = 600, right?

Then why did you put a question mark

AthulSNair:
To avoid confusion, that equation is correct without Q in the denominator, right?

Yes

can you take a look at these sites to make sure i haven't missed anything, this where I got the program
water flow sensor

No. I don't understand why that approach merits consideration and, as I said, I wouldn't have anything to do with the programme, or Hobbytronics.

This is from arduino forum itself which gives the same explanation in the program
water flow sensor

 attachInterrupt(0, rpm, RISING); //and the inte

Suggests this code is similar to that which I alluded toin reply #2

this may be a stupid question if Hz = 75 the Q(liter per hour) = 600, right?

Then why did you put a question mark

Because I was asking "how hard can it be to determine that flow rate l/hr = Q*60 = 600?" which is a question that should be asked of and by the idiot who wrote the programme.

Interestingly, this thread had spawned another discussion (on StackExchange).

Oh dear....

Is my comment "I wouldn't have anything to do with the programme, or Hobbytronics." still valid?

And I wonder how Athul is faring with his project.