Raspberry Pi 5 Home Automation with Relays - Proteus Simulation: the Raspberry Pi 5, the 4-channel relay, the lamps, the fan and the wall switches

Raspberry Pi 5 Home Automation with Relays - Proteus Simulation

40
700
60
25
60
PCBWay

Hello friends, I hope you all are doing great. Today, I am going to share the Raspberry Pi 5 Home Automation with Relays - Proteus Simulation, project 102 of our IoT Projects series. Raspberry Pi home automation is one of the most popular projects there is: switch the lights and the fan from your phone. Here is a complete one with the circuit, the Python code and a Proteus simulation, and with the part most tutorials skip: the normal switches on the wall keep working too.

A Raspberry Pi 5 drives a 4-channel relay module for four loads of a small house: the living room light, the bedroom light, the ceiling fan and the porch light. Each load can be switched from a web dashboard on a laptop or a phone, and from its own rocker switch on the wall. The wall switches work as two-way switches: every flip changes the light, no matter what the web did before, so nobody in the house is ever locked out by the automation. On top of that the fan has a sleep timer, the porch light follows a schedule, and the Pi keeps count of how long each load ran and the energy it used. In this tutorial, we will go through the circuit, how a relay module switches a load, how the wall switches are read with pull-ups and interrupts, the two-way switching logic, the timer and the schedule, and the Python code. Then we live through an evening in the simulation. At the end, you can download the project with the program and every library file.

NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the Raspberry Pi 5 home automation with relays in Proteus simulation:

Raspberry Pi 5 Home Automation with Relays - Proteus Simulation: the Raspberry Pi 5, the 4-channel relay, the lamps, the fan and the wall switches
Figure: The Raspberry Pi home automation in a Proteus simulation.
Raspberry Pi 5 home automation Proteus simulation: the porch light by schedule, lamps from the web and the wall, the fan on a timer
Figure: The project working in the Proteus simulation (an animation of the main steps).

What Does This Home Automation Do?

  • Four loads: the living room light, the bedroom light, the ceiling fan and the porch light, each on a relay.
  • A web dashboard: each load, ALL ON and ALL OFF, from a laptop or a phone.
  • Wall switches that always work: every flip toggles its load, whatever the web did.
  • A sleep timer: FAN TIMER runs the fan for 20 s and switches it off.
  • A schedule: the porch light comes on at 18:00 and goes off at 18:04 on its own.
  • Energy: every load's on-time and an estimate of its energy from its rated power.

In the simulation, the relay module's pop-up shows each coil and contact, the bulbs' pop-ups show their filament and current, the fan's pop-up its speed, and each wall switch has a Flip button.

Components Used in the Proteus Simulation

All the parts come from our Proteus libraries, each with its own tutorial. The project zip at the end includes every LIB and DLL file.

The parts of the home automation and their library posts
PartProteus deviceLibrary postConnection
Raspberry Pi 5RASPBERRY PI 5Raspberry Pi 5 Library for ProteusPI1, home.py, Ethernet 192.168.1.51
4-channel relay moduleRELAY4ADVTEPRelay Module Library for ProteusRLY1: IN1 - IN4 on GPIO5, 6, 13, 19
12 V bulbs (21 W, 5 W)BULBP21WTEP, BULBW5WTEPFilament Bulb Library for ProteusL1 living room, L2 bedroom, L3 porch
12 V 4-pin PC fanPCFAN4PINTEP4-Pin PC Fan Library for ProteusFAN1, the ceiling fan
Rocker switches (x 4)SWITCHROCKERTEPRocker Switch Library for ProteusSW1 - SW4 on GPIO17, 27, 22, 23
Resistor network 10 kRESNETSIP5TEPResistor Network Library for ProteusRN1: the switches' pull-ups
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the Home Automation: the Circuit

Raspberry Pi home automation Proteus simulation circuit: a Raspberry Pi 5, a 4-channel relay, lamps, a fan and wall switches
Figure: The complete circuit of the home automation on an A3 Proteus sheet.
The connections of the home automation
SignalRaspberry Pi 5Goes to
IN1 - IN4GPIO5, 6, 13, 19 (pins 29, 31, 33, 35)the relay module's inputs (active LOW)
S1 - S4GPIO17, 27, 22, 23 (pins 11, 13, 15, 16)the wall switches (to GND), pulled up by RN1
VCC5V (pin 2)the relay module's VCC and JD-VCC
3V3pin 1the resistor network's common
+12V-COM1 - COM4: the loads' supply through the relay contacts

The relay contacts switch the loads' own 12 V supply: each COM is on +12V and each NO goes to its load. The Raspberry Pi never touches that circuit; it only drives the relay module's inputs. In a real house the contacts would switch the mains instead, which is exactly why the relays are there.

