import time
from machine import Pin
# Define the relay control pin (Change GPIO pin as necessary)
RELAY_PIN = Pin(27, Pin.OUT) # GPIO 27 connected to relay module
# Function to turn the relay ON
def turn_on_relay():
RELAY_PIN.value(1) # Turn ON the relay (allow current to flow to the connected device)
print("Relay is ON.")
# Function to turn the relay OFF
def turn_off_relay():
RELAY_PIN.value(0) # Turn OFF the relay (stop current to the connected device)
print("Relay is OFF.")
# Main loop
def main():
while True:
# For demonstration, turn the relay on and off every 5 seconds
turn_on_relay() # Turn ON the relay
time.sleep(2) # Wait for 5 seconds
turn_off_relay() # Turn OFF the relay
time.sleep(2) # Wait for 5 seconds
# Run the main loop
main()