IOT

IoT Water Flow Meter using ESP8266 & Water Flow Sensor

An essential component of city management is the water management system. Water management entails giving water only when it is actually needed and without wasting any. Therefore, measuring water flow rate and volume is crucial. Water management is nearly impossible without measuring these characteristics. Additionally, it has become crucial to remotely monitor water volume, flow rate, and quality utilizing internet connectivity. As a result, a Monitoring Water Management System Online is required.

In this blog, using an ESP8266 and a water flow sensor, we will create an IoT-based water flow meter for this project. We’ll connect the NodeMCU ESP8266 Board to the YFS201 Hall Effect Water Flow Sensor. We’ll use a 0.96″ OLED display to show the total volume and water flow rate. The hardware will then be integrated with the IoT Server. We’ll use the Thingspeak App as an IoT server. The information about the water flow rate and volume can be uploaded to Thingspeak Server and accessed or monitored from anywhere in the world.

Although there are a lot of water flow sensors on the market, they are too pricey for most people to use and afford. A cheap water flow meter is therefore necessary. Therefore, we will create a straightforward IoT-based water flow meter using the YFS201 Hall Effect Water Flow Sensor and ESP8266.

Hardware Required

  • ESP8266-12E Board
  • YF-S201 Hall-Effect Water Flow Sensor
  • 0.96″ I2C OLED Display
  • Connecting Wires
  • Breadboard

YF-S201 Hall-Effect Water Flow Sensor

The YF-S201 Hall-Effect Water Flow Sensor is shown in this photograph. This sensor has an inlet and an outlet, so it may be connected to the waterline. A pinwheel inside the sensor detects how much liquid has passed through it. With each rotation, an embedded magnetic hall effect sensor produces an electrical pulse.

.The sensor comes with three wires:

1. Red (5-24VDC power)

2. Black (ground)

3. Yellow (Hall effect pulse output)

By counting the pulses coming from the sensor’s output, the water flow rate may be determined. About 2.25 milliliters make up each pulse. The flow rate and volume vary slightly based on the flow rate, fluid pressure, and sensor orientation, making this sensor the best and cheapest one but not the most precise. It takes a lot of calibrating to achieve greater precision of more than 10%. Using this sensor, you may create a simple IoT-based water flow meter.

Since the pulse signal is a simple square wave, the following method can be used to log and convert the pulse rate into liters per minute.

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

IoT Water Flow Meter using ESP8266 & Water Flow Sensor

Let’s connect the Nodemcu ESP8266 & OLED Display to the YF-S201 Hall-Effect Water Flow Sensor now. The water flow rate and total volume of water passed through the pipe will be displayed on the OLED Display. A consistent interval of 15 seconds can be used to send the same Flow Rate & Volume data to Thingspeak Server. If you require immediate info, you can switch to the Blynk Application. The MQTT Protocol can also be used to improve wireless communication.

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

Water Flow Sensor ESP8266

Since the water flow sensor is a digital sensor, its output pin can be connected to any of the digital ESP8266 pins. I linked to GPIO2, or D4, in my situation. The sensor can be attached to the ESP8266’s Vin and operates at 5 volts. Similarly, the ESP8266’s D2 and D1 are linked to the SDA and SCL pins of the I2C OLED Display, respectively. OLED displays operate at 3.3V, therefore they can be connected to the Nodemcu’s 3.3V pin.

I put the circuit together on a breadboard. The tiny Water Flow Meter board can be made using a specially designed PCB.

PCB Designing & Ordering Online

The PCB for this project has been designed in EasyEDA online PCB-making tool. Below are the front view and Back View of the PCB generated from Gerber Viewer of NextPCB.

The Gerber file for all the PCBs 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.

Now that the Gerber File has been uploaded, you can order from the website. The PCB quality is flawless and clean. For PCB & PCBA Services, the majority of consumers rely on NextPCB for this reason.

Mathematical Calculation to Measure Flow Rate & Volume

By monitoring changes in water velocity, we have calculated the flow rate. The pressure that pushes water through pipelines determines the water’s velocity. Since the pipe’s cross-sectional area is known and constant, we can calculate the average velocity, which represents the flow rate.

Let’s assume that Q represents the flow rate or overall flow of water through the pipe, V represents the average velocity, and A represents the pipe’s cross-sectional area. 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)

Setting up Thingspeak

The Thingspeak Account needs to be set up right now. Follow these steps to set up Thingspeak:

Step 1: Visit https://thingspeak.com/ and create your account by filling up the details.

Step 2: Create a New Channel by selecting “Channel” and entering the information requested in the image below.

Step 3: Select “Write API Key” after clicking on API Key. Copied the API Key. This is crucial; the Code Part will require it.

Step 4: Select “Private View” and modify the display window as necessary.

The Thingspeak Setup Part has concluded. Let’s get to the programming section now.

Source Code/Program

Now Let’s look at the Arduino IDE code for the ESP8266 water flow sensor. Direct uploading of the code to the NodeMCU board is possible. However, a few libraries for OLED displays are first required. Therefore, first, download the Library and then 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”;
  • Change the Wifi SSID, password, and Thingspeak API key from the line above.
  • Below is a list of the source code’s entirety.
include
include
include
include
include
define SCREEN_WIDTH 128 // OLED display width, in pixels
define SCREEN_HEIGHT 64 // OLED display height, in pixels
define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
String apiKey = “KBD1JSZTUKCXJ15V”; // Enter your Write API key from ThingSpeak
const char *ssid = “Alexahome”; // replace with your wifi ssid and wpa2 key
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;
void setup()
{
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128×64)
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), 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; flowLitres = (flowRate / 60); // Add the millilitres passed in this second to the cumulative total totalMilliLitres += flowMilliLitres; totalLitres += flowLitres; // Print the flow rate for this second in litres / minute Serial.print("Flow rate: "); Serial.print(float(flowRate)); // Print the integer part of the variable 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 starting 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 api.thingspeak.com
{
String postStr = apiKey;
postStr += “&field1=”;
postStr += String(float(flowRate));
postStr += “&field2=”;
postStr += String(totalLitres);
postStr += “\r\n\r\n”;
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-urlencoded\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr);
}
client.stop();
}

Monitoring Water Flow Rate & Volume

The OLED Display will start operating and begin showing the flow rate and volume as soon as the code is uploaded. The flow rate will initially be 0 litres per minute (L/M). Total Volume will also be displayed as 0 Liter (L).

The OLED display will show the Flow Rate (F) and Volume after the motor is turned on and the water begins to flow (V).

You can now keep an eye on the data for water flow rate and volume on Thingspeak Server as well. You only need to go to the Thingspeak Dashboard’s Private View.

Water Flow Sensor Thingspeak

ConclusionI hope all of you understand how to design IoT Water Flow Meter using ESP8266. We MATHA ELECTRONICS will be back soon with more informative blogs.

Leave a Reply

Your email address will not be published.