How the Raspberry Pi Home Automation Works

Switching a Load with a Relay

A relay is a switch moved by an electromagnet. The module's inputs drive the coils through optocouplers and transistors, so a GPIO pin only lights an LED inside the optocoupler. These modules are active LOW: a LOW on IN1 pulls relay 1 in and connects COM1 to NO1; a HIGH releases it. That is why the program starts every output HIGH, so that the house wakes up with everything off, and why it writes 0 for on. The JD-VCC jumper feeds the coils from the same 5 V as the module's logic; on a real board you can pull it and give the coils their own supply.

Reading the Wall Switches

Each rocker switch connects its GPIO line to ground when it is turned on. One 10 k resistor of RN1 pulls each line up to 3V3, so an open switch reads HIGH and a closed one LOW. Instead of checking the switches in a loop, the program claims each line as an alert with lgpio and gets a callback on every edge, up or down, with 50 ms of debounce for the contact's bounce.

Two-Way Switching

Here is the problem every home automation meets: the living room light is on because someone pressed LIVING on the phone, and now the person in the room flips the wall switch. If the program read the switch's position as the light's state, the switch might already be in the "on" position, and the flip would do nothing. So the program does not look at the position at all; it treats every change as a request to toggle. Up or down, a flip always changes the light. That is how a two-way staircase switch behaves, and it means the wall switch and the web can never fight each other.

A Timer, a Schedule and the Energy

FAN TIMER switches the fan on and remembers when to switch it off; a small thread checks every 0.1 s. The porch light follows the home clock: on at 18:00, off at 18:04. To fit an evening into a short simulation, the home clock starts at 17:58 and runs 12 times faster than real time; a real installation simply uses the real clock. For the energy, the program adds up how long each load was on and multiplies by its rated power: 21 W, 5 W, 3.6 W and 5 W. There is no meter on the loads, so this is an estimate.

Choosing a Relay for Lamps and Motors

A relay's contact rating is printed for a resistive load, but a house is full of loads that are not resistive. A cold incandescent filament has a fraction of its hot resistance, so it draws many times its running current for the first moment; a motor draws a large starting current and makes a spark when it is switched off. Both wear the contacts much faster than the rating suggests. That is why relays are chosen with a wide margin for lamps and motors, why LED drivers with their input capacitors are treated the same way, and why a solid-state relay or a contactor takes over for big loads. The simulation shows this effect directly, as the results below will show.

The Python Code Explained

The program is home.py, written for the Raspberry Pi 5 with lgpio for the GPIO and http.server for the dashboard. The loads: their names, relay pins, wall switch pins and rated power:

