Arduino with flow sensor and lcd display

I am writing arduino code for flow sensor and lcd display.
Below is my code and lcd showing 40 l/h constant value and never change.
Anybody can help me?I am very new with arduino programming.

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
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++;
}
//flow_frequency = 0;
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;
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Flow Meter!");
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print litres/hour
if(currentTime >= (cloopTime + 1000))
{
//flow_frequency = 0;
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
lcd.setCursor(0, 1);
lcd.print(l_hour,DEC);
lcd.print (" L/hour");
Serial.print(l_hour, DEC); // Print litres/hour
Serial.println(" L/hour");
// print the number of seconds since reset:

}
}

can you share the flow sensor data sheet please?

flow sensor model is YFS-201

YF-S201.pdf (50 KB)

If tweaked your code slightly... try it out and let us know if there is any improvement

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
volatile int flow_frequency=0; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long cloopTime;

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

void setup()
{
   pinMode(flowsensor, INPUT);
   Serial.begin(9600);
   attachInterrupt(0, flow, RISING); // Setup Interrupt
   cloopTime = millis();
   // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Flow Meter!");
}
void loop ()
{
   // Every second, calculate and print litres/hour
   if(millis()-cloopTime >= 1000)
   {
      cloopTime = millis(); // Updates cloopTime
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      l_hour = flow_frequency * 8; // (Pulse frequency x 60 min) / 7.5Q = Pulse frequency*8 =flowrate in L/hour
      flow_frequency = 0; // Reset Counter
      lcd.setCursor(0, 1);
      lcd.print(l_hour,DEC);
      lcd.print (" L/hour");
      Serial.print(l_hour, DEC); // Print litres/hour
      Serial.println(" L/hour");
       // print the number of seconds since reset:
 
   }
}

Also how are you checking for different flowrate?

sherzaad:
If tweaked your code slightly... try it out and let us know if there is any improvement

#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
volatile int flow_frequency=0; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long cloopTime;

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

void setup()
{
  pinMode(flowsensor, INPUT);
  Serial.begin(9600);
  attachInterrupt(0, flow, RISING); // Setup Interrupt
  cloopTime = millis();
  // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Flow Meter!");
}
void loop ()
{
  // Every second, calculate and print litres/hour
  if(millis()-cloopTime >= 1000)
  {
      cloopTime = millis(); // Updates cloopTime
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      l_hour = flow_frequency * 8; // (Pulse frequency x 60 min) / 7.5Q = Pulse frequency*8 =flowrate in L/hour
      flow_frequency = 0; // Reset Counter
      lcd.setCursor(0, 1);
      lcd.print(l_hour,DEC);
      lcd.print (" L/hour");
      Serial.print(l_hour, DEC); // Print litres/hour
      Serial.println(" L/hour");
      // print the number of seconds since reset:

}
}




Also how are you checking for different flowrate?

still showing the value 48 L/hour

volatile int flow_frequency; // Measures flow meter pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowmeter = 2; // Flow Meter Pin number
unsigned long currentTime;
unsigned long cloopTime;

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

void setup()
{
pinMode(flowmeter, INPUT);
Serial.begin(9600);
attachInterrupt(0, flow, RISING); // Setup Interrupt
// see attachInterrupt() - Arduino Reference
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. (Results in +/- 3% range)
l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flow rate in L/hour
flow_frequency = 0; // Reset Counter
Serial.print(l_hour, DEC); // Print litres/hour
Serial.println(" L/hour");
}
}

if i only monitor by serial,i can see correct value

volatile int  flow_frequency;  // Measures flow meter pulses

That was as far as I read. I can not imagine needing to use a comment to describe the contents of a variable, when naming it flow_pulse_count would eliminate the need for the comment.

minko:
volatile int flow_frequency; // Measures flow meter pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowmeter = 2; // Flow Meter Pin number
unsigned long currentTime;
unsigned long cloopTime;

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

void setup()
{
pinMode(flowmeter, INPUT);
Serial.begin(9600);
attachInterrupt(0, flow, RISING); // Setup Interrupt
// see attachInterrupt() - Arduino Reference
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. (Results in +/- 3% range)
l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flow rate in L/hour
flow_frequency = 0; // Reset Counter
Serial.print(l_hour, DEC); // Print litres/hour
Serial.println(" L/hour");
}
}

if i only monitor by serial,i can see correct value

Interesting... check out the example pointed to by Nick_Pyner and see what they did different to include the LCD display

I am facing problem with arduino with display and yfs201 flow sensor.
My code is below.If i put lcd.begin (16,2);,flow senor reading is not correct and never change reading.
Can help me anybody?

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_mint; // 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();
//lcd.begin(16, 2);
cloopTime = currentTime;
lcd.print(" Flow meter***");
delay(2000);
lcd.clear();
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print litres/mint
if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
l_mint = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate inL/mint
flow_frequency = 0; // Reset Counter
Serial.print(l_mint, DEC); // Print litres/hour
Serial.println(" L/mint");
lcd.setCursor(0,0);
lcd.print(" Flow Meter");
lcd.setCursor(0,1);
lcd.setCursor(2,1);
lcd.print(l_mint, DEC);
lcd.setCursor(5,1);
lcd.print("L/Min");
}
}

LiquidCrystal lcd(12,11,5,4,3,2);

The LCD uses pin 2

unsigned char flowsensor = 2; // Sensor Input
pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up

The flow sensor also seems to use pin 2

minko:
I am facing problem with arduino with display and yfs201 flow sensor.
My code is below.If i put lcd.begin (16,2);,flow senor reading is not correct and never change reading.
Can help me anybody?

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_mint; // 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();
//lcd.begin(16, 2);
cloopTime = currentTime;
lcd.print(" Flow meter***");
delay(2000);
lcd.clear();
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print litres/mint
if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
l_mint = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate inL/mint
flow_frequency = 0; // Reset Counter
Serial.print(l_mint, DEC); // Print litres/hour
Serial.println(" L/mint");
lcd.setCursor(0,0);
lcd.print(" Flow Meter");
lcd.setCursor(0,1);
lcd.setCursor(2,1);
lcd.print(l_mint, DEC);
lcd.setCursor(5,1);
lcd.print("L/Min");
}
}

I think this might be the problem:

LiquidCrystal lcd(12,11,5,4,3,2);

unsigned char flowsensor = 2; // Sensor Input <-------

lcd.begin(16, 2);

use ANOTHER PIN for your SENSOR INPUT

If I changed sensor input to other pin.The senor is not working

I changed lcd pin2 and working.But once stop flowing showing double zero.

minko:
I changed lcd pin2 and working.But once stop flowing showing double zero.

So, just print two zeroes.

Thanks so much every body.change lcd function to {12,11,6,5,4,3} and now is working.

Thanks