MECHATRONICS PRACTICAL:
Measurement of Water Flow using water flow sensor with Arduino Uno and
seeing output in 16X2 LCD
CODE:
#include <LiquidCrystal.h> //lcd header
const int watermeterPin = 2; //op from sensor is obtained at pin 2
volatile int pulse_frequency; // for counting number of pulses
unsigned int litreperminute; //stores the result in litres/min
unsigned long currentTime, loopTime; //used for setting delay
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //LCD pin declaration
void setup()
{
pinMode(watermeterPin, INPUT); //setting digital pin 2 as input
Serial.begin(9600); //set bitrate as 9600 bps
attachInterrupt(0, getFlow, FALLING); //accept interrupt from pin 2, getFlow
//is ISR, triggered when the pin goes
//from high to low
currentTime = millis(); //start measuring time
loopTime = currentTime;
lcd.begin(16, 2); //declare type of lcd
}
void loop ()
{
delay(1000);
litreperminute = (pulse_frequency / 7.5); //calculations in L/min
pulse_frequency = 0; //reset pulsefreq for new readings
lcd.setCursor(0,0); //set cursor to first line
lcd.print("Water Flow Meter");
lcd.setCursor(0,1); //set cursor to second line
lcd.print("FlowRate:");
lcd.print(litreperminute, DEC); //display value in decimal on LCD
lcd.print("L/min ");
Serial.print("FlowRate:");
Serial.print(litreperminute, DEC); //display value in decimal on SM
Serial.println(" Liter/min ");
}
void getFlow () //ISR
{
pulse_frequency++; //increment count
}
Measurement of Water Flow using water flow sensor with Arduino Uno and
seeing output in 16X2 LCD
#include <LiquidCrystal.h> //lcd header
const int watermeterPin = 2; //op from sensor is obtained at pin 2
volatile int pulse_frequency; // for counting number of pulses
unsigned int litreperminute; //stores the result in litres/min
unsigned long currentTime, loopTime; //used for setting delay
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //LCD pin declaration
void setup()
{
pinMode(watermeterPin, INPUT); //setting digital pin 2 as input
Serial.begin(9600); //set bitrate as 9600 bps
attachInterrupt(0, getFlow, FALLING); //accept interrupt from pin 2, getFlow
//is ISR, triggered when the pin goes
//from high to low
currentTime = millis(); //start measuring time
loopTime = currentTime;
lcd.begin(16, 2); //declare type of lcd
}
void loop ()
{
delay(1000);
litreperminute = (pulse_frequency / 7.5); //calculations in L/min
pulse_frequency = 0; //reset pulsefreq for new readings
lcd.setCursor(0,0); //set cursor to first line
lcd.print("Water Flow Meter");
lcd.setCursor(0,1); //set cursor to second line
lcd.print("FlowRate:");
lcd.print(litreperminute, DEC); //display value in decimal on LCD
lcd.print("L/min ");
Serial.print("FlowRate:");
Serial.print(litreperminute, DEC); //display value in decimal on SM
Serial.println(" Liter/min ");
}
void getFlow () //ISR
{
pulse_frequency++; //increment count
}