• First Steps
  • General
  • Projects
Facebook Twitter YouTube
Tutorials for Raspberry Pi Tutorials for Raspberry Pi
  • Hardware & GPIO
  • Sensors & Components
  • Scale
  • Cases
  • Fingerprint
  • Gas Sensor
  • Heartbeat
  • German
Tutorials for Raspberry Pi Tutorials for Raspberry Pi
Home»Hardware & GPIO»Reading out a Flow Meter/Water Flow Sensor on the Raspberry Pi

Reading out a Flow Meter/Water Flow Sensor on the Raspberry Pi

Facebook Twitter LinkedIn Tumblr Email Reddit
Durchflussmesser / Water Flow Sensor am Raspberry Pi auslesen
Share
Facebook Twitter LinkedIn Email Tumblr Reddit Telegram WhatsApp

In projects such as the automated greenhouse, it is important to know how much water, for example, reaches the plants for irrigation. There is also an inexpensive variant that we can quickly read using the Python programming language and also integrate into our smart home system. We can query the Raspberry Pi Water Flow Sensor at any frequency (e.g. every second) to determine how many litres flow through it per second or minute.

You can find the exact setup and the code for it in this tutorial.

 

Required Hardware Parts

Depending on the project, you will be using a few other hardware parts. In this tutorial, however, we will limit ourselves to reading out the sensor and will therefore only assume the parts that are absolutely necessary. These are:

  • Raspberry Pi
  • Water flow sensor (model YF-S201)
  • Jumper cables
  • optional: water pump (voltage depends on the required power)

In order to be able to connect a hose, we need a so-called tap connector. There are two ways to do this:

  1. Print it yourself (3D printer required) with the help of this file
  2. Buy it (half inch)

 

Connection of the Water Flow Sensor YF-S201 to the Raspberry Pi

In the data sheet of the sensor we find different information, of which the most important are:

  • Input voltage: 3.5 – 24V
  • Flow Range: 1-30 litres per minute
  • F = 7Q (L/MIN)
  • Sample frequencies:
    • 2L/MIN=16HZ
    • 4L/MIN=32.5HZ
    • 6L/MIN=49.3HZ
    • 8L/MIN=65.5HZ
    • 10L/MIN=82HZ
  • a single signal pin (yellow)

The connection to the Raspberry Pi using a jumper cable is easy thanks to the three pins:

  1. GND (black) to GND (Pin 6)
  2. VCC (red) to 3.3V (Pin 1)
  3. Signal (yellow) an GPIO 13 (Pin 33)

Although at least 3.5V is required according to the data sheet, I was able to read the signal successfully even at 3.3V. If this is not possible, you can use 5V and put a larger resistor between the signal output and the GPIO.

In principle, any other free GPIO pin can also be used:

Raspberry Pi GPIO Pin Assignment
Raspberry Pi GPIO Pin Assignment

Now all that remains is the connection of the hoses. We need the tap connector for the flow meter, which we can best print ourselves:

Raspberry Pi Water Flow Sensor Tap Connector

If you are using a different Raspberry Pi Water Flow Sensor, then pay attention to the description. The one used here had the following information:

  • Inside diameter: 11 mm
  • Outer diameter: 20 mm

Accordingly, a part can be easily edited using tools such as Tinkercad. The same also applies to hose connections: If you want to connect water hoses with different dimensions, you can print these connectors yourself.

 

Reading out Raspberry Pi Water Flow Sensor via Python

We now create a new Python script with the following content:

sudo nano water_flow.py

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python
import RPi.GPIO as GPIO
import time, sys
#import paho.mqtt.publish as publish
 
FLOW_SENSOR_GPIO = 13
#MQTT_SERVER = "192.168.1.220"
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(FLOW_SENSOR_GPIO, GPIO.IN, pull_up_down = GPIO.PUD_UP)
 
global count
count = 0
 
def countPulse(channel):
   global count
   if start_counter == 1:
      count = count+1
 
GPIO.add_event_detect(FLOW_SENSOR_GPIO, GPIO.FALLING, callback=countPulse)
 
while True:
    try:
        start_counter = 1
        time.sleep(1)
        start_counter = 0
        flow = (count / 7.5) # Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
        print("The flow is: %.3f Liter/min" % (flow))
        #publish.single("/Garden.Pi/WaterFlow", flow, hostname=MQTT_SERVER)
        count = 0
        time.sleep(5)
    except KeyboardInterrupt:
        print('\nkeyboard interrupt!')
        GPIO.cleanup()
        sys.exit()
 

