0
0
Raspberry Piprogramming~20 mins

Home automation with relay modules in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Home Automation Relay Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
GPIO Pin Setup for Relay Control
What is the output of this Raspberry Pi Python code snippet that sets up GPIO pin 17 as an output and turns it on?
Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.HIGH)
print(GPIO.input(17))
A0
B1
CGPIO.HIGH
DRuntimeError
Attempts:
2 left
💡 Hint
GPIO.HIGH sets the pin to high voltage, so reading it back should give 1.
🧠 Conceptual
intermediate
2:00remaining
Relay Module Safety Consideration
Which of the following is the most important safety consideration when connecting a relay module to a Raspberry Pi for home automation?
AEnsuring the relay coil voltage matches the Raspberry Pi GPIO voltage
BConnecting the relay output directly to the Raspberry Pi 5V pin
CUsing a separate power supply for the relay coil to avoid damaging the Raspberry Pi
DUsing the Raspberry Pi 3.3V pin to power the relay coil
Attempts:
2 left
💡 Hint
Relays often require more current than a GPIO pin can safely provide.
🔧 Debug
advanced
2:00remaining
Debugging Relay Control Code
This code is intended to toggle a relay connected to GPIO pin 22 on and off every second. What error will it raise when run?
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)

while True:
    GPIO.output(22, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(22, GPIO.LOW)
    time.sleep(1)
ASyntaxError: expected ':' after 'while true'
BRuntimeError: GPIO pin already in use
CIndentationError: unexpected indent
DNo error, runs correctly
Attempts:
2 left
💡 Hint
Check the syntax of the while loop line.
🚀 Application
advanced
2:00remaining
Relay Control with Button Input
You want to write a program that turns on a relay connected to GPIO 18 only while a button connected to GPIO 23 is pressed. Which code snippet correctly implements this behavior?
A
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    if GPIO.input(23) == GPIO.HIGH:
        GPIO.output(18, GPIO.HIGH)
    else:
        GPIO.output(18, GPIO.LOW)
B
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.IN)

while True:
    if GPIO.input(23) == GPIO.HIGH:
        GPIO.output(18, GPIO.HIGH)
    else:
        GPIO.output(18, GPIO.LOW)
C
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

while True:
    if GPIO.input(23) == GPIO.HIGH:
        GPIO.output(18, GPIO.HIGH)
    else:
        GPIO.output(18, GPIO.LOW)
D
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    if GPIO.input(23) == GPIO.LOW:
        GPIO.output(18, GPIO.HIGH)
    else:
        GPIO.output(18, GPIO.LOW)
Attempts:
2 left
💡 Hint
Buttons often use pull-up resistors and read LOW when pressed.
Predict Output
expert
2:00remaining
Relay Control with Asyncio and Cleanup
What will be the output of this Raspberry Pi Python code that toggles a relay on GPIO 27 asynchronously three times, then cleans up GPIO?
Raspberry Pi
import asyncio
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.OUT)

async def toggle_relay():
    for _ in range(3):
        GPIO.output(27, GPIO.HIGH)
        print('Relay ON')
        await asyncio.sleep(0.5)
        GPIO.output(27, GPIO.LOW)
        print('Relay OFF')
        await asyncio.sleep(0.5)

async def main():
    await toggle_relay()
    GPIO.cleanup()

asyncio.run(main())
A
Relay ON
Relay OFF
Relay ON
Relay OFF
Relay ON
Relay OFF
B
Relay ON
Relay ON
Relay ON
Relay OFF
Relay OFF
Relay OFF
CRuntimeError: Event loop is closed
DNo output, program hangs
Attempts:
2 left
💡 Hint
The code prints 'Relay ON' and 'Relay OFF' alternately three times.