LCD1602A LCD 5V display module (IIC/I2C interface)
Experimental wiring: Uno D2 connected to flow sensor OUT, relay connected to D4
Arduino------LCD1602
5V-------------VCC
GND-----------GND
A4-----------SDA IIC data cable
A5-----------SCL IIC clock line
Experimental open source code
/*
[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)
Experiment 88: LCD1602A LCD 5V display module (IIC/I2C interface)
Project 21: Water flow sensor, 5V relay module and LCD1602 I2C module
Experimental wiring: Uno D2 connected to flow sensor OUT, relay connected to D4
Arduino------LCD1602
5V-------------VCC
GND-----------GND
A4-----------SDA IIC data cable
A5-----------SCL IIC clock line
*/
#include <LiquidCrystal_I2C.h> //include LiquidCrystal Library
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define FLOWSENSORPIN 2 //Water flow sensor connected to Arduino digital pin 2
#define relayPin 4 // 5v relay module connected to Arduino digital pin 4
volatile uint16_t pulses = 0; // count how many pulses
volatile uint8_t lastflowpinstate; // track the state of the pulse pin
volatile uint32_t lastflowratetimer = 0; // you can try to keep time of how long it is between pulses
volatile float flowrate; // and use that to calculate a flowrate
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
uint8_t x = digitalRead(FLOWSENSORPIN);
if (x == lastflowpinstate) {
lastflowratetimer++;
return; // nothing changed!
}
if (x == HIGH) {
//low to high transition!
pulses++;
}
lastflowpinstate = x;
flowrate = 1000.0;
flowrate /= lastflowratetimer; // in hertz
lastflowratetimer = 0;
}
void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
}
}
void setup() {
Serial.begin(9600);
Serial.print("---Water flow sensor---");
lcd.init(); // Initialize the LCD display
lcd. backlight();
lcd.begin(16, 2); //16X2 lcd display
lcd.setBacklight(HIGH);
lcd.setCursor(0, 0); //setting display position
lcd. print("Aqua counter");
pinMode(FLOWSENSORPIN, INPUT); //sets the FLOWSENSORPIN as an INPUT
pinMode(relayPin, OUTPUT);//sets the relayPin as OUTPUT
digitalWrite(relayPin, LOW);
digitalWrite(FLOWSENSORPIN, HIGH);//optional Internal Pull-Up
lastflowpinstate = digitalRead(FLOWSENSORPIN);
useInterrupt(true);
delay(2000);
lcd. clear();
}
void loop()
{
lcd.setCursor(0, 0);
lcd. print("Pulses:"); lcd. print(pulses, DEC);
lcd. print("Hz:");
lcd. print(flowrate);
//lcd. print(flowrate);
Serial.print("Freq: "); Serial.println(flowrate);
Serial.print("Pulses: "); Serial.println(pulses, DEC);
// if a plastic sensor use the following calculation
// Sensor Frequency (Hz) = 7.5 * Q (Liters/min)
// Liters = Q * time elapsed (seconds) / 60 (seconds/minute)
// Liters = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds) / 60
// Liters = Pulses / (7.5 * 60)
float liters = pulses;
liters /= 7.5;
liters /= 60.0;
/*
// if a brass sensor use the following calculation
float liters = pulses;
liters /= 8.1;
liters -= 6;
liters /= 60.0;
*/
Serial.print(liters); Serial.println("Liters");
lcd.setCursor(0, 1);
lcd. print(liters); lcd. print("Litres");
if (liters >= 0.15) //water limit set
{
digitalWrite(relayPin, HIGH);
}
else {
digitalWrite(relayPin, LOW);
}
delay(2000);
lcd. clear();
}
Experimental serial port
return
Experimental scene graph