How to Build a Simple Home Automation System with Arduino

Learn how to build a basic home automation system using Arduino. Control lights, fans, and appliances with relays and a smartphone app.

ROBOTICSSCIENCE THEORYGADGETSTECHNOLOGY

3/23/20265 min read

Home automation — controlling lights, fans, appliances, and security systems from your phone or by schedule — used to require expensive proprietary systems. Today, with an Arduino (or ESP32), a few relay modules, and some basic programming, you can build your own smart home devices for under $20.

This guide walks you through building a practical home automation system step by step: from the fundamental components to a working circuit that lets you control household appliances remotely via a smartphone or automatically based on sensors.

What Is Home Automation?

Home automation refers to the automatic or remote control of home systems and appliances — lighting, climate control, security, entertainment, and more. A home automation system typically combines sensors (to detect conditions), controllers (to make decisions), and actuators (to take action).

In a maker context, Arduino-based home automation bridges the gap between commercial smart home systems and fully custom solutions — giving you complete control over what your system does, at a fraction of the commercial cost.

Components You Will Need

  • Arduino Uno, Nano, or ESP32 (ESP32 recommended for Wi-Fi control)

  • Relay module (1-channel, 4-channel, or 8-channel depending on how many devices you want to control)

  • DHT22 temperature and humidity sensor

  • PIR motion sensor

  • Push buttons and tactile switches

  • 16x2 LCD with I2C module (optional but useful for local status display)

  • HC-05 Bluetooth module OR HC-06 (for Bluetooth control) — or use ESP32 with built-in BT

  • 5V and 12V power supplies

  • Jumper wires, breadboard or PCB

Safety note: This guide controls appliances via relay modules. All mains-voltage (110V/220V) connections must be made with fully insulated wiring, and the relay module must be rated for the load. If you are not confident with mains electricity, use 12V DC appliances (LED strips, small fans, pumps) instead — equally useful and completely safe.

Understanding Relay Modules

A relay is an electrically operated switch. A small signal from your Arduino (typically 3.3V or 5V) energises an electromagnet inside the relay, which mechanically closes or opens a set of high-voltage/high-current contacts. This allows a low-power microcontroller to control mains-powered devices safely.

Relay modules for Arduino come in 1, 2, 4, and 8-channel versions. Each channel controls one device. The module includes a transistor driver so the Arduino pin only needs to sink a few milliamps of current, and an opto-isolator that provides electrical isolation between the Arduino's low-voltage circuitry and the high-voltage relay contacts.

Most relay modules are active-low: setting the Arduino pin to LOW activates the relay (turns the appliance on), and HIGH deactivates it. This can be counter-intuitive at first — just remember to invert your logic.

Project 1: Manual Switch Control

The simplest version of home automation: push buttons connected to Arduino inputs control relay outputs. Press button 1 to toggle light 1 on or off; press button 2 for light 2, and so on.

Wiring

Connect each relay module input pin to an Arduino digital output pin (e.g. relay 1 to pin 4, relay 2 to pin 5). Connect each push button between an Arduino digital input pin and GND, using INPUT_PULLUP mode (the Arduino's internal pull-up resistor handles the HIGH state; pressing the button pulls the pin LOW).

Code Logic

In setup(), set relay pins as OUTPUT and button pins as INPUT_PULLUP. In loop(), read each button pin. If a button is pressed (reads LOW), toggle the corresponding relay pin state and add a short debounce delay to prevent multiple triggers from a single press.

Project 2: Automatic Control with Sensors

Upgrade your system to respond automatically to environmental conditions. A PIR sensor triggers a relay when motion is detected (automatic corridor lighting). A DHT22 sensor triggers a relay to activate a fan when temperature exceeds a threshold.

PIR Motion-Activated Light

Connect the PIR sensor output to a digital input pin. When the PIR output goes HIGH (motion detected), activate the relay. After a set timeout (e.g. 30 seconds of no motion), deactivate it. Use a variable to track the last motion time and millis() for non-blocking timing.

Temperature-Controlled Fan

Read the DHT22 temperature every 5 seconds. If temperature exceeds your threshold (e.g. 28 degrees C), activate the relay connected to a fan. When temperature drops below the threshold minus a hysteresis value (e.g. 26 degrees C), deactivate the relay. The hysteresis prevents rapid on-off cycling near the threshold.

Project 3: Bluetooth Smartphone Control

Add an HC-05 Bluetooth module to control your relay system from a smartphone. This is a significant upgrade in convenience and does not require any internet connection or router.

Hardware Setup

Connect the HC-05 TX pin to Arduino RX (pin 0) and HC-05 RX to Arduino TX (pin 1) via a voltage divider (the HC-05 RX is 3.3V tolerant but the voltage divider is good practice). Alternatively, use the SoftwareSerial library to use pins 10 and 11 as a software serial port, leaving the hardware serial free for USB debugging.

Arduino Code

In loop(), check for incoming serial data from the Bluetooth module. Define a simple command protocol: 'A' turns relay 1 on, 'a' turns relay 1 off, 'B' turns relay 2 on, 'b' turns relay 2 off, and so on. Parse the incoming character and activate the corresponding relay.

Smartphone App

Use a free Bluetooth terminal app (Serial Bluetooth Terminal for Android, or LightBlue for iOS with BLE) or design a custom interface using MIT App Inventor (a free visual app builder). With App Inventor, you can create large toggle buttons for each relay, giving you a proper home control panel on your phone.

Project 4: Wi-Fi Control with ESP32

Replacing the Arduino Uno + HC-05 with an ESP32 adds Wi-Fi control — allowing you to control your home automation system from anywhere in your house or even remotely over the internet.

Web Server Approach

The ESP32 can host a small web server. Your smartphone connects to the same Wi-Fi network and opens a browser to the ESP32's IP address. The web page shows toggle buttons for each relay. Tapping a button sends an HTTP request to the ESP32, which activates or deactivates the corresponding relay.

The ESP32 Arduino core includes a WebServer library that makes this straightforward. Store the HTML for your control page as a string in program memory (using PROGMEM or inline), serve it on HTTP GET to '/', and handle relay commands on HTTP GET to '/relay?num=1&state=on'.

MQTT and Blynk Integration

For more sophisticated control, use MQTT protocol with a broker like HiveMQ Cloud or Mosquitto running on a Raspberry Pi. Subscribe your ESP32 to topic 'home/relay/1' and publish 'ON' or 'OFF' from any MQTT client — including home automation platforms like Home Assistant, Node-RED, or openHAB.

Blynk is a popular IoT platform specifically designed for Arduino/ESP32 projects with a drag-and-drop app builder and cloud connectivity. The free tier supports basic control functionality and is ideal for beginners.

Scheduling: Time-Based Automation

True automation means your system acts on schedules, not just manual commands. With the ESP32's Wi-Fi capability, you can use NTP (Network Time Protocol) to synchronise time from the internet, then implement schedules: turn lights on at sunset, turn off all devices at midnight, run the sprinkler at 6 AM.

The ESP32's NTP library makes time synchronisation straightforward. Store schedules in a simple array of {hour, minute, relayNumber, action} structures and check them in a loop every minute.

Safety Considerations

  • Use properly rated relay modules (the relay contacts must be rated for the voltage and current of the load).

  • Ensure all mains-voltage connections are made with properly rated wire and fully insulated connectors.

  • Include a manual override switch so you can control appliances if the Arduino fails.

  • Do not enclose the Arduino and relay module together with mains-voltage wiring in a sealed box without proper ventilation — relays generate heat.

  • Consider using a UPS (uninterruptible power supply) for the Arduino so it survives power cuts without losing state.

Expanding Your System

Once your basic system is working, there are many directions to expand:

  • Add an energy monitor module (PZEM-004T) to track real-time power consumption of each circuit.

  • Add door and window sensors (magnetic reed switches) for a basic security system.

  • Integrate with Amazon Alexa or Google Home using IFTTT webhooks or dedicated libraries.

  • Add a keypad and LCD for a PIN-protected access control system.

Conclusion

Building a home automation system with Arduino or ESP32 is one of the most satisfying maker projects — it produces something genuinely useful that you will interact with every day. The system scales gracefully from a single relay and a button to a sophisticated Wi-Fi-connected, sensor-driven, schedule-aware smart home controller.

Start simple, test thoroughly, and expand incrementally. For complete wiring diagrams, project code, and extension guides, visit the Circuit Diary Projects page. More home automation tutorials are available on the Circuit Diary Blog.