Home automation lets you control devices like lights or fans from your Raspberry Pi. Relay modules act like switches that the Pi can turn on or off safely.
Home automation with relay modules in Raspberry Pi
import RPi.GPIO as GPIO import time relay_pin = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(relay_pin, GPIO.OUT) # To turn relay ON GPIO.output(relay_pin, GPIO.LOW) # To turn relay OFF GPIO.output(relay_pin, GPIO.HIGH)
Relay modules usually work with LOW signal to turn ON and HIGH to turn OFF.
Use GPIO.BCM mode to refer to pins by their Broadcom number.
import RPi.GPIO as GPIO relay_pin = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(relay_pin, GPIO.OUT) # Turn relay ON GPIO.output(relay_pin, GPIO.LOW) # Wait 2 seconds import time time.sleep(2) # Turn relay OFF GPIO.output(relay_pin, GPIO.HIGH) GPIO.cleanup()
import RPi.GPIO as GPIO import time relay_pin = 27 GPIO.setmode(GPIO.BCM) GPIO.setup(relay_pin, GPIO.OUT) # Toggle relay ON and OFF 3 times for _ in range(3): GPIO.output(relay_pin, GPIO.LOW) time.sleep(1) GPIO.output(relay_pin, GPIO.HIGH) time.sleep(1) GPIO.cleanup()
This program turns a relay connected to pin 22 ON for 3 seconds, then turns it OFF and cleans up the GPIO pins.
import RPi.GPIO as GPIO import time relay_pin = 22 GPIO.setmode(GPIO.BCM) GPIO.setup(relay_pin, GPIO.OUT) print('Turning relay ON') GPIO.output(relay_pin, GPIO.LOW) # Relay ON print('Relay is ON for 3 seconds') time.sleep(3) print('Turning relay OFF') GPIO.output(relay_pin, GPIO.HIGH) # Relay OFF GPIO.cleanup() print('Program finished')
Always connect relay modules to a separate power supply if they need more current than the Pi can provide.
Use GPIO.cleanup() at the end to reset pins and avoid warnings next time you run the program.
Check your relay module's datasheet to confirm if LOW or HIGH activates the relay.
Relay modules let your Raspberry Pi safely control high-power devices.
Use GPIO pins to send LOW or HIGH signals to switch the relay ON or OFF.
Always clean up GPIO pins after your program finishes.
Practice
Solution
Step 1: Understand relay module function
A relay module acts as a switch controlled by the Raspberry Pi's GPIO pins to turn devices ON or OFF safely.Step 2: Identify the purpose in home automation
It allows the Pi to control high-power devices without directly handling high voltage or current.Final Answer:
To safely control high-power devices using low-power GPIO signals -> Option BQuick Check:
Relay modules = safe device control [OK]
- Thinking relay speeds up the Pi
- Confusing relay with data storage
- Assuming relay provides network access
Solution
Step 1: Set GPIO pin mode
Pin 17 must be set as output to control the relay, so GPIO.setup(17, GPIO.OUT) is correct.Step 2: Turn relay ON with active LOW
For active LOW relay, sending GPIO.LOW activates the relay, so GPIO.output(17, GPIO.LOW) is correct.Final Answer:
GPIO.setup(17, GPIO.OUT) followed by GPIO.output(17, GPIO.LOW) -> Option AQuick Check:
Active LOW means LOW signal turns relay ON [OK]
- Setting pin as input instead of output
- Using HIGH to turn ON active LOW relay
- Confusing pin numbers
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(22, GPIO.OUT) GPIO.output(22, GPIO.LOW) print(GPIO.input(22)) GPIO.cleanup()
Solution
Step 1: Setup and output LOW on pin 22
The code sets pin 22 as output and writes LOW (0) to it.Step 2: Read pin state with GPIO.input(22)
GPIO.input returns the current pin state; since output is LOW, it returns 0.Final Answer:
0 -> Option CQuick Check:
Output LOW means input reads 0 [OK]
- Expecting 1 instead of 0 after output LOW
- Thinking GPIO.input causes error on output pins
- Confusing GPIO.BCM with GPIO.BOARD numbering
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) GPIO.output(18, GPIO.LOW) GPIO.cleanup() GPIO.output(18, GPIO.HIGH)
Solution
Step 1: Understand GPIO.cleanup() effect
GPIO.cleanup() resets all GPIO pins, releasing resources and resetting pin states.Step 2: Check GPIO.output after cleanup
Calling GPIO.output after cleanup without re-setup causes a runtime error because pin 18 is no longer configured.Final Answer:
Calling GPIO.output after GPIO.cleanup causes an error -> Option AQuick Check:
Use GPIO.setup after cleanup before output [OK]
- Thinking cleanup only clears pin states but allows output
- Believing setup order is flexible
- Assuming GPIO.HIGH is invalid
Solution
Step 1: Setup pins 5 and 6 as outputs
Both pins must be outputs to control relays: GPIO.setup(5, GPIO.OUT) and GPIO.setup(6, GPIO.OUT).Step 2: Apply active LOW logic for relays
To turn ON the fan (pin 5), output LOW; to turn OFF the light (pin 6), output HIGH.Final Answer:
Setup pins as output, output LOW on 5, HIGH on 6 -> Option DQuick Check:
Active LOW ON = LOW, OFF = HIGH [OK]
- Setting pins as input instead of output
- Reversing HIGH and LOW for active LOW relay
- Turning both devices ON or OFF incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
