0
0
Raspberry Piprogramming~30 mins

Home automation with relay modules in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
Home automation with relay modules
📖 Scenario: You have a Raspberry Pi connected to 3 relay modules. Each relay controls a different home appliance: a lamp, a fan, and a heater. You want to write a simple program to turn these appliances on or off based on a list of commands.
🎯 Goal: Build a Python program that stores the relay pins and appliance names, sets a command to turn appliances on or off, applies the commands using a loop, and prints the final states of each appliance.
📋 What You'll Learn
Create a dictionary with relay pins as keys and appliance names as values
Create a dictionary with appliance names as keys and their desired states ('ON' or 'OFF') as values
Use a for loop to iterate over the relay pins and appliances
Print the appliance name and its final state
💡 Why This Matters
🌍 Real World
Home automation systems use relay modules to control appliances like lights, fans, and heaters remotely or automatically.
💼 Career
Understanding how to control hardware with code is important for jobs in IoT, embedded systems, and home automation engineering.
Progress0 / 4 steps
1
Set up relay pins and appliances
Create a dictionary called relays with these exact entries: 17: 'Lamp', 27: 'Fan', 22: 'Heater'.
Raspberry Pi
Need a hint?

Use curly braces {} to create a dictionary with keys as pin numbers and values as appliance names.

2
Set appliance commands
Create a dictionary called commands with these exact entries: 'Lamp': 'ON', 'Fan': 'OFF', 'Heater': 'ON'.
Raspberry Pi
Need a hint?

Use a dictionary to map appliance names to their desired states.

3
Apply commands to relays
Use a for loop with variables pin and appliance to iterate over relays.items(). Inside the loop, create a variable state that gets the command for the appliance from commands.
Raspberry Pi
Need a hint?

Use relays.items() to get both pin and appliance in the loop.

4
Print appliance states
Inside the for loop, add a print statement that shows the appliance name and its state in this exact format: "Lamp is ON".
Raspberry Pi
Need a hint?

Use an f-string to print the appliance and its state exactly as shown.