Built-in project · Arduino Uno R3

Water meter on an Arduino Uno

A YF-S201 flow sensor’s pulses counted on an interrupt: 450 make a liter, and an I2C LCD shows the flow and the liters so far. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

The parts retail for about $38. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino Water Flow Sensor (YF-S201): Code and Wiring

Water meter · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Water meter: a YF-S201 flow sensor and a 16x2 LCD on an I2C backpack.
//
//   pin 2    the sensor's SIG (yellow); VCC and GND from the rails
//   A4, A5   the LCD backpack's SDA and SCL (address 0x27)
//
// The wheel in the sensor pulses SIG 7.5 times a second for each liter a
// minute, so 450 pulses make a liter. An interrupt counts the falling edges;
// once a second the sketch takes the count, and
//
//   flow (tenths of L/min) = pulses x 10 / 7.5 = pulses x 4 / 3
//   total (ml)             = all pulses x 1000 / 450 = all pulses x 20 / 9
//
// in whole numbers, since this board has no floating point to spare.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const int SENSOR = 2;

LiquidCrystal_I2C lcd(0x27, 16, 2);

volatile unsigned long pulses = 0;
unsigned long counted = 0;
unsigned long lastSecond = 0;

void onPulse() {
  pulses++;
}

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: water meter");
  pinMode(SENSOR, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(SENSOR), onPulse, FALLING);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Water meter");
  lastSecond = millis();
}

void loop() {
  if (millis() - lastSecond < 1000) return;
  lastSecond += 1000;
  noInterrupts();
  unsigned long all = pulses;
  interrupts();
  unsigned long thisSecond = all - counted;
  counted = all;

  unsigned long tenths = thisSecond * 4 / 3;
  unsigned long ml = all * 20 / 9;

  lcd.setCursor(0, 0);
  lcd.print("Flow ");
  lcd.print(tenths / 10);
  lcd.print('.');
  lcd.print(tenths % 10);
  lcd.print(" L/min   ");
  lcd.setCursor(0, 1);
  lcd.print("Total ");
  lcd.print(ml / 1000);
  lcd.print('.');
  unsigned long frac = (ml % 1000) / 10;
  if (frac < 10) lcd.print('0');
  lcd.print(frac);
  lcd.print(" L    ");

  Serial.print("flow ");
  Serial.print(tenths / 10);
  Serial.print('.');
  Serial.print(tenths % 10);
  Serial.print(" L/min, total ");
  Serial.print(ml);
  Serial.println(" ml");
}

Parts list

4 parts, plus the jumper wires. They retail for about $38; on Mokxi they're free.

How it is wired

5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 2; Water flow sensor, YF-S201 pin SIG
  • Arduino Uno R3 pin 5V; Water flow sensor, YF-S201 pin VCC; Character LCD with I2C backpack pin VCC
  • Ground: Arduino Uno R3 pin GND; Water flow sensor, YF-S201 pin GND; Character LCD with I2C backpack pin GND
  • Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
  • Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.