Inline Flow Sensor with Arduino

Hi,

I am doing a project where I will use a flow sensor to measure the fuel consumption of an engine. However, I am only doing a test setup for now and the liquid I am using is just water. I am using a flow sensor with the following specifications:

I am using a code I found from a blog (see Flow Sensor code. Although the person is using a different flow sensor from mine.

I am actually getting a value but when I compare the value using the volumetric method (I collect the water and record the volume from a predetermined time of 10 secs). The value I am getting from the Arduino code is smaller compared to the volumetric method. For example, when I get 24 L/h from the code, my calculated flow rate using the volumetric method is 115 L/h. It's too big of a margin.

I already tried configuring the code a little bit by changing the "7.5" to 9. I obtained the value 9 from the specs of the flow sensor (180 Hz / 20 L/min = 9). Although I have no idea if that is correct.

I'd like to ask for advice in order to properly calibrate the values I am getting from the sensor.

Thank you!

Code:

/*
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 pulsesunsigned 

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");
   }
}

I once installed a flow sensor to the petrol line into the carburettor in the car.
One important piece of data was the diagram showing pulses versus flow rate. That sensor was highly none linear.
Can You provide those data for that sensor?

Hi,

Unfortunately, the datasheet of the sensor does not provide the diagram showing the pulses versus flow rate. I am still trying to contact the manufacturer for the said diagram. For now, I only have this kind of diagram to show:

Thank you for your response.

Hi,

how about this information. It is in tabular form.

Thank you!

Your code needs a bit of work. You have to turn off interrupts when accessing your counter variable.

/*
  YF‐ S201 Water Flow Sensor
  Water Flow Sensor output processed to read in litres/hour
  Adaptation Courtesy: www.hobbytronics.co.uk
*/

volatile unsigned int flow_frequency; // Measures flow sensor pulsesunsigned

const byte flowsensor = 2; // Sensor Input

unsigned long cloopTime;

void flow () // Interrupt function
{
  flow_frequency++;
}

void setup()
{
  //pinMode(flowsensor, INPUT);
  //digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
  pinMode(flowsensor, INPUT_PULLUP);
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(flowsensor), flow, RISING); // Setup Interrupt
  cloopTime = millis();
}

void loop ()
{
  unsigned int flow_count;
  
  unsigned long 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.
    noInterrupts();
    flow_count = flow_frequency;
    flow_frequency = 0; // Reset Counter
    interrupts();
    
    float l_hour = flow_count * 60.0 / 7.5;
    Serial.print(l_hour, 2); // Print litres/hour
    Serial.println(" L/hour");
  }
}

Based on that chart and whatever port size you have, you may have to do some sort of lookup table and then interpolate between the two closest points

Hi, I will look into your suggestions. Thank you!

From rough calcs the sensor output is non-linear, and the flow rate/frequency output is determined by the output plumbing sizing.
You would need to do testing of measureing the freq out at various flow rates to apply the Kfactor into the formula,utilizing the pipe sizing of the final application.
The charting is indicating flow rate in gallons per minute , therefore you need to determine if it is US gal. or Imperial and convert to Litres.
The non-linear output could be applied into the Multi Map function to provide an accurate output off L/min.
Bit of work faffing around but quite doable.........HTH.

So what is this secret engine doing? The chart suggests it is quite large and hard working.