import RPi.GPIO as GPIO
import time
# GPIO mode
GPIO.setmode(GPIO.BCM)
# Pin definitions
RELAY_PIN = 23
LED_PIN = 18
# Setup pins as output
GPIO.setup(RELAY_PIN, GPIO.OUT)
GPIO.setup(LED_PIN, GPIO.OUT)
print("Relay and LED control started")
try:
while True:
# Turn ON relay and LED
GPIO.output(RELAY_PIN, GPIO.HIGH)
GPIO.output(LED_PIN, GPIO.HIGH)
print("Relay ON, LED ON")
time.sleep(2)
# Turn OFF relay and LED
GPIO.output(RELAY_PIN, GPIO.LOW)
GPIO.output(LED_PIN, GPIO.LOW)
print("Relay OFF, LED OFF")
time.sleep(2)
except KeyboardInterrupt:
print("Program stopped by user")
finally:
GPIO.cleanup()