How to Build a Smart Home Automation System (Step-by-Step)

Switching lights and fans on from your phone is one of the most satisfying first IoT projects, and it teaches the core skills you will reuse in every connected build. This guide walks you through a complete Wi-Fi smart home automation system end to end. If you would rather not hunt for every part separately, Compoden's AI build assistant can read this project and ship the matched kit with sample code, so you spend your evening building instead of sourcing.

What you'll build

A four-channel home automation controller. An ESP32 connects to your home Wi-Fi and switches mains appliances such as lights, a fan, and a charging point through a relay board. You control everything from a mobile app, and physical wall switches keep working in parallel so the rest of the family is not stranded if the Wi-Fi drops.

Parts you need (rough BOM)

  • ESP32 development board
  • 4-channel relay module (5V, opto-isolated)
  • 5V 2A power supply or a stable USB adapter
  • Jumper wires and a small breadboard for testing
  • Screw terminal connectors and insulated wire for mains
  • A project enclosure (plastic, with cable entry)
  • Optional: physical switches for manual override

Mains wiring carries real risk. If you are not confident, keep the relay outputs on low-voltage loads first, or have a qualified electrician do the final connection.

How it works

The ESP32 joins your Wi-Fi and talks to a cloud service such as Blynk or an MQTT broker. When you tap a button in the app, a message reaches the ESP32, which drives the matching relay pin high or low. The relay's mechanical contact then closes the mains circuit to the appliance. Manual switches are wired so they can toggle the same load independently of the microcontroller.

Wiring overview

Power the relay board from 5V and connect its grounds to the ESP32 ground. Wire four ESP32 GPIO pins (for example GPIO 16, 17, 18, 19) to the relay input pins IN1 to IN4. On the output side, the live wire from your mains supply goes to the common (COM) terminal of each relay, and the normally-open (NO) terminal goes out to the appliance. Keep low-voltage and mains wiring physically separated inside the box.

Build steps

  1. Flash a blink test to the ESP32 to confirm the board and USB driver work.
  2. Wire the relay board on the bench and toggle each channel with a test sketch, listening for the click.
  3. Set up your app (Blynk or an MQTT dashboard) and note the auth token.
  4. Load the automation firmware, enter your Wi-Fi name, password, and token.
  5. Confirm each app button switches the correct relay channel.
  6. Power down, move to the enclosure, and wire one mains load at a time, testing as you go.
  7. Add manual override switches in parallel with the relay outputs.
  8. Close the enclosure, label the channels, and mount it safely.

Code outline

The firmware does four things: connect to Wi-Fi, connect to the cloud or broker, map app controls to GPIO pins, and report state back. In pseudocode:

  • Define relay pins as an array and set them as OUTPUT in setup().
  • Call WiFi.begin() with your credentials and wait for a connection.
  • Initialise the Blynk or MQTT client with your token or broker address.
  • In the app callback, read the button value and write it to the matching pin.
  • In loop(), keep the client running and periodically publish current state.

Most relay boards are active-low, so writing LOW turns a channel on. Test and invert your logic if needed.

Troubleshooting

If the ESP32 never connects, check the Wi-Fi name for typos and confirm you are on a 2.4GHz network. If relays click randomly at boot, that is the ESP32's pin state during start-up; choose safe GPIOs and set them off in setup() early. If an appliance never switches, test the relay with a multimeter on continuity before suspecting code. Brownouts usually mean an underpowered supply, so move to a dedicated 5V 2A adapter.

Going further

Add a DHT22 sensor to switch a fan by temperature, schedule lights with the real-time clock, or wire a current sensor to log how much each channel uses. You can also add voice control by linking your MQTT broker to an assistant, or expand to eight channels with a larger relay board.

Build it with Compoden's AI

Describe this project to Compoden's AI build assistant and it matches a ready-to-build kit, including the ESP32, relay board, connectors, and enclosure, then ships it with sample code tuned to the parts in the box. You skip the guesswork of mismatched modules and get a working starting point. Browse related parts at /collections/all.

Ready to control your home from your phone? Tell Compoden's AI what you want to automate and start building this weekend.

FAQ

Is it safe to switch mains appliances with a relay board? Yes, when wired correctly with an opto-isolated relay, proper insulation, and a sealed enclosure. If you are unsure about mains, keep loads low-voltage or have an electrician finish the connection.

Do I need internet for it to work? Cloud apps like Blynk need internet, but a local MQTT broker on your home network lets the system run without the wider internet.

Will my switches still work during a power or Wi-Fi cut? If you wire manual override switches in parallel, the appliances stay controllable even when the ESP32 or Wi-Fi is down.

Back to blog