Zbotic Logo Zbotic Logo
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

  • Shop
  • About Us
  • Contact Us
  • Reseller
  • Blogs
020 69134444
1800 209 0998
[email protected]
Help Desk
Facebook Twitter Instagram Linkedin YouTube
Zbotic Logo Zbotic Logo
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

All departments
  • 3D Print Service
  • 3D Printer
  • Batteries & Chargers
  • Development Boards
  • Drone Parts
  • EBike parts
  • Sensor Modules
  • Electronic Components
  • Electronic Modules
  • IoT and Wireless
  • Mechanical Parts and Workbench Tools
  • Motors & Drivers & Pumps & Actuators
  • DIY and Robot Kits
  • Show more
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
Return to previous page
Home Arduino & Microcontrollers

Arduino Mega Based Home Automation with 8 Relays & OLED

Arduino Mega Based Home Automation with 8 Relays & OLED

March 11, 2026 /Posted byJayesh Jain / 0

Smart home devices from commercial brands cost thousands of rupees and lock you into proprietary ecosystems. With an Arduino Mega and an 8-channel relay module, you can build a fully custom home automation system that controls up to 8 electrical appliances — ceiling fans, tube lights, geysers, air coolers — for under ₹800 in components. You own the hardware, you control the code, and you can extend it any way you want.

This project uses the Arduino Mega 2560 specifically because it has 54 digital I/O pins, 4 hardware UARTs, and 16 analog inputs — far more room to grow than the Uno. By the end of this guide, you’ll have a working 8-relay controller with an OLED status display and manual push-button overrides.

Table of Contents

  1. System Architecture Overview
  2. Components Required
  3. Understanding the 8-Channel Relay Module
  4. Full Wiring Guide
  5. Arduino Mega Code
  6. Adding the OLED Status Display
  7. Expansion Ideas: WiFi, Voice & Timer
  8. Frequently Asked Questions

System Architecture Overview

The system is designed around three layers:

  1. Control Layer (Arduino Mega): The brain. Reads button inputs, processes serial commands, updates the OLED, and drives relay output pins.
  2. Interface Layer: 8 push buttons for manual override (one per relay/appliance) + a 0.96″ OLED showing the on/off status of all 8 devices in real time.
  3. Power Layer: 8-channel relay module switching the actual 230V AC appliances. Fully isolated from the low-voltage control side using optocouplers built into the relay board.

This architecture means the 5V Arduino logic is electrically isolated from the 230V mains side. The relays use optocoupler isolation — the control signal is an LED that optically triggers the relay coil transistor, with no direct electrical connection. This isolation is critical for safety.

A word on safety: This project involves 230V AC mains wiring. If you are not confident working with mains voltage, have a licensed electrician make the 230V connections. The Arduino-side wiring (5V) is completely safe for beginners. Never work on live 230V wiring.

Recommended: Arduino Mega 2560 R3 Board — The Mega 2560 is the ideal controller for this project. With 54 digital I/O pins, you have plenty of room for 8 relay outputs, 8 button inputs, I2C for OLED, and future expansions like WiFi, sensors, and a keypad — all without running out of pins.

Components Required

Component Specification Qty
Arduino Mega 2560 ATmega2560, 5V, 54 digital I/O 1
8-Channel Relay Module 5V coil, 10A/250VAC contacts, optocoupler isolated 1
OLED Display 0.96″ 128×64 I2C SSD1306 1
Push Buttons Momentary tactile switches 8
10kΩ Resistors Pull-down for buttons 8
5V 3A Power Supply Powers Arduino + relay coils 1
Project Enclosure ABS plastic box, min 200×150mm 1
Screw Terminal Strips For mains wiring connections 16
Jumper Wires Male-Male and Female-Male 30+

Optional additions for Phase 2: ESP8266 WiFi module (for phone control), HC-05 Bluetooth module, DS3231 RTC module (for timers), DHT22 temperature sensor.

Understanding the 8-Channel Relay Module

The standard 8-channel relay module used in Indian hobbyist projects has some important characteristics to understand:

Active LOW vs Active HIGH: Most 5V relay modules on the market are active LOW — the relay energizes (turns on) when the input pin is driven LOW (0V), and de-energizes when the pin is HIGH (5V). This is because the module uses an NPN transistor + optocoupler design. Our code accounts for this with inverted logic.

Coil current draw: Each relay coil draws about 60–80mA. With 8 relays potentially all active simultaneously, that’s up to 640mA just for the coils. This exceeds what the Arduino’s 5V regulator can supply safely (max 500mA for on-board regulator). Always power the relay module from an external 5V supply. Connect the JD-VCC and VCC pins appropriately:

  • If using the module’s built-in optocoupler isolation: Connect JD-VCC to external 5V. Connect VCC to Arduino 5V. Do NOT bridge JD-VCC and VCC.
  • If not using isolation (not recommended for mains control): Bridge JD-VCC and VCC, connect both to a single 5V supply.

Contact ratings: The relay contacts are typically rated 10A at 250V AC or 10A at 30V DC. Standard home appliances in India (fans, lights, TV up to 150W) are well within this rating. Do not use these relays to switch motors above 5A without snubber circuits and arc suppression.

Recommended: 12V Modbus RTU 4-Channel Relay Module with RS485 for Arduino — For industrial-grade home automation with RS485 communication, this Modbus relay module allows you to control relays from Raspberry Pi, PLCs, or even over a wired network — perfect for permanent wall installations.

Full Wiring Guide

Arduino Mega to 8-Channel Relay Module (control side):

  • Relay IN1 → Arduino Mega Pin 22
  • Relay IN2 → Arduino Mega Pin 23
  • Relay IN3 → Arduino Mega Pin 24
  • Relay IN4 → Arduino Mega Pin 25
  • Relay IN5 → Arduino Mega Pin 26
  • Relay IN6 → Arduino Mega Pin 27
  • Relay IN7 → Arduino Mega Pin 28
  • Relay IN8 → Arduino Mega Pin 29
  • Relay VCC → Arduino Mega 5V pin
  • Relay GND → Arduino Mega GND
  • Relay JD-VCC → External 5V supply (separate from Arduino 5V)

Push Buttons (8 buttons, one per appliance):

  • Button 1: One side → Pin 30, other side → GND (use internal pull-up via INPUT_PULLUP)
  • Button 2: Pin 31, Button 3: Pin 32, … Button 8: Pin 37

OLED Display (I2C):

  • OLED SDA → Arduino Mega Pin 20 (SDA)
  • OLED SCL → Arduino Mega Pin 21 (SCL)
  • OLED VCC → 3.3V or 5V (check your module)
  • OLED GND → GND

230V AC wiring (have an electrician do this part): For each channel, the Neutral wire goes directly to the appliance. The Live wire goes into the relay’s COM terminal. The relay’s NO (Normally Open) terminal connects to the appliance Live. When the relay closes, the appliance receives power.

Arduino Mega Code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Relay pins (Active LOW on most modules)
const int relayPins[8] = {22, 23, 24, 25, 26, 27, 28, 29};

// Button pins
const int buttonPins[8] = {30, 31, 32, 33, 34, 35, 36, 37};

// Appliance names
const char* applianceNames[8] = {
  "Light 1", "Light 2", "Fan 1  ",
  "Fan 2  ", "Geyser ", "AC     ",
  "TV     ", "Pump   "
};

bool relayState[8] = {false, false, false, false, false, false, false, false};
bool lastButtonState[8];
unsigned long lastDebounceTime[8] = {0};
const unsigned long debounceDelay = 50;

void setup() {
  Serial.begin(9600);
  Serial.println("Home Automation Ready. Type R1-R8 ON/OFF");

  for (int i = 0; i < 8; i++) {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], HIGH);  // Active LOW: HIGH = relay off
    pinMode(buttonPins[i], INPUT_PULLUP);
    lastButtonState[i] = HIGH;
  }

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("OLED init failed");
    for (;;);
  }
  updateOLED();
}

void loop() {
  // Handle push buttons with debounce
  for (int i = 0; i < 8; i++) {
    bool reading = digitalRead(buttonPins[i]);
    if (reading != lastButtonState[i]) {
      lastDebounceTime[i] = millis();
    }
    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
      if (reading == LOW && lastButtonState[i] == HIGH) {
        // Button pressed — toggle relay
        toggleRelay(i);
      }
    }
    lastButtonState[i] = reading;
  }

  // Handle serial commands: e.g., "R1 ON", "R3 OFF", "ALL OFF"
  if (Serial.available()) {
    String cmd = Serial.readStringUntil('n');
    cmd.trim();
    cmd.toUpperCase();
    processCommand(cmd);
  }
}

void toggleRelay(int index) {
  relayState[index] = !relayState[index];
  digitalWrite(relayPins[index], relayState[index] ? LOW : HIGH);
  updateOLED();
  Serial.print(applianceNames[index]);
  Serial.println(relayState[index] ? " → ON" : " → OFF");
}

void setRelay(int index, bool state) {
  relayState[index] = state;
  digitalWrite(relayPins[index], state ? LOW : HIGH);
  updateOLED();
}

void processCommand(String cmd) {
  if (cmd == "ALL OFF") {
    for (int i = 0; i < 8; i++) setRelay(i, false);
    Serial.println("All appliances OFF");
    return;
  }
  if (cmd == "ALL ON") {
    for (int i = 0; i < 8; i++) setRelay(i, true);
    Serial.println("All appliances ON");
    return;
  }
  // Parse "R1 ON", "R5 OFF" etc
  if (cmd.startsWith("R") && cmd.length() >= 4) {
    int relayNum = cmd.substring(1, 2).toInt();
    String stateStr = cmd.substring(3);
    if (relayNum >= 1 && relayNum <= 8) {
      bool state = (stateStr == "ON");
      setRelay(relayNum - 1, state);
      Serial.print("R"); Serial.print(relayNum);
      Serial.println(state ? " = ON" : " = OFF");
    }
  }
}

void updateOLED() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println(" Home Automation v1.0");
  display.drawLine(0, 9, 127, 9, SSD1306_WHITE);

  for (int i = 0; i < 8; i++) {
    int col = (i < 4) ? 0 : 64;
    int row = 12 + (i % 4) * 13;
    display.setCursor(col, row);
    display.print(applianceNames[i]);
    display.setCursor(col + 46, row);
    if (relayState[i]) {
      display.fillRect(col + 44, row - 1, 18, 9, SSD1306_WHITE);
      display.setTextColor(SSD1306_BLACK);
      display.print(" ON");
      display.setTextColor(SSD1306_WHITE);
    } else {
      display.print("OFF");
    }
  }
  display.display();
}

Adding the OLED Status Display

The OLED display shows a live 2-column layout with all 8 appliance names and their ON/OFF status. Key design decisions in the display code:

  • Inverse video for ON state: Active appliances show “ON” with a filled white rectangle and black text — immediately visible at a glance even in bright rooms.
  • 2-column layout: 4 appliances per column fits all 8 on the 64px-tall screen without scrolling.
  • Header line: A horizontal separator below the title gives it a polished UI look.

The updateOLED() function is called any time a relay state changes — whether from a button press or a serial command. This keeps the display always in sync with the actual hardware state.

Recommended: 2.4″ Touch Screen TFT Display Shield for Arduino Uno/Mega — Upgrade your home automation panel with this touch TFT shield. Replace the push buttons with a full graphical touchscreen interface showing room names, icons, and status — directly on the Mega.

Expansion Ideas: WiFi, Voice & Timer

The real power of an Arduino Mega-based system is how easily it can be extended:

1. ESP8266 WiFi control
Connect an ESP8266 (NodeMCU or ESP-01) to Serial1 (pins 18/19 on the Mega). Flash the ESP8266 with a simple HTTP server sketch. Send relay commands from a web browser or a custom Android app. No cloud dependency, works on your local WiFi.

2. Voice control via Alexa or Google Home
Use the SinricPro or Espalexa library on the ESP8266. Register your relays as virtual switches in the Alexa app. Say “Alexa, turn on Fan 1” and the ESP sends a serial command to the Mega. Full voice control in under 2 hours.

3. Timer-based scheduling with DS3231 RTC
Add a DS3231 real-time clock module (I2C, using pins 20/21). Program schedules like “turn on Geyser at 6:00 AM, turn off at 6:30 AM”. The RTC keeps accurate time even through power failures, making it ideal for geysers, garden pumps, and security lights.

4. Temperature-based fan control
Add a DHT22 or LM35 temperature sensor. Automatically turn on the fan when room temperature exceeds 30°C and turn it off when it drops below 26°C. Display current temperature on the OLED alongside relay status.

5. PIR motion sensor automation
Add a PIR sensor to auto-control corridor and bathroom lights. Lights turn on when motion is detected and turn off after 60 seconds of no motion. Saves electricity and convenience.

6. IR remote control
Add a TSOP1738 IR receiver and use any old TV remote to control your appliances. Map each button to toggle a specific relay. Perfect for controlling bedroom appliances without getting out of bed.

Recommended: Arduino Starter Kit with 170 Pages Project Book — If you’re new to Arduino and want to learn the fundamentals before tackling home automation, this official Arduino Starter Kit includes 15 guided projects covering sensors, displays, and actuators with a comprehensive project book.

Frequently Asked Questions

Can I use Arduino Uno instead of Mega for this project?

You can control 8 relays with an Uno, but you’ll run out of I/O pins very quickly once you add buttons, OLED, and any expansion modules. The Uno has only 14 digital pins (2 used by Serial, 2 by I2C = 10 usable). For 8 relays + 8 buttons = 16 pins minimum, the Uno is insufficient without adding an I/O expander (like PCF8574 over I2C). The Mega is the right choice for this project — it has room to grow to 16 relays, a keypad, multiple sensors, and WiFi.

Is it safe to use these relay modules for 230V AC appliances in India?

The relay modules themselves are safe if used correctly — the relay contacts are rated for 10A at 250V AC. The key safety rules are: (1) Always use an enclosure — never leave live mains wiring exposed. (2) Use proper gauge wire — minimum 1.5mm² for circuits up to 6A. (3) Add a main fuse on the live input. (4) Ensure the JD-VCC isolation is maintained — never bridge JD-VCC to VCC when using mains voltages. (5) Have a licensed electrician complete the 230V connections if you’re not trained in mains wiring.

How do I add WiFi control without replacing the Mega?

The simplest approach is to add an ESP8266 module (like ESP-01 or a NodeMCU) connected to the Mega’s Serial1 port (pins 18 TX1 and 19 RX1). Program the ESP8266 to act as a WiFi-to-serial bridge: it receives commands from your phone/browser over HTTP/MQTT and forwards them as text to the Mega’s serial port. The Mega’s existing processCommand() function handles everything without changes.

What happens to my appliances during a power cut?

When power returns, the Arduino restarts from scratch. By default, all relay states are initialised to OFF (relays de-energized) in setup(). This is generally the safe behaviour — appliances don’t unexpectedly switch on. If you want appliances to restore their last state after a power cut, save the state array to the Arduino’s EEPROM before each change and read it back in setup().

Can I control more than 8 appliances?

Yes. Add a second 8-channel relay module to use pins 38–45 for relays 9–16, and pins 46–53 for buttons 9–16. The Arduino Mega can comfortably handle 16 relays + 16 buttons + OLED + serial — all without any additional hardware. For even more channels, daisy-chain relay boards via I2C using PCF8574 I/O expanders (8 relay channels per expander, address-selectable for up to 8 boards = 64 channels).

Build your smart home with Arduino! Get the Arduino Mega 2560, relay modules, OLED displays, and all components at Zbotic’s Arduino & Microcontrollers store. Genuine boards, fast delivery across India, and helpful tech support.

Tags: arduino mega, arduino projects, home automation, oled display, relay module, smart home DIY
Share Post
  • Facebook
  • Linkedin
  • Whatsapp
Raspberry Pi Zero 2 W Projects...
blog raspberry pi zero 2 w projects 10 best lightweight builds 595162
blog arduino multitasking guide millis instead of delay 595174
Arduino Multitasking Guide: mi...

Related posts

Svg%3E
Read more

Arduino Batch Programming: Flash Multiple Boards Quickly

April 1, 2026 0
Table of Contents Introduction Components and Hardware Setup Wiring Diagram and Connections Complete Code with Explanation Customization and Improvements Troubleshooting... Continue reading
Svg%3E
Read more

Arduino Based Radar System with Ultrasonic Sensor

April 1, 2026 0
Table of Contents Introduction Components and Hardware Setup Wiring Diagram and Connections Complete Code with Explanation Customization and Improvements Troubleshooting... Continue reading
Svg%3E
Read more

Arduino Automatic Plant Monitor: Sunlight, Moisture, Temperature

April 1, 2026 0
Table of Contents Introduction Components and Hardware Setup Wiring Diagram and Connections Complete Code with Explanation Customization and Improvements Troubleshooting... Continue reading
Svg%3E
Read more

Arduino Lie Detector: GSR Sensor Polygraph Project

April 1, 2026 0
Table of Contents Introduction Components and Hardware Setup Wiring Diagram and Connections Complete Code with Explanation Customization and Improvements Troubleshooting... Continue reading
Svg%3E
Read more

Arduino Metal Detector: Build a Treasure Finder

April 1, 2026 0
Table of Contents Introduction Components and Hardware Setup Wiring Diagram and Connections Complete Code with Explanation Customization and Improvements Troubleshooting... Continue reading

Add comment Cancel reply

Your email address will not be published. Required fields are marked

Facebook Twitter Instagram Pinterest Linkedin Youtube

Get the latest deals and more.

Download on Google Play Download on the App Store

Call us: 020 69134444 / 1800 209 0998

Monday - Saturday 09:30 AM - 06:00 PM
For Technical Supports Email: [email protected]
For Sales / Enquiries Email: [email protected]

  • My Account

    • Cart

    • Wishlist

    • Checkout

    • My Orders

    • Track Order

    • My Account

  • Information

    • FAQs

    • Blogs

    • Career

    • About Us

    • Contact Us

    • Payment Options

  • Policies

    • Privacy Policy

    • Terms & Conditions

    • GST Input Tax Credit

    • Shipping Return Policy

    • E-Waste Collection Points

    • Our Sitemap

© Zbotic.in is registered trademark of Moxie Supply Pvt Ltd – All Rights Reserved
Login
Use Phone Number
Use Email Address
Not a member yet? Register Now
Reset Password
Use Phone Number
Use Email Address
Register
Already a member? Login Now