Calculating the flow rate and volume of water is a common requirement in many applications, such as monitoring the water usage in a household or measuring the flow rate of water in a pipe. With the help of an Arduino microcontroller and a flow sensor, we can easily measure the flow rate and volume of water and display the results on an LCD screen.
In this water sensor with Arduino tutorial, we will walk you through the process of building a simple water flow meter using an Arduino microcontroller and a flow sensor. We will explain how to connect the flow sensor to the Arduino, how to measure the flow rate and volume of water, and how to display the results on an LCD screen.
Water Flow Sensor
Before we start with the code and connections, let's briefly discuss the water flow sensor. Water flow sensors are devices that measure the rate at which water flows through a pipe. There are many types of flow sensors available in the market, but we will use the YF-S201 Hall Effect Water Flow Sensor for this project. This sensor has a simple, compact design and can measure the flow rate of water up to 30 liters per minute.
The YF-S201 sensor consists of a plastic valve body, a water rotor, and a Hall Effect sensor. The rotor is made of plastic and has a small magnet embedded in it. As water flows through the valve body, the rotor spins, and the Hall Effect sensor detects the magnetic field of the magnet in the rotor. By counting the number of rotations of the rotor in a given time, we can calculate the flow rate of water.
Connecting the Flow Sensor to the Arduino
To connect the YF-S201 flow sensor to the Arduino, we need to make the following connections:
- VCC pin of the flow sensor to the 5V pin of the Arduino
- GND pin of the flow sensor to the GND pin of the Arduino
- OUT pin of the flow sensor to the digital pin 2 of the Arduino
The circuit diagram below shows how to connect the water flow sensor with Arduino and LCD connection to Arduino.
Calculating Flow Rate and Volume of Water
Once we have connected the flow sensor to the Arduino, we can start measuring the flow rate and volume of water. In the above code, we have used the interrupt feature to count the number of pulses generated by the flow sensor. The pulse count is then used to calculate the flow rate and volume of water.
The flow rate is calculated as follows:
flow_rate = pulse * 1000 / pulses_per_litre;
where pulse
is the number of pulses counted in one second and pulses_per_litre
is the number of pulses generated by the flow sensor for one liter of water.
The volume of water that has passed through the flow sensor is calculated as follows:
volume = volume + flow_rate * 0.1;
where volume
is the total volume of water that has passed through the flow sensor, and flow_rate
is the current flow rate of water in ml/s. We multiply the flow rate by 0.1 to convert it from ml/s to ml/second.
Displaying the Results on the LCD Screen
Finally, we display the flow rate and volume of water on an LCD screen. We have used the LiquidCrystal library to control the LCD screen. The lcd.print()
function is used to display the flow rate and volume of water on the LCD screen. The lcd.setCursor()
function is used to set the position of the cursor on the LCD screen.
The complete Arduino code for the water flow rate and volume measurement is given below:
Here's a breakdown of what the code does:
The
LiquidCrystal.h
library is included at the top of the code, which defines the LiquidCrystal class that is used to control the LCD.Several global variables are declared, including
pin
, which is the input pin to which the flow sensor is connected;pulse
, which is used to count the number of pulses from the flow sensor;volume
, which is the total volume of liquid that has flowed through the sensor;flow_rate
, which is the current flow rate of the liquid; andpulses_per_litre
, which is the number of pulses generated by the flow sensor for each liter of liquid that passes through it.In the
setup()
function, the serial port is initialized for communication, the input pin is set up for reading, and an interrupt is attached to the pin that is triggered on a rising edge.The
lcd
object is initialized with the pins that are connected to the LCD display, and a welcome message is printed to the display.In the
loop()
function, thepulse
variable is reset to 0, interrupts are enabled to count pulses from the flow sensor for one second, interrupts are disabled, and theflow_rate
is calculated from the number of pulses counted in that second.The
flow_rate
is then displayed on the first line of the LCD screen, along with the units of ml/s.The
volume
of liquid that has passed through the sensor is then calculated asvolume = volume + flow_rate * 0.1
, and displayed on the second line of the LCD screen, along with the units of ml.Finally, the
count_pulse()
function is called when an interrupt is triggered by the flow sensor, which increments thepulse
variable.
Overall, this code provides a simple example of how to interface with a flow sensor and display the results on an LCD screen. It could be extended to include additional functionality, such as logging the flow data to a file or database, or triggering alarms based on the flow rate or volume.
References
[1] ATmega328P Input Capture with Interrupt
[2] Interrupt based Temperature Reading and Display with ATmega32