Intermediatearduino

Arduino Home Automation Hub

Build a centralized home automation hub using Arduino Mega that controls lights, fans, and appliances via web interface.

8-10 hours
$60-90
Sarah Mitchell
Arduino Home Automation Hub

Parts List

  • Arduino Mega 2560
  • 4-Channel Relay Module
  • ESP8266 WiFi Module
  • LEDs (Various Colors)
  • Breadboard
  • Jumper Wires
  • AC Load (Lamps, Fans)

Step-by-Step Instructions

1

Set Up Arduino IDE

Download and install the Arduino IDE from the official website, then add the board manager URL for ESP8266 support. Navigate to Preferences and paste the ESP8266 board manager URL into the Additional Boards Manager URLs field. Install the required libraries including ESP8266WiFi, WebServer, and Servo through the Library Manager.

2

Wire the Relay Module

Connect the IN1-IN4 pins of the 4-channel relay module to Arduino digital pins 22 through 25. Wire the relay module's VCC to the Arduino 5V pin and GND to ground. Each relay channel will control one AC appliance, so run the live wire through the relay's COM and NO terminals for normally-open switching.

3

Integrate ESP8266

Connect the ESP8266 WiFi module to the Arduino's hardware serial pins (TX1/RX1 on pins 18/19). Wire the ESP8266 VCC to 3.3V and CH_PD to 3.3V, and GND to ground. Use the AT command set to configure the ESP8266 as a WiFi station that connects to your home network and listens for incoming HTTP requests.

4

Write the Control Firmware

Write the main Arduino sketch that receives commands from the ESP8266 via serial communication. Parse incoming HTTP request parameters to determine which relay to toggle on or off. Include safety features such as a debounce delay to prevent rapid switching and an auto-off timer that turns off appliances after a configurable duration.

void setup() {
  Serial1.begin(9600);
  for (int i = 22; i <= 25; i++) {
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH);
  }
}

void loop() {
  if (Serial1.available()) {
    String cmd = Serial1.readStringUntil('\n');
    if (cmd.startsWith("RELAY")) {
      int pin = cmd.charAt(5) - '0' + 21;
      bool state = cmd.charAt(7) == '1';
      digitalWrite(pin, state ? LOW : HIGH);
    }
  }
}
5

Build the Web Interface

Create an HTML file with toggle switches for each appliance, styled with CSS for a responsive mobile-friendly layout. Use JavaScript fetch() calls to send HTTP requests to the ESP8266 when a switch is toggled. Host the web interface from the ESP8266's SPIFFS filesystem so the dashboard is accessible from any device on your WiFi network.