IoT Water Flow Meter Using ESP8266 & Water Flow Sensor

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

2022. 10. 14.

14:13 IoT Water Flow Meter using ESP8266 & Water Flow Sensor

IoT Water Flow Meter using ESP8266


& Water Flow Sensor

Admin Last Updated: August 21, 2022

Table of Contents [hide]

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

Overview: IoT Water Flow Meter using


ESP8266 & Water Flow Sensor

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

Meter using ESP8266 & Water Flow Sensor. We will interface

YFS201 Hall Effect Water Flow Sensor with NodeMCU ESP8266


Board. We will display the water flow rate & Total Volume in 0.96″

OLED Display. We will then integrate the hardware with IoT Server.

For IoT Server, we will use Thingspeak App. The water flow rate &

volume data can be uploaded to Thingspeak Server & can be

viewed/monitored from any part of the world.

Water Management System is an important part of City


Management. Water management involves supplying water

according to the real requirement & without wasting Water.


Therefore it is very important to measure water flow rate and

volume. Without measuring these parameters, Water Management


is almost impossible. Also monitoring the Water Volume, Flow Rate

& Water Quality remotely using Internet Connectivity has become


very essential. Therefore there is a need for Monitoring Water
Management System Online.

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

flow meter is required. So we will use YFS201 Hall Effect Water


Flow Sensor with ESP8266 & design simple IoT Based Water Flow

Meter.

Bill of Materials
Following are the components required for making this project. All
the components can be easily purchased from Amazon. The

component purchase link is given below.

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

1 NodeMCU ESP8266-12E 1 https://amz

Board

2 Water Flow YF-S201 Hall- 1 https://amz


Sensor Effect Water

Flow Sensor

3 OLED Display 0.96" I2C 1 https://amz

OLED Display

4 Connecting Jumper Wires 10 https://amz

Wires

5 Breadboard - 1 https://amz

YF-S201 Hall-Effect Water Flow Sensor

This is an image of the YF-S201 Hall-Effect Water Flow Sensor. This

sensor can be connected to the waterline as it has both inlet and

outlet. Inside the sensor, there is a pinwheel that measures how


much liquid has moved through it. There’s an integrated magnetic

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

hall effect sensor that outputs an electrical pulse with every

revolution.

The sensor comes with three wires:

1. Red (5-24VDC power)

2. Black (ground)

3. Yellow (Hall effect pulse output)

The water flow rate can be calculated by counting the pulses from

the output of the sensor. Each pulse is approximately 2.25

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

pressure, and sensor orientation. To get better precision of more

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.

Pulse frequency (Hz) / 7.5 = flow rate in L/min

To learn more about this sensor, you can follow our previous guide

here: Arduino & Water Flow Sensor Complete Hookup 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

IoT Water Flow Meter using ESP8266 &


Water Flow Sensor
Now let us interface YF-S201 Hall-Effect Water Flow Sensor with

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

Thingspeak Server after an interval of 15 seconds regularly. You

can switch to Blynk Application if you want immediate data.

Similarly using MQTT Protocol better wireless communication can


be achieved.

But now let us see the IoT Water Flow Meter Circuit Diagram &
Connection.

Water Flow Sensor is a digital Sensor, so we can connect its output

pin to any of the digital pins of ESP8266. In my case, I connected to


GPIO2, i.e D4. The sensor works at 5V & can be connected to Vin

of ESP8266. Similarly, I2C OLED Display SDA & SCL pins are

connected to D2 & D1 of ESP8266 respectively. The OLED Display

works at 3.3V so it can be connected to 3.3V pin of Nodemcu.

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

I have assembled the circuit on Breadboard. You can use the

custom-designed PCB for making the small Water Flow Meter


board.

PCB Designing & Ordering Online


The PCB for this project has been designed in EasyEDA online PCB

making tool. Below is the front view and Back View of the PCB

generated from Gerber Viewer of NextPCB.

The Gerber file for all the PCB is given below. You can download the

Gerber file and order the PCB online from NextPCB.

Download Gerber File: IoT Water Flow Meter

Now you can visit the NextPCB official website by clicking here:

https://www.nextpcb.com/. So you will be directed to NextPCB

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 people trust NextPCB for PCB & PCBA Services.

Mathematical Calculation to Measure Flow


Rate & Volume
We have determined flow rate by a change in velocity of the water.

The water velocity depends on the pressure that forces the through

pipelines. The cross-sectional area of the pipe is known and

remains constant, thus we calculate the average velocity that

indicates the flow rate.

Let us consider Q is the flow rate/total flow of water through the

pipe, V is the average velocity & A is the cross-sectional area of the

pipe. In such a case the basis relationship for determining the


liquid’s flow rate in such cases is Q=VxA

Sensor Frequency (Hz) = 7.5 * Q (Liters/min)

Litres = Q * time elapsed (seconds) / 60 (seconds/minute)

Litres = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds)

/ 60

Litres = Pulses / (7.5 * 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

Thingspeak follow the following Steps:

Step 1: Visit https://thingspeak.com/ and create your account by

filling up the details.

Step 2: Create a New Channel by Clicking on “Channel” & fill up the

following details as shown in the image below.

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

API Key. This is very important, it will be required in Code Part.

Step 4: You can click on the “Private View” & customize the display

window as you want.

So, that’s all from the Thingspeak Setup Part. Now let us move to

the programming Part.

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

before that we need few Libraries for OLED Display. So download


the Library first and add it to the Arduino IDE.

1. Download SSD1306 Library

2. Download Adafruit GFX Library

String apiKey = "KBD1JSZTUKCXJ15V";


const char *ssid = "Alexahome";  
const char *pass = "loranthus";

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.

The complete source code is given below.

#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

    Serial.print("Flow rate: ");


    Serial.print(float(flowRate));  // Print the integer par
    Serial.print("L/min");
    Serial.print("\t");       // Print tab space
 
    display.clearDisplay();
    
    display.setCursor(10,0);  //oled display
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.print("Water Flow Meter");
    
    display.setCursor(0,20);  //oled display
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.print("R:");
    display.print(float(flowRate));
    display.setCursor(100,28);  //oled display
    display.setTextSize(1);
    display.print("L/M");
 
    // Print the cumulative total of litres flowed since sta
    Serial.print("Output Liquid Quantity: ");
    Serial.print(totalMilliLitres);
    Serial.print("mL / ");
    Serial.print(totalLitres);
    Serial.println("L");
 
    display.setCursor(0,45);  //oled display
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.print("V:");
    display.print(totalLitres);
    display.setCursor(100,53);  //oled display
    display.setTextSize(1);
    display.print("L");
    display.display();
  }
  
  if (client.connect(server, 80)) // "184.106.153.149" or ap
  {
    String postStr = apiKey;
      postStr += "&field1=";
      postStr += String(float(flowRate));
      postStr += "&field2=";
      postStr += String(totalLitres);
      postStr += "\r\n\r\n";

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();
}

Monitoring Water Flow Rate & Volume


Once the Code is uploaded the OLED Display will strat working and

will show the flow rate and volume. Initially the flow rate will be 0

liter/minute(L/M). Also Total Volume shown will be 0 Liter(L).

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy