IoT Water Flow Meter Using ESP8266 & Water Flow Sensor
IoT Water Flow Meter Using ESP8266 & Water Flow Sensor
IoT Water Flow Meter Using ESP8266 & Water Flow Sensor
14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
1 Overview: IoT Water Flow Meter using ESP8266 & Water Flow
Sensor
2 Bill of Materials
3 YF-S201 Hall-Effect Water Flow Sensor
4 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
5 PCB Designing & Ordering Online
6 Mathematical Calculation to Measure Flow Rate & Volume
7 Setting up Thingspeak
8 Source Code/Program
9 Monitoring Water Flow Rate & Volume
10 Video Tutorial & Complete Guide
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 1/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
In this project we will learn how to make IoT Based Water Flow
OLED Display. We will then integrate the hardware with IoT Server.
For IoT Server, we will use Thingspeak App. The water flow rate &
There are so many Water Flow Sensors available in the market but
are too expensive to use and afford. As a result, a low-cost water
Meter.
Bill of Materials
Following are the components required for making this project. All
the components can be easily purchased from Amazon. The
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 2/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
COMPONENTS
S.N. DESCRIPTION QUANTITY
NAME
Board
Flow Sensor
OLED Display
Wires
5 Breadboard - 1 https://amz
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 3/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
revolution.
2. Black (ground)
The water flow rate can be calculated by counting the pulses from
milliliters. This Sensor is cheaper and best but not the accurate one
as flow rate/volume varies a bit depending on the flow rate, fluid
than 10%, a lot of calibration is required. You can make a basic IoT
Based Water Flow Meter using this Sensor.
The pulse signal is a simple square wave so its quite easy to log
and convert into liters per minute using the following formula.
To learn more about this sensor, you can follow our previous guide
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 4/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
Nodemcu ESP8266 & OLED Display. The OLED Display will show
Water Flow Rate & Total Volume of Water passed through the
pipe. The same Flow Rate & Volume data can be sent to
But now let us see the IoT Water Flow Meter Circuit Diagram &
Connection.
of ESP8266. Similarly, I2C OLED Display SDA & SCL pins are
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 5/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
making tool. Below is the front view and Back View of the PCB
The Gerber file for all the PCB is given below. You can download the
Now you can visit the NextPCB official website by clicking here:
website.
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 6/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
You can now upload the Gerber File to the Website and place an
order. The PCB quality are clean and brilliant. That is why most of
The water velocity depends on the pressure that forces the through
/ 60
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 7/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
Setting up Thingspeak
Now we need to setup the Thingspeak Account. To set up
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 8/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
Step 3: Click on API Key, you will see the “Write API Key“. Copy the
Step 4: You can click on the “Private View” & customize the display
So, that’s all from the Thingspeak Setup Part. Now let us move to
Source Code/Program
Now Let us see ESP8266 Water Flow Sensor Code using Arduino
IDE. The code can be directly uploaded to the NodeMCU Board. But
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 9/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
Change the Thingspeak API Key, Wifi SSID & Password from the
line above.
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixel
#define OLED_RESET -1 // Reset pin # (or -1 if sharing
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire,
String apiKey = "KBD1JSZTUKCXJ15V"; // Enter your Write API
const char *ssid = "Alexahome"; // replace with your wif
const char *pass = "loranthus";
const char* server = "api.thingspeak.com";
#define LED_BUILTIN 16
#define SENSOR 2
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 long flowMilliLitres;
unsigned int totalMilliLitres;
float flowLitres;
float totalLitres;
void IRAM_ATTR pulseCounter()
{
pulseCount++;
}
WiFiClient client;
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 10/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
void setup()
{
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize wi
display.clearDisplay();
delay(10);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SENSOR, INPUT_PULLUP);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounte
}
void loop()
{
currentMillis = millis();
if (currentMillis - previousMillis > interval)
{
pulse1Sec = pulseCount;
pulseCount = 0;
// Because this loop may not complete in exactly 1 secon
// the number of milliseconds that have passed since the
// that to scale the output. We also apply the calibrati
// based on the number of pulses per second per units of
// this case) coming from the sensor.
flowRate = ((1000.0 / (millis() - previousMillis)) * pul
previousMillis = millis();
// Divide the flow rate in litres/minute by 60 to determ
// passed through the sensor in this 1 second interval,
// convert to millilitres.
flowMilliLitres = (flowRate / 60) * 1000;
flowLitres = (flowRate / 60);
// Add the millilitres passed in this second to the cumu
totalMilliLitres += flowMilliLitres;
totalLitres += flowLitres;
// Print the flow rate for this second in litres / minut
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 11/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 12/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlen
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
client.stop();
}
will show the flow rate and volume. Initially the flow rate will be 0
Once the motor is turned ON & Water Starts flowing, you can see
the OLED Display displaying the Flow Rate (F) & Volume(V).
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 13/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
Now you can monitor the Water Flow Rate & Volume Data on
Thingspeak Server as well. You just need to visit the Private View of
Thingspeak Dashboard.
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 14/15
2022. 10. 14. 14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor
https://how2electronics.com/iot-water-flow-meter-using-esp8266-water-flow-sensor/ 15/15