Interface Water Flow Sensor Using ESP32 Board

Measure your water usage using water flow sensor.


There have several case where water usage can suddenly spike, just because of water pipe broken under the ground. The sad thing is you only noticed after you get the latest water bill. So in this tutorial I will share with you on how to interface water flow sensor with ESP32 board. Later we will develop notification system if the water usage exceeds certain value.


This video is show on how to interface water flow sensor with ESP32 board.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is the sample program to measure water flow rate and the total water flow through the sensor.

– Interface water flow sensor with ESP32 board.
– ESP32 Dev Module
– G 1/2 Water Flow Sensor
#define LED_BUILTIN 2
#define SENSOR 27
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
boolean ledState = LOW;
float calibrationFactor = 4.5;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
void IRAM_ATTR pulseCounter()
void setup()
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
void loop()
currentMillis = millis();
if (currentMillis – previousMillis > interval) {
pulse1Sec = pulseCount;
pulseCount = 0;
// Because this loop may not complete in exactly 1 second intervals we calculate
// the number of milliseconds that have passed since the last execution and use
// that to scale the output. We also apply the calibrationFactor to scale the output
// based on the number of pulses per second per units of measure (litres/minute in
// this case) coming from the sensor.
flowRate = ((1000.0 / (millis() – previousMillis)) * pulse1Sec) / calibrationFactor;
previousMillis = millis();
// Divide the flow rate in litres/minute by 60 to determine how many litres have
// passed through the sensor in this 1 second interval, then multiply by 1000 to
// convert to millilitres.
flowMilliLitres = (flowRate / 60) * 1000;
// Add the millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(int(flowRate)); // Print the integer part of the variable
Serial.print("\t"); // Print tab space
// Print the cumulative total of litres flowed since starting
Serial.print("Output Liquid Quantity: ");
Serial.print("mL / ");
Serial.print(totalMilliLitres / 1000);

view raw
hosted with ❤ by GitHub

Thank You


  1. G 1/2 Water Flow Sensor Datasheet
  2. Working with Water Flow Sensors & Arduino
  3. ESP32 with PIR Motion Sensor using Interrupts and Timers

Thanks for reading this tutorial. If you have any technical inquiry, please post at Cytron Technical Forum.

3 thoughts on “Interface Water Flow Sensor Using ESP32 Board”

  1. please can I have the code using a water flow sensor (yf-s201 precisely) instead.
    Also a notification system if the water usage exceeds a certain value.

  2. Assalamu’alaikum bro. Saya Asma, final year student. Saya tengah buat FYP menggunakan water flow sensor. Saya nak tanya adakah boleh ESP32 support lebih daripada satu water flow sensor? Terima kasih bro…!

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
DIY Automated Vacuum Cleaner Using REKA:BIT With Micro:bit
Rainbow Spark in Mini House using Maker Uno.
TinyML on Arduino using Edge Impulse
Tutorials of Cytron Technologies Scroll to Top