LOADS = [('LIVING', 'living room light', 5, 17, 21.0), ('BEDROOM', 'bedroom light', 6, 27, 5.0), ('FAN', 'ceiling fan', 13, 22, 3.6),

The set-up: every relay released (HIGH), every switch an alert line with debounce and a callback:

    lgpio.gpio_claim_output(chip, relay, 1)                       # (1 = HIGH = the relay released)
    lgpio.gpio_set_debounce_micros(chip, wall, 50000)             # (a switch bounces: 50 ms)
    lgpio.callback(chip, wall, lgpio.BOTH_EDGES, on_wall)

A load switched on or off, the relay active LOW:

    lgpio.gpio_write(chip, LOADS[k][2], 0 if on else 1)          # (active LOW: 0 pulls the relay in)

Two-way switching: any change of a wall switch toggles its load:

                on = not st[k]['on']
            switch(k, on, 'wall switch')

The fan timer and the schedule:

            switch(2, False, 'timer ran out')
            s['sched_done'].add('on'); switch(k, True, 'schedule 18:00')

The energy estimate when a load changes:

        d['wh'] += d['w'] * dt / 3600.0

The dashboard's links come in rows of three: Refresh, ALL ON and ALL OFF; LIVING, BEDROOM and FAN; PORCH and FAN TIMER. /data gives the clock, every load and the events as JSON.

How to Run the Proteus Simulation

  1. Download the project zip below and install the library files and the TEPPi folder (the steps are under the download button).
  2. Open Pi-5-Home-Automation.pdsprj from the Proteus Simulation folder. Keep home.py and the DLL files next to it.
  3. Press Play. Open the Laptop WiFi Monitor (press Go) and press LIVING: the living room lamp lights.
  4. Click a wall switch (SW1 - SW4) for its pop-up and press Flip: its load changes.
  5. Watch the porch light come on at 18:00 on the home clock, and try FAN TIMER.

Simulation Results: an Evening at Home in Proteus

We pressed Play and lived through an evening: the porch light by the schedule, the living room from the laptop, the bedroom from its wall switch, the living room off again from its wall switch, the fan on a timer from the phone, and ALL OFF at the end. The times are the program's clock with the home clock in brackets; the whole run took 47.9 simulated seconds.

Ready at 17:58

The program reported READY at 0.00 s, 17:58 on the home clock. The relay module's pop-up shows all four relays released, every input HIGH, and the wall switch's pop-up an open contact: the house starts with everything off.

Raspberry Pi home automation Proteus simulation ready at 17:58: all lamps and the fan off
Figure: Ready: everything off.
Relay module pop-up in the home automation simulation: all four relays off, the inputs HIGH
Figure: The relay module: all four released.
Rocker switch pop-up in the home automation simulation: the living room wall switch open
Figure: A wall switch: open.
Laptop WiFi Monitor showing the home automation dashboard in the simulation: 17:58, 0 of 4 on
Figure: The dashboard.

The Porch Light at 18:00

At 10.00 s the home clock reached 18:00 and the program reported PORCH ON (schedule 18:00). Relay 4 pulled in and the porch lamp lit: 11.98 V, 416 mA, 4.99 W.

Raspberry Pi home automation Proteus simulation: the porch light switched on by the schedule at 18:00
Figure: The porch light by the schedule.
W5W bulb pop-up in the home automation simulation: the porch lamp at 11.98 V, 416 mA, 4.99 W
Figure: The porch lamp: 5 W.

The Living Room from the Web

We pressed LIVING on the laptop at 12.02 s: relay 1 pulled in and the 21 W living room lamp settled at 11.91 V, 1.74 A, 20.8 W. The bulb's pop-up shows something real lamps do too: the cold filament took 23.5 A at the instant of switching on, 13.5 times its running current, before it heated up in about 230 ms. The relay module's pop-up flags that peak in red: it is above the 10 A rating of the relay's contacts. This inrush is why relays are rated much lower for lamp loads than for plain resistive ones, and why a real installation picks its relays with a margin.

Raspberry Pi home automation Proteus simulation: the living room lamp switched on from the web
Figure: The living room light from the web.
P21W bulb pop-up in the home automation simulation: 20.8 W, 1.74 A, a 23.5 A switch-on surge
Figure: The living room lamp: a 23.5 A inrush, then 1.74 A.
Relay module pop-up in the home automation simulation: K1 and K4 on, the 23.5 A inrush flagged
Figure: The relay module: K1 and K4 on.
Laptop WiFi Monitor in the home automation simulation: LIVING ON from the web, the porch on by schedule
Figure: The dashboard: LIVING from the web.

Wall Switches That Always Work

At 14.75 s we flipped the bedroom's wall switch: BEDROOM ON (wall switch). Then the real test: the living room light had been switched on from the web, and at 16.80 s we flipped the living room's wall switch. The program reported LIVING OFF (wall switch). The switch now stands in its ON position while its light is off, and that does not matter: every flip toggles.

Raspberry Pi home automation Proteus simulation: the bedroom lamp switched on from its wall switch
Figure: The bedroom light from the wall.
Rocker switch pop-up in the home automation simulation: the bedroom wall switch flipped, the contact closed
Figure: The bedroom switch flipped.
W5W bulb pop-up in the home automation simulation: the bedroom lamp at 4.99 W
Figure: The bedroom lamp.
Laptop WiFi Monitor in the home automation simulation: the living room light off from its wall switch
Figure: The dashboard: switched off from the wall.

A Fan Timer from the Phone

From the phone we pressed FAN TIMER at 20.87 s: FAN ON (web: timer 20 s), off at 18:06:10 on the home clock. Relay 3 pulled in and the fan spun up: 1386 RPM on its way to 1499, 0.141 A. Meanwhile the schedule switched the porch light off at 30.00 s (18:04), and at 40.90 s the timer ran out: FAN OFF (timer ran out), the fan coasting down with no 12 V.

Mobile WiFi Monitor showing the home automation dashboard in the simulation
Figure: The phone.
Mobile WiFi Monitor in the home automation simulation: FAN TIMER pressed, 20 s left
Figure: The fan timer from the phone.
Raspberry Pi home automation Proteus simulation: the ceiling fan running on its 20 s timer
Figure: The fan on its timer.
Fan pop-up in the home automation simulation: speeding up, 1386 RPM, 0.141 A
Figure: The fan speeding up.
Relay module pop-up in the home automation simulation: K2, K3 and K4 on
Figure: The relay module: three on.
Raspberry Pi home automation Proteus simulation: the fan timer ran out, the porch off by schedule
Figure: The timer ran out.
Fan pop-up in the home automation simulation: no 12 V, the fan coasting down
Figure: The fan coasting.
Laptop WiFi Monitor in the home automation simulation: every load, who switched it and its energy
Figure: The dashboard: the evening so far.

ALL OFF

Finally ALL OFF from the laptop at 44.57 s switched off the one load still on, the bedroom light. The dashboard summed up the evening: the living room 5 s on, the bedroom 30 s, the fan 20 s and the porch 20 s, 0.117 Wh in all, estimated from the rated powers.

Laptop WiFi Monitor in the home automation simulation: ALL OFF, 0.117 Wh used
Figure: ALL OFF.
Raspberry Pi home automation Proteus simulation at the end: every load off
Figure: The end: all off.
Relay module pop-up in the home automation simulation: all four relays released again
Figure: The relay module: all released.

The Pi's terminal shows every switching with the home clock and who did it:

Raspberry Pi 5 terminal in the home automation simulation: every switching, the schedule and the timer
Figure: The program's log.

Download the Project Files

Click the button below to download Raspberry-Pi-5-Home-Automation-Relays-Proteus-Simulation-v1.0.zip (about 14.4 MB, without the source code of the models):

Raspberry Pi 5 Home Automation with Relays - Proteus Simulation V1.0
  • Proteus Simulation: Pi-5-Home-Automation.pdsprj, home.py and copies of the models.
  • Python Code: home.py, the program.
  • Proteus Library Files: the LIB files of every part.
  • Proteus Model Files: the DLL models and the TEPPi folder (the Raspberry Pi runtime).
  • README.txt: the installation steps, the properties, the test results and the links to every library post.

No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.

How to Install the Project Files

  1. Close Proteus and extract the whole zip file, for example to your Desktop.
  2. Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
  3. Copy everything in Proteus Model Files, the DLL files and the TEPPi folder, into the MODELS folder.
  4. Start Proteus and open the project from the Proteus Simulation folder.

Changing the Code

home.py is plain Python; edit it in any editor and keep it next to the project. The loads, their pins and powers, the schedule and the timer are near the top. On a real Raspberry Pi 5, install python3-lgpio, set CLOCK_X to 1 and the clock to the real time, and run it with python3 home.py.

Ideas to Extend the Project

  • Use the real clock and several schedules a day, editable from the dashboard.
  • Add a PIR sensor: the porch light on for a minute when someone walks up.
  • Add a light sensor so the porch light follows the sunset instead of a fixed time.
  • Measure the real energy with a current sensor on the supply instead of estimating it.
  • Add scenes: one link for "movie night" that dims the living room and switches the rest off.
  • Remember the states in a file so a power cut brings the house back as it was.

From Simulation to Real Hardware

  • Mains safety: switching 230 V or 120 V loads needs relays rated for them, fuses, an enclosure and an electrician for the house wiring.
  • The wall switches: wire them to the Pi with low-voltage cable only; the Pi must never see the mains.
  • The relay module: use one with optocouplers and a separate JD-VCC supply so the coils' noise stays away from the Pi.
  • Inductive loads: fans and motors make sparks at the contacts; a snubber or a solid-state relay helps.
  • Security: a dashboard that switches a house needs a password and must never be open to the internet.

Troubleshooting

  • A relay is on when it should be off: the module's inputs are active LOW; write 1 for off.
  • A wall switch does nothing: its line needs its pull-up, and the switch's other side must be on GND.
  • A flip counts twice: the switch bounces; the program sets 50 ms of debounce.
  • The porch light never comes on: the schedule follows the home clock, 17:58 plus 12 times the run time.
  • No dashboard: the program serves on port 8000; open http://192.168.1.51:8000/.

Limitations of the Simulation

  • The relay module, the bulbs, the fan and the switches are our libraries' models, built from their datasheets and the parts' behaviour, not every detail of real parts.
  • 12 V lamps and a 12 V fan stand in for the house's mains loads.
  • The home clock runs 12 times faster than real time so that the schedule fits into a short run.
  • Raspberry Pi is a trademark of Raspberry Pi Ltd; this project is not made or endorsed by Raspberry Pi Ltd or by Labcenter.

So, that was all about the Raspberry Pi 5 Home Automation with Relays - Proteus Simulation. I hope it helps you understand how a relay module switches the loads of a house, how to read wall switches with pull-ups and interrupts, and why two-way switching keeps the automation friendly to everyone at home, all tested in a simulation before you touch any wiring. More popular projects with the ESP32 and the Raspberry Pi 5 are coming. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!


Comments

0

Join the conversation