The relay is the interface between your Raspberry Pi and high-voltage components. It enables you to control the lighting of a component requiring high power, such as a light bulb, from the Raspberry Pi. It is widely used in home automation to control roller shutters, coffee machines and heaters from a Raspberry Pi.
The relay consists of two parts: the control part (on the Raspberry Pi side) and the power part. These two parts are separated by galvanic isolation. To supply the energy needed to light a lamp, for example, you’ll need an external power supply delivering the power required for what you want to light.
Here are the relay pins:
The relay can be operated on 230V, which can be dangerous. Be careful what you do. Arduino Factory is not responsible for any problems.
The Raspberry Pi’s GPIOS operate at +3.3V. This means you need a relay operating at +3.3V. A relay with a higher operating voltage won’t be able to open and close its switch.
Here’s the diagram to connect the relay to the Raspberry Pi:
In the diagram, you can replace the lamp with the object you wish to control.
Here’s the program that opens and closes the relay:
import RPi.GPIO as GPIO
import time
# Pin configuration
relay_pin = 17
# Initial GPIO configuration
GPIO.setmode(GPIO.BCM)
GPIO.setup(relay_pin, GPIO.OUT)
try:
while True:
print(“Relay activated”)
GPIO.output(relay_pin, GPIO.HIGH) # Activate relay
time.sleep(2) # Keep relay activated for 2 seconds
print(“Relay deactivated”)
GPIO.output(relay_pin, GPIO.LOW) # Deactivate relay
time.sleep(2) # Wait 2 seconds before next activation
except KeyboardInterrupt:
pass
finally:
# Clean GPIO resources
GPIO.cleanup()