As you all know that the water level is decreasing day by day. So we need a water management system. Water Management System is an important part of Smart City Management. Water management involves supplying water according to the real requirement & without wasting Water. There are many ways to measure water flow these days. as well as various types of water flow meters used to measure the amount of water flow in pipelines, but they are all too expensive. Instead of these expensive water flow meters, we can use readily-available and low-cost YFS201 Water Flow Sensor. This sensor can be used in Soft Drinks Industries and Chemical industries to continually measure and quantify the liquids that they are handling during this automation process.
In this tutorial we will learn how to make IoT Based Water Flow Meter using NodeMCU ESP8266 & YFS201 Water Flow Sensor. We will interface YFS201 Hall Effect Water Flow Sensor with NodeMCU ESP8266 Board and 0.96″ OLED Display. We will use an IoT Cloud platform. For IoT Cloud platform, we will use Thingspeak. The water flow rate & volume data can be uploaded to Thingspeak Server & can be viewed/monitored from anywhere using any internet connected device.
- Required components are as follows:
- NodeMCU – ESP8266-12E Board
- Water Flow Sensor – YF-S201 Hall-Effect Water Flow Sensor
- OLED Display – 0.96″ I2C OLED Display
- Connecting Jumper Wires (10)
YF-S201 Hall-Effect Water Flow Sensor
In the above image you can see the sensor. This sensor sits in line with your water line and contains a pinwheel sensor to measure how much liquid has moved through it. There’s an integrated magnetic hall effect sensor that outputs an electrical pulse with every revolution. The hall effect sensor is sealed from the water pipe and allows the sensor to stay safe and dry.
The sensor comes with three wires: red (5-24VDC power), black (ground) and yellow (Hall effect pulse output). By counting the pulses from the output of the sensor, you can easily calculate water flow. Each pulse is approximately 2.25 milliliters. Note this isn’t a precision sensor, and the pulse rate does vary a bit depending on the flow rate, fluid pressure and sensor orientation. It will need careful calibration if better than 10% precision is required. However, its great for basic measurement tasks!
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.
- Model: YF-S201
- Sensor Type: Hall effect
- Working Voltage: 5 to 18V DC (min tested working voltage 4.5V)
- Max current draw: 15mA @ 5V
- Output Type: 5V TTL
- Working Flow Rate: 1 to 30 Liters/Minute
- Working Temperature range: -25 to +80℃
- Working Humidity Range: 35%-80% RH
- Accuracy: ±10%
- Maximum water pressure: 2.0 MPa
- Output duty cycle: 50% +-10%
- Output rise time: 0.04us
- Output fall time: 0.18us
- Flow rate pulse characteristics: Frequency (Hz) = 7.5 * Flow rate (L/min)
- Pulses per Liter: 450
- Durability: minimum 300,000 cycles
- Cable length: 15cm
- 1/2″ nominal pipe connections, 0.78″ outer diameter, 1/2″ of thread
- Size: 2.5″ x 1.4″ x 1.4″
IoT Water Flow Meter using NodeMCU ESP8266
Now We will interface YF-S201 Hall-Effect Water Flow Sensor with Nodemcu ESP8266 & OLED Display. In OLED display we can see Water Flow Rate & Total Volume of Water passed through the pipe. Measured data can be sent to Thingspeak Server after an interval of 15 seconds regularly. You can switch to Blynk App if you want immediate data or MQTT Protocol.
Follow circuit connections are as follows:
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.
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.
The gerber file for the PCB is given below. You can download the gerber file and order the PCB online from PCBWay.
How to get PCB? Don’t worry. Simply Visit PCBWay.com and upload gerber file and order the PCB. PCBWay is one of the most experienced PCB manufacturers in world. They offer very good quality PCB at reasonable price.
Why Buy Printed Circuit Board From PCBWay?
- 24 hours production lead-time*
- Team supported with over 90 professionals & technical
- 99% on-Time Shipping with great support from DHL
- Great quality approved by customers all over the world
- Trusted Experience
- A 24/7 non-stop production and assistance service
- Low Minimums
- Factory PCBs with most competitive advantages
- A Chinese expert in Electronic Manufacturing Service (EMS)
Setting up Thingspeak IoT Cloud Platform
ThingSpeak is an open-source software written in Ruby which allows users to communicate with internet enabled devices. It facilitates data access, retrieval and logging of data by providing an API to both the devices and social network websites.
Now we will setup the Thingspeak Account for this project. It is very simple. Please follow given below steps.
1: Visit https://thingspeak.com/ and sign up with your details.
2: Create a New Channel by Clicking on “Channel” & fill up the following details as shown in the image below.
3: Click on API Key, you will see the “Write API Key“. Copy the API Key. It will be required in Code Part.
4: You can click on the “Private View” & customize the display window as you want.
Source Code/Program for NodeMCU
Upload given below code to the NodeMCU. But before that we need few Libraries for OLED Display. So download the Library first and add it to the Arduino IDE.
String apiKey = "###########"; //ENTER THE API KEY const char *ssid = "IOTBYHVM"; //YOUR SSID const char *pass = "IOTBYHVM"; //YOUR PAASWORD
Change the Thingspeak API Key, Wifi SSID & Password from the line above.
The complete source code is given below.
#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 = “#############”; // Enter your Write API key from ThingSpeak
const char *ssid = “IOTBYHVM”; // replace with your wifi ssid and wpa2 key
const char *pass = “IOTBYHVM”;
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;
unsigned long flowMilliLitres;
unsigned int totalMilliLitres;
void IRAM_ATTR pulseCounter()
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128×64)
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
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(“\t”); // Print tab space
display.setCursor(10,0); //oled display
display.print(“Water Flow Meter”);
display.setCursor(0,20); //oled display
display.setCursor(100,28); //oled display
// Print the cumulative total of litres flowed since starting
Serial.print(“Output Liquid Quantity: “);
Serial.print(“mL / “);
display.setCursor(0,45); //oled display
display.setCursor(100,53); //oled display
if (client.connect(server, 80)) // “220.127.116.11” 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(“X-THINGSPEAKAPIKEY: “ + apiKey + “\n”);
Monitoring Water Flow Rate & Volume
After uploading the code 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).
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.
Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.