/* YF-S201-Water-Flow-Sensor modified on 14 oct 2020 by Amir Mohammad Shojaee @ Electropeak Home based on www.hobbytronics.co.uk examples */ #include #include LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display. ACHTUNG: Adresse kann auch 0x3F sein !!! // Anschlüsse: // GND - GND // VCC - 5V // SDA - ANALOG Pin 4 // SCL - ANALOG pin 5 double flow; //Water flow L/Min int flowsensor = 2; unsigned long currentTime; unsigned long lastTime; unsigned long pulse_freq; float velocity; // =========================== // ======= SETUP ========= // =========================== void setup() { pinMode(flowsensor, INPUT); Serial.begin(9600); attachInterrupt(0, pulse, RISING); // Setup Interrupt currentTime = millis(); lastTime = currentTime; lcd.begin(); // initialize the lcd lcd.backlight(); lcd.setCursor(0,0); lcd.print("Stroemungs-"); lcd.setCursor(0,1); lcd.print("messer"); delay(3000); lcd.setCursor(0,0); lcd.print(" "); lcd.setCursor(0,1); lcd.print(" "); } // ======================== // ======= LOOP ========= // ======================== void loop () { currentTime = millis(); // Every second, calculate frequency if(currentTime >= (lastTime + 1000)) { lastTime = currentTime; // Pulse frequency (Hz) = 7.5*Q, Q is flow rate in L/min. //flow = (pulse_freq / 7.5); /* Serial.print(pulse_freq); Serial.println(" Hz"); */ lcd.setCursor(0,0); lcd.print("f = "); lcd.print(pulse_freq); lcd.print(" Hz "); velocity = pulse_freq * 0.023; // calculation of the water-speed. 0.023 is the conversion-factor from frequency to velocity. You have to determine this factor by experiment! lcd.setCursor(0,1); lcd.print("v = "); lcd.print(velocity); lcd.print(" m/s "); pulse_freq = 0; // Reset Counter } } // ============================= // ======= INTERRUPT ========= // ============================= void pulse () // Interrupt function { pulse_freq++; }