Internet-Connected Smart Water Meter

In this article by Pradeeka Seneviratne author of the book, Internet of Things with Arduino Blueprints, explains that for many years and even now, water meter readings have been collected manually. To do this, a person has to visit the location where the water meter is installed. In this article, you will learn how to make a smart water meter with an LCD screen that has the ability to connect to the internet and serve meter readings to the consumer through the Internet.

In this article, you shall do the following:

  • Learn about water flow sensors and its basic operation
  • Learn how to mount and plumb a water flow meter on and into the pipeline
  • Read and count the water flow sensor pulses
  • Calculate the water flow rate and volume
  • Learn about LCD displays and connecting with Arduino
  • Convert a water flow meter to a simple web server and serve meter readings through the Internet

(For more resources related to this topic, see here.)

Prerequisites

Water flow sensors

The heart of a water flow sensor consists of a Hall effect sensor (https://en.wikipedia.org/wiki/Hall_effect_sensor) that outputs pulses for magnetic field changes. Inside the housing, there is a small pinwheel with a permanent magnet attached to it. When the water flows through the housing, the pinwheel begins to spin, and the magnet attached to it passes very close to the Hall effect sensor in every cycle. The Hall effect sensor is covered with a separate plastic housing to protect it from the water. The result generates an electric pulse that transitions from low voltage to high voltage, or high voltage to low voltage, depending on the attached permanent magnet's polarity. The resulting pulse can be read and counted using the Arduino.

For this project, we will use a Liquid Flow sensor from Futurlec (http://www.futurlec.com/FLOW25L0.shtml). The following image shows the external view of a Liquid Flow Sensor:

Liquid flow sensor – the flow direction is marked with an arrow

The following image shows the inside view of the liquid flow sensor. You can see a pinwheel that is located inside the housing:

Pinwheel attached inside the water flow sensor

Wiring the water flow sensor with Arduino

The water flow sensor that we are using with this project has three wires, which are the following:

  • Red (or it may be a different color) wire, which indicates the Positive terminal
  • Black (or it may be a different color) wire, which indicates the Negative terminal
  • Brown (or it may be a different color) wire, which indicates the DATA terminal

All three wire ends are connected to a JST connector. Always refer to the datasheet of the product for wiring specifications before connecting them with the microcontroller and the power source.

When you use jumper wires with male and female headers, do the following:

  • Connect positive terminal of the water flow sensor to Arduino 5V.
  • Connect negative terminal of the water flow sensor to Arduino GND.
  • Connect DATA terminal of the water flow sensor to Arduino digital pin 2.

    Water flow sensor connected with Arduino Ethernet Shield using three wires

You can directly power the water flow sensor using Arduino since most residential type water flow sensors operate under 5V and consume a very low amount of current. Read the product manual for more information about the supply voltage and supply current range to save your Arduino from high current consumption by the water flow sensor. If your water flow sensor requires a supply current of more than 200mA or a supply voltage of more than 5v to function correctly, then use a separate power source with it.

The following image illustrates jumper wires with male and female headers:

Jumper wires with male and female headers

Reading pulses

The water flow sensor produces and outputs digital pulses that denote the amount of water flowing through it. These pulses can be detected and counted using the Arduino board.

Let's assume the water flow sensor that we are using for this project will generate approximately 450 pulses per liter (most probably, this value can be found in the product datasheet). So 1 pulse approximately equals to [1000 ml/450 pulses] 2.22 ml. These values can be different depending on the speed of the water flow and the mounting polarity of the water flow sensor.

Arduino can read digital pulses generating by the water flow sensor through the DATA line.

Rising edge and falling edge

There are two type of pulses, as listed here:.

  • Positive-going pulse: In an idle state, the logic level is normally LOW. It goes HIGH state, stays there for some time, and comes back to the LOW state.
  • Negative-going pulse: In an idle state, the logic level is normally HIGH. It goes LOW state, stays LOW state for time, and comes back to the HIGH state.

The rising and falling edges of a pulse are vertical. The transition from LOW state to HIGH state is called rising edge and the transition from HIGH state to LOW state is called falling edge.

Representation of Rising edge and Falling edge in digital signal

You can capture digital pulses using either the rising edge or the falling edge. In this project, we will use the rising edge.

Reading and counting pulses with Arduino

In the previous step, you attached the water flow sensor to Arduino UNO. The generated pulse can be read by Arduino digital pin 2 and the interrupt 0 is attached to it.

The following Arduino sketch will count the number of pulses per second and display it on the Arduino Serial Monitor:

  1. Open a new Arduino IDE and copy the sketch named B04844_03_01.ino.
  2. Change the following pin number assignment if you have attached your water flow sensor to a different Arduino pin:
    int pin = 2;
  3. Verify and upload the sketch on the Arduino board:
    int pin = 2; //Water flow sensor attached to digital pin 2
    volatile unsigned int pulse;
    const int pulses_per_litre = 450;
    void setup()
    {
       Serial.begin(9600);
       pinMode(pin, INPUT);
       attachInterrupt(0, count_pulse, RISING);
    }
    void loop()
    {
       pulse = 0;
       interrupts();
       delay(1000);
       noInterrupts();
       Serial.print("Pulses per second: ");
       Serial.println(pulse);
    }
    void count_pulse()
    {
       pulse++;
    }
  4. Open the Arduino Serial Monitor and blow air through the water flow sensor using your mouth.
  5. The number of pulses per second will print on the Arduino Serial Monitor for each loop, as shown in the following screenshot:

    Pulses per second in each loop

The attachInterrupt() function is responsible for handling the count_pulse() function. When the interrupts() function is called, the count_pulse() function will start to collect the pulses generated by the liquid flow sensor. This will continue for 1000 milliseconds, and then the noInterrupts() function is called to stop the operation of count_pulse() function. Then, the pulse count is assigned to the pulse variable and prints it on the serial monitor. This will repeat again and again inside the loop() function until you press the reset button or disconnect the Arduino from the power.

Calculating the water flow rate

The water flow rate is the amount of water flowing in at a given point of time and can be expressed in gallons per second or liters per second. The number of pulses generated per liter of water flowing through the sensor can be found in the water flow sensor's specification sheet. Let's say there are m pulses per liter of water.

You can also count the number of pulses generated by the sensor per second: Let's say there are n pulses per second.

The water flow rate R can be expressed as:

In litres per second

Also, you can calculate the water flow rate in liters per minute using the following formula:

For example, if your water flow sensor generates 450 pulses for one liter of water flowing through it, and you get 10 pulses for the first second, then the elapsed water flow rate is:

10/450 = 0.022 liters per second or 0.022 * 1000 = 22 milliliters per second.

The following steps will explain you how to calculate the water flow rate using a simple Arduino sketch:

  1. Open a new Arduino IDE and copy the sketch named B04844_03_02.ino.
  2. Verify and upload the sketch on the Arduino board.
  3. The following code block will calculate the water flow rate in milliliters per second:
    Serial.print("Water flow rate: ");
    Serial.print(pulse * 1000/pulses_per_litre);
    Serial.println("milliliters per second");
  4. Open the Arduino Serial Monitor and blow air through the water flow sensor using your mouth.
  5. The number of pulses per second and the water flow rate in milliliters per second will print on the Arduino Serial Monitor for each loop, as shown in the following screenshot:

    Pulses per second and water flow rate in each loop

Calculating the water flow volume

The water flow volume can be calculated by summing up the product of flow rate and the time interval:

Volume = ∑ Flow Rate * Time_Interval

The following Arduino sketch will calculate and output the total water volume since the device startup:

  1. Open a new Arduino IDE and copy the sketch named B04844_03_03.ino.
  2. The water flow volume can be calculated using following code block:
    volume = volume + flow_rate * 0.1; //Time Interval is 0.1 second
    Serial.print("Volume: ");
    Serial.print(volume);
    Serial.println(" milliliters");
  3. Verify and upload the sketch on the Arduino board.
  4. Open the Arduino Serial Monitor and blow air through the water flow sensor using your mouth.
  5. The number of pulses per second, water flow rate in milliliters per second, and total volume of water in milliliters will be printed on the Arduino Serial Monitor for each loop, as shown in the following screenshot:

    Pulses per second, water flow rate and in each loop and sum of volume

 To accurately measure water flow rate and volume, the water flow sensor needs to be carefully calibrated. The hall effect sensor inside the housing is not a precision sensor, and the pulse rate does vary a bit depending on the flow rate, fluid pressure, and sensor orientation.

Adding an LCD screen to the water meter

You can add an LCD screen to your newly built water meter to display readings, rather than displaying them on the Arduino serial monitor. You can then disconnect your water meter from the computer after uploading the sketch on to your Arduino.

Using a Hitachi HD44780 driver compatible LCD screen and Arduino Liquid Crystal library, you can easily integrate it with your water meter. Typically, this type of LCD screen has 16 interface connectors. The display has two rows and 16 columns, so each row can display up to 16 characters.

The following image represents the top view of a Hitachi HD44760 driver compatible LCD screen. Note that the 16-pin header is soldered to the PCB to easily connect it with a breadboard.

Hitachi HD44780 driver compatible LCD screen (16 x 2)—Top View

The following image represents the bottom view of the LCD screen. Again, you can see the soldered 16-pin header.

Hitachi HD44780 driver compatible LCD screen (16x2)—Bottom View

Wire your LCD screen with Arduino as shown in the next diagram. Use the 10k potentiometer to control the contrast of the LCD screen. Now, perform the following steps to connect your LCD screen with your Arduino:

  1. LCD RS pin (pin number 4 from left) to Arduino digital pin 8.
  2. LCD ENABLE pin (pin number 6 from left) to Arduino digital pin 7.
  3. LCD READ/WRITE pin (pin number 5 from left) to Arduino GND.
  4. LCD DB4 pin (pin number 11 from left) to Arduino digital pin 6.
  5. LCD DB5 pin (pin number 12 from left) to Arduino digital pin 5.
  6. LCD DB6 pin (pin number 13 from left) to Arduino digital pin 4.
  7. LCD DB7 pin (pin number 14 from left) to Arduino digital pin 3.
  8. Wire a 10K pot between Arduino +5V and GND, and wire its wiper (center pin) to LCD screen V0 pin (pin number 3 from left).
  9. LCD GND pin (pin number 1 from left) to Arduino GND.
  10. LCD +5V pin (pin number 2 from left) to Arduino 5V pin.
  11. LCD Backlight Power pin (pin number 15 from left) to Arduino 5V pin.
  12. LCD Backlight GND pin (pin number 16 from left) to Arduino GND.

    Fritzing representation of the circuit

  13. Open a new Arduino IDE and copy the sketch named B04844_03_04.ino.

  14. First initialize the Liquid Crystal library using following line:

    #include <LiquidCrystal.h>
  15. To create a new LCD object with following parameters, the syntax is LiquidCrystal lcd (RS, ENABLE, DB4, DB5, DB6, DB7):
    LiquidCrystal lcd(8, 7, 6, 5, 4, 3);
  16. Then initialize number of rows and columns in the LCD. Syntax is lcd.begin(number_of_columns, number_of_rows):
    lcd.begin(16, 2);
  17. You can set the starting location to print a text on the LCD screen using following function, syntax is lcd.setCursor(column, row):
    lcd.setCursor(7, 1);
    The column and row numbers are 0 index based and the following line will start to print a text in the intersection of the 8th column and 2nd row.
  18. Then, use the lcd.print() function to print some text on the LCD screen:
    lcd.print(" ml/s");
  19. Verify and upload the sketch on the Arduino board.
  20. Blow some air through the water flow sensor using your mouth.

You can see some information on the LCD screen such as pulses per second, water flow rate, and total water volume from the beginning of the time:

 LCD screen output

Converting your water meter to a web server

In the previous steps, you learned how to display your water flow sensor's readings and calculate water flow rate and total volume on the Arduino serial monitor. In this step, you will learn how to integrate a simple web server to your water flow sensor and remotely read your water flow sensor's readings.

You can make an Arduino web server with Arduino WiFi Shield or Arduino Ethernet shield. The following steps will explain how to convert the Arduino water flow meter to a web server with Arduino Wi-Fi shield:

  1. Remove all the wires you have connected to your Arduino in the previous sections in this article.
  2. Stack the Arduino WiFi shield on the Arduino board using wire wrap headers. Make sure the Arduino WiFi shield is properly seated on the Arduino board.
  3. Now, reconnect the wires from water flow sensor to the Wi-Fi shield. Use the same pin numbers as used in the previous steps.
  4. Connect the 9VDC power supply to the Arduino board.
  5. Connect your Arduino to your PC using the USB cable and upload the next sketch. Once the upload is completed, remove your USB cable from the Arduino.
  6. Open a new Arduino IDE and copy the sketch named B04844_03_05.ino.
  7. Change the following two lines according to your WiFi network settings, as shown here:
    char ssid[] = "MyHomeWiFi";
    
    char pass[] = "secretPassword";
  8. Verify and upload the sketch on the Arduino board.
  9. Blow the air through the water flow sensor using your mouth, or it would be better if you can connect the water flow sensor to a water pipeline to see the actual operation with the water.
  10. Open your web browser, type the WiFi shield's IP address assigned by your network, and hit the Enter key:
    http://192.168.1.177
  11. You can see your water flow sensor's pulses per second, flow rate, and total volume on the Web page. The page refreshes every 5 seconds to display updated information.
  12. You can add an LCD screen to the Arduino WiFi shield as discussed in the previous step. However, remember that you can't use some of the pins in the Wi-Fi shield because they are reserved for SD (pin 4), SS (pin 10), and SPI (pin 11, 12, 13). We have not included the circuit and source code here in order to make the Arduino sketch simple.

A little bit about plumbing

Typically, the direction of the water flow is indicated by an arrow mark on top of the water flow meter's enclosure. Also, you can mount the water flow meter either horizontally or vertically according to its specifications. Some water flow meters can mount both horizontally and vertically.

You can install your water flow meter to a half-inch pipeline using normal BSP pipe connectors. The outer diameter of the connector is 0.78" and the inner thread size is half-inch.

The water flow meter has threaded ends on both sides. Connect the threaded side of the PVC connectors to both ends of the water flow meter. Use a thread seal tape to seal the connection, and then connect the other ends to an existing half-inch pipeline using PVC pipe glue or solvent cement.

Make sure that you connect the water flow meter with the pipe line in the correct direction. See the arrow mark on top of the water flow meter for flow direction.

BNC pipe line connector made by PVC

Securing the connection between the water flow meter and BNC pipe connector using thread seal

PVC solvent cement.

Image taken from https://www.flickr.com/photos/ttrimm/7355734996/

Summary

In this article, you gained hands-on experience and knowledge about water flow sensors and counting pulses while calculating and displaying them. Finally, you made a simple web server to allow users to read the water meter through the Internet. You can apply this to any type of liquid, but make sure to select the correct flow sensor because some liquids react chemically with the material that the sensor is made of. You can Google and find which flow sensors support your preferred liquid type.

Resources for Article:


Further resources on this subject:


You've been reading an excerpt of:

Internet of Things with Arduino Blueprints

Explore Title
comments powered by Disqus