Water_Flow_Control_Python_Project_Notes
Water_Flow_Control_Python_Project_Notes
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
3. Circuit Connections
# 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
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.
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