Last active
June 30, 2022 20:21
-
-
Save dave-malone/33a24410bd84486415f07e5cbcea6309 to your computer and use it in GitHub Desktop.
A simple Python program for Raspberry Pi that initiates a pump to deliver water from a reservoir when a water sensor reading returns a value indicating that the water level is running low
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time | |
WATER_SENSOR_PIN = 23 | |
RELAY_CONTROL_PIN = 27 | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(WATER_SENSOR_PIN, GPIO.IN) | |
GPIO.setup(RELAY_CONTROL_PIN, GPIO.OUT) | |
def water_level_low(): | |
if GPIO.input(WATER_SENSOR_PIN): | |
return False | |
else: | |
return True | |
def toggle_water_pump_on(): | |
GPIO.output(RELAY_CONTROL_PIN, GPIO.LOW) | |
def toggle_water_pump_off(): | |
GPIO.output(RELAY_CONTROL_PIN, GPIO.HIGH) | |
def top_off_water(): | |
if water_level_low() != True: | |
print("water level is good") | |
else: | |
startTime = time.time() | |
print("Water level is low; turning pump on") | |
toggle_water_pump_on() | |
while water_level_low(): | |
print("Topping off") | |
time.sleep(0.01) | |
print("Water level topped off; shutting pump off") | |
toggle_water_pump_off() | |
stopTime = time.time() | |
runTime = stopTime - startTime | |
print("Pump ran for %.2f seconds" % runTime) | |
try: | |
while True: | |
top_off_water() | |
time.sleep(5) | |
except KeyboardInterrupt: | |
print("Keyboard interrupt") | |
except: | |
print("unexpected error") | |
finally: | |
print("cleaning up") | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment