import RPi.GPIO as GPIO
import time
BUTTON_PIN = 17
RELAY_PIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(RELAY_PIN, GPIO.OUT)
print("System Ready. Press the button to turn ON motor.")
try:
while True:
if GPIO.input(BUTTON_PIN) == GPIO.HIGH:
GPIO.output(RELAY_PIN, GPIO.HIGH)
print("Motor ON")
else:
GPIO.output(RELAY_PIN, GPIO.LOW)
print("Motor OFF")
time.sleep(0.2)
except KeyboardInterrupt:
print("Program stopped")
finally:
GPIO.cleanup()