Projects / Water litre counter

Water litre counter

Beginner2.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO

    Wire the sensor

    Red to 5V, black to GND, yellow to D2.

  2. NANO

    Add the pull-up

    10 kilohm from D2 to 5V.

  3. NANO

    Wire the display

    SDA to A4, SCL to A5.

  4. NANO17.8 mASSD1306

    Install the libraries

    Adafruit SSD1306 and Adafruit GFX.

  5. NANO

    Upload and test blowing

    Blow gently through the sensor to see pulses.

  6. NANOS201

    Fit the sensor to a hose

    Arrow in the direction of the flow.

  7. NANO

    Run 1 litre through

    Check the total.

  8. NANO

    Adjust the constant

    Calibrate PULSES_PER_LITRE with the jug.

In short

How do you build a water litre counter?

A flow sensor in a water line counts litres with a tiny turbine and shows the total and the flow rate. Keep the electronics dry. You need Arduino Nano, Water flow sensor YF-S201, SSD1306 OLED Display, Resistor 10 kΩ, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 8 steps and about 150 minutes.

Level
Beginner
Time
About 150 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
YF-S201 red wireNano 5VSensor power, 5 to 18 V
YF-S201 black wireNano GNDCommon ground
YF-S201 yellow wireNano D2Pulse output for the interrupt
10 kilohm resistorBetween D2 and 5VPull-up for the open-collector Hall output
OLED SDANano A4I2C data
OLED SCLNano A5I2C clock
OLED VCC and GNDNano 5V and GNDPower (check your OLED accepts 5 V)

Code

The YF-S201 flow sensor has a small turbine inside a plastic body. The turbine spins faster as more water flows through, and a Hall sensor sends a pulse for every turn. The datasheet says the pulse frequency in hertz is about 7.5 times the flow in litres per minute, which works out to about 450 pulses per litre. The sketch counts the pulses with an interrupt, so none are missed while the display updates, and once a second it copies the count, works out how many pulses arrived, and converts that to a flow rate and a running total. The shared counter is read with the interrupts briefly switched off, so a pulse cannot arrive halfway through the read. The 450 figure is only nominal, and the sensor is accurate to about 10 percent, so calibrate it by running a measured amount through, such as a 1 litre jug, and adjusting PULSES_PER_LITRE until the total matches. Only use the sensor on cold water, and keep the Nano, the display and all connections well away from splashes and joints: put the electronics in a dry box and run the sensor wire to it. Never connect it to anything powered from the mains.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);

const int FLOW_PIN = 2;                       // interrupt 0 on the Nano
const float PULSES_PER_LITRE = 450.0;         // YF-S201 is about 7.5 Hz per L/min, so 450 pulses per litre; calibrate with a jug

volatile unsigned long pulseCount = 0;
unsigned long lastPulses = 0, lastMs = 0;
float totalLitres = 0, flowLpm = 0;

void onPulse() { pulseCount++; }

void setup() {
  pinMode(FLOW_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(FLOW_PIN), onPulse, FALLING);
  Serial.begin(9600);
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
  lastMs = millis();
}

void loop() {
  unsigned long now = millis();
  if (now - lastMs >= 1000) {
    noInterrupts();
    unsigned long p = pulseCount;             // read the shared counter safely
    interrupts();
    unsigned long delta = p - lastPulses;
    float seconds = (now - lastMs) / 1000.0;
    lastPulses = p;
    lastMs = now;

    flowLpm = (delta / PULSES_PER_LITRE) / seconds * 60.0;
    totalLitres += delta / PULSES_PER_LITRE;

    oled.clearDisplay();
    oled.setTextColor(SSD1306_WHITE);
    oled.setTextSize(1);
    oled.setCursor(0, 0);  oled.print("Water counter");
    oled.setTextSize(2);
    oled.setCursor(0, 16); oled.print(flowLpm, 1); oled.print(" L/min");
    oled.setCursor(0, 40); oled.print(totalLitres, 2); oled.print(" L");
    oled.display();

    Serial.print(flowLpm, 2); Serial.print(" L/min, total "); Serial.println(totalLitres, 3);
  }
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

0.00 L/min, total 0.000

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
No flow0.0 L/min and the total does not change
Gentle flowA few litres per minute
Fill a 1 litre jugTotal about 1.0 L, within about 10 percent
After calibrationTotal within a few percent of the jug
Serial monitorFlow and total printed every second

If it does not work

No pulses

Check the yellow wire is on D2 and the sensor arrow follows the flow.

Total runs backwards or jumps

Check the wiring and the pull-up resistor.

Total too high or low

Adjust PULSES_PER_LITRE with the jug test.

Display blank

Check A4 and A5.

Water near the electronics

Switch off and dry everything before use.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the water litre counter?

Arduino Nano, Water flow sensor YF-S201, SSD1306 OLED Display, Resistor 10 kΩ, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.

How long does the water litre counter take to build?

About 150 minutes for a beginner maker, in 8 steps.

How do I know the water litre counter works?

No flow: 0.0 L/min and the total does not change. Gentle flow: A few litres per minute. Fill a 1 litre jug: Total about 1.0 L, within about 10 percent.

No pulses: what should I check?

Check the yellow wire is on D2 and the sensor arrow follows the flow.

Total runs backwards or jumps: what should I check?

Check the wiring and the pull-up resistor.

Total too high or low: what should I check?

Adjust PULSES_PER_LITRE with the jug test.

Display blank: what should I check?

Check A4 and A5.