Water Flow Control System with Python
1. Introduction
This document describes the implementation of a Water Flow Control System using Python. Unlike traditional
systems based on Arduino, this setup uses Python (running on a Raspberry Pi or similar) to control and
monitor water flow and tank levels. It interfaces with sensors and actuators through GPIO, I2C, and
interrupts.
2. Components Required
1. Raspberry Pi (any model with GPIO)
2. Water Level Sensor (Ultrasonic HC-SR04 or multiple float sensors)
3. Water Flow Sensor (YF-S201)
4. Relay Module (5V relay for pump control)
5. 12V DC Water Pump
6. LCD Display (16x2 with I2C backpack)
7. Power Supply (5V for Pi, 12V for pump)
8. Jumper wires, resistors, and breadboard
3. Circuit Connections
- Ultrasonic Sensor: Trigger -> GPIO23, Echo -> GPIO24
- Flow Sensor YF-S201: Output -> GPIO17 (with interrupt)
- Relay IN -> GPIO27
- LCD (I2C): SDA -> GPIO2, SCL -> GPIO3
- Water pump connected via relay to 12V power
4. Python Code Overview
import RPi.GPIO as GPIO
import time
from smbus import SMBus
import threading
# GPIO setup
TRIG = 23
ECHO = 24
FLOW = 17
RELAY = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
GPIO.setup(FLOW, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(RELAY, GPIO.OUT)
flow_count = 0
def flow_callback(channel):
global flow_count
flow_count += 1
GPIO.add_event_detect(FLOW, GPIO.FALLING, callback=flow_callback)
def read_distance():
GPIO.output(TRIG, False)
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
return round(distance, 2)
def control_pump(distance):
if distance > 20: # tank level low
GPIO.output(RELAY, GPIO.HIGH)
else:
GPIO.output(RELAY, GPIO.LOW)
try:
while True:
distance = read_distance()
control_pump(distance)
print(f"Water Level Distance: {distance} cm | Flow Pulses: {flow_count}")
flow_count = 0
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
5. Key Advantages
- No need for separate microcontrollers; single Python script handles all logic.
- Easily extendable with web interfaces using Flask/Django.
- Real-time control and monitoring.
- Ideal for integration with smart home/IoT systems.
6. Conclusion
Using Python on Raspberry Pi offers a flexible and powerful platform for developing real-time automation
projects like a water flow and level control system. With proper sensor integration and GPIO handling, the
system can be robust and easily adaptable.