Save and exit the editor with CTRL + O, CTRL + X.

Now to explain the code:

  • Line 1-13: Imports and definitions
  • Lines 15-18: Function that is called when the voltage applied to the GPIO changes
  • Line 20: Definition of which function should be called (from HIGH to LOW -> also FALLING)
  • Line 22: Infinite loop
  • Lines 24-27: First we “activate” the counter (start_counter = 1) and then wait a second. Then we deactivate the counter again and calculate the flow per minute.
  • Optional (lines 4, 7, 29): If we want to send the result via MQTT.

Depending on the data sheet and implementation, values per pulse frequency (Hz) of 7.5Q are found, with Q indicating the flow rate in litres/min. That means 7.5Hz = 1L/min. This value can deviate (in the linked data sheet it is more like 8-8.2 Hz) and should be adjusted accordingly.

 

Integration into the Smart Home (OpenHAB etc.)

If you water your beds, you definitely want to know how much water has flowed over time. MQTT is very suitable for this, as we can communicate via an interface. Using the example of OpenHAB, this is particularly easy, since a lot is already included.

All that is needed for this is a persistence layer (database in which the values are stored). The values can then be sent to a predefined channel via MQTT (information on this here). Corresponding lines are commented out in the code example above.

Last but not least, it would be advisable to set the Python script to autostart so that it sends data regularly.

 

Flow sensor Garden Greenhouse MQTT OpenHAB Smart Home
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleRaspberry Pi per Solarzelle und Akku mit Strom versorgen
Next Article Lecture des étiquettes RFID RC522 de Raspberry Pi (NFC)

Related Posts

How to build a ESP8266 Scale (with Weight Sensor HX711)

How-To: Bluetooth Connection between ESP32’s and Raspberry Pi’s

How to create a Solar Powered Raspberry Pi Bitcoin Miner

How to build a Smart Agriculture System using IoT

8 Comments

  1. Chuck Mulder on 2. August 2021 1:52

    Photos missing from tutorial

    Reply
  2. John William Jones on 10. September 2021 20:52

    This will be worth its weight in gold on my co2 laser cutter

    Reply
  3. Paresh Chauhan on 9. November 2021 10:48

    If i wuld like to connect 4 flow sensor to raspberry pi what changes can be made to program. Does It have I2C address. Can u suggest some hints to me

    Reply
  4. Ben on 30. March 2022 12:43

    Hello,

    I’m using your script for my flow meter, but I can’t find the pulse frequence.
    > flow = (count / 7.5) # Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.

    Do you know if this would be the same for all flow meter? Or is there a way I can test this? Tried it with the pump on, but it starts building pressure, max, later on less flow, so not sure how I can fine tune this. :$

    Reply
  5. dreslick on 10. September 2022 1:20

    Are these sensors safe for drinking water? Do they leach chemicals into the stream?

    Reply
    • kartik mudhliar on 1. April 2024 14:17

      No. there are no chemicals present in the sensor. The sensor is made up of plastic/fiber.

      Reply
  6. jim on 17. March 2023 16:59

    I have three different flow meters, from a different brand, but they look like the one mentioned here. Each has a different F value, and they are all off – one meter measures the volume entering the tank, the other measures it leaving – these measure .95 and 3.45. I will, and I recommend everyone, calibrate these meters. You can build a rig into which you pour an exact volume of water and measure the pulses. You should make multiple measurements, and repeat for several volumes of water and realize there will be variations between tests.

    Reply
  7. kartik mudhliar on 1. April 2024 14:16

    Getting the below error:
    Traceback (most recent call last):
    File “/home/punelab/flow2.py”, line 20, in
    GPIO.add_event_detect(FLOW_SENSOR_GPIO, GPIO.FALLING, callback=countPulse)
    RuntimeError: Failed to add edge detection

    Reply

Leave A Reply Cancel Reply

Run a Raspberry Pi on rechargeable Batteries

Controlling the Raspberry Pi RTC Module – I2C Real Time Clock

Control your Raspberry Pi by using a wireless Xbox 360 controller

Top 21 OS for the Raspberry Pi for your Project – Overview

Raspberry Pi Robot Kit – Assembly (Part 1)

Connect and Control WS2812 RGB LED Strips via Raspberry Pi

Subscribe to Blog
Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!
Subscribe

Thank you!

We will contact you soon.

Tutorials for Raspberry Pi
Facebook Twitter YouTube
  • Contact & Disclaimer

Type above and press Enter to search. Press Esc to cancel.