Project tutorial
How to Make Smart Home Using Arduino Control Relay Module

How to Make Smart Home Using Arduino Control Relay Module

This relay module can control 5 appliances from Bluetooth, IR remote, manual switch & in Auto mode Temp sensor and LDR can control the relay

  • 1,495 views
  • 0 comments
  • 3 respects

Components and supplies

Necessary tools and machines

Apps and online services

About this project


This smart relay has the following features:
1. Home appliances controlled by mobile Bluetooth
2. Home appliances controlled by TV Remote (Infrared)
3. Home appliances controlled by temperature & Humidity sensor automatically
4. Home appliances controlled by Dark Sensor
5. Display the LIVE temperature & Humidity reading.
6. Home appliances controlled with manual switches
7. Inbuilt Arduino so that the Arduino code can be uploaded to the relay module.

Component List:

1. ATMEGA328P microcontroller
2. HC05 Bluetooth Module
3. DHT11 Sensor
4. OLED Display (128 X 32)
5. 1738 Infrared Receiver
6. PC817 Optocoupler (5 no)
7. BC547 NPN Transistors (5 no)
8. 1N4007 Diodes (5 no)
9. 1N4001 Diode (1 no)
10. LEDs 5mm (6 no)
11. 22pF Capacitors (2 no)
12. 100nF (104) Capacitor (1 no)
13. 100uF Capacitor (1 no)
14. 220-ohm Resistors (10 no) (R6 to R10)
15. 1k Resistors (7 no) (R1 to R5)
16. 10k Resistors (8 no)
17. 2k (1no) & 4.7k (1no) Resistor
18. LDR (1 no)
19. 16MHz Crystal,
20. Push Buttons (8 no)
21. 5V relays (5 no)
22. Jumper (2no), connectors, IC base
23. FTDI 232 USB to Serial interface board or Arduino UNO
24. PCB

Circuit Diagram:


This is the complete circuit diagram for this smart home automation project. I have explained this smart relay module circuit in the tutorial video of this home automation project.

The ATMEGA328P microcontroller is used to control the 5 channel relay module. I have also connected the HC05 Bluetooth module, 1738 IR receiver to control the relays from Bluetooth and Infrared remote. And DHT11 temperature & humidity sensor and LDR to control the relay automatically.
In this circuit, we can use both 5V or 12V relay but we have to change the resistors accordingly as mentioned in the circuit.
I have also mapped all the microcontroller pin that used in this circuit.

Please watch the complete tutorial video where I have explained all the steps.

Different Mode of the Smart Relay Module :


In this Smart home project we can control the relay module in different modes:

1. Bluetooth Mode
2. Infrared Mode
3. Auto Mode
4. Manual Mode

We can easily change the mode with the CMODE and SMODE button fitted on the PCB.

To change the mode:
1. Press the CMODE button.
2. Then press the SMODE button to select the mode.
3. After selecting the mode again press the CMODE button.

Designing the PCB:

As I am going to use the circuit daily, so after testing all the features of the smart relay module on the breadboard, we can start designing the PCB.

You can download the PCB Gerber file of this home automation project from the following link:

https://drive.google.com/uc?export=download&id=180s0bidnq6u6ilYs4vcLQwcjJ2zMrFZP

Solder All the Components

After that solder all the components as per the circuit diagram.

Then connect Atmega328P microcontroller, HC05, and all the sensors.

Program the Microcontroller


1. Connect the USB to Serial interface board (FTDI232).

2. Download the Arduino sketch. (Attached)

3. Select the Arduino UNO board and proper PORT. Then upload the code.

Connect the Home Appliances with Smart Relay


Now, connect the 5 home appliances as per the circuit diagram and please take proper safety precautions while working with high voltage.

Then give the 5Volt DC supply to PCB as shown in the circuit. (I have used my old mobile charger)

Finally, our Smart Relay Module is Ready

Turn on the 110V/230V supply and 5V DC supply.

Now you can control your home appliances in a smart way.

I hope you have liked this home automation project. All the required information for this project shared in detail.

And I will really appreciate it if you share your valuable feedback, Also if you have any query please write in the comment section.

Thank you & Happy Learning

Code

Arduino Code Smart Relay V3Arduino
Code for the Arduino control relay module
//Home Automation using Smart Relay V3 
//with Tempareture & Light controlled Auto Mode (Tech StudyCell)
//youtube.com/techstudycell

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <IRremote.h>

const float maxTemp = 28.2;
const float minTemp = 27.5;
const int ldrOn = 800;
const int ldrOff = 400;

//Define PIN constant
const int relay_1 = 2;
const int relay_2 = 3;
const int relay_3 = 4;
const int relay_4 = 5;
const int relay_5 = 6;

const int mswitch_1 = 7;
const int mswitch_2 = 8;
const int mswitch_3 = 9;
const int mswitch_4 = 10;
const int mswitch_5 = 11;
const int cmode = 12;
const int smode = 13;
int RECV_PIN = A0;

//Define IR receiver and Result Objects
IRrecv irrecv(RECV_PIN);
decode_results results;

//#define LDRPIN A2
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define DHTPIN A1     // Digital pin connected to the DHT sensor 

// Uncomment the type of sensor in use:
#define DHTTYPE    DHT11     // DHT 11

DHT_Unified dht(DHTPIN, DHTTYPE);

int toggleState_1 = 0; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 0; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 0; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 0; //Define integer to remember the toggle state for relay 4
int toggleState_5 = 0; //Define integer to remember the toggle state for relay 5

int switchMode = 0;
int ModeFlag = 0;
int inloop;
float temperature;
float humidity;
String displayText;
int displayDelay;
uint32_t delayMS;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  irrecv.enableIRIn(); // Enable the IR receiver
  
  pinMode(relay_1, OUTPUT);
  pinMode(relay_2, OUTPUT);
  pinMode(relay_3, OUTPUT);
  pinMode(relay_4, OUTPUT);
  pinMode(relay_5, OUTPUT);

  pinMode(smode, INPUT);
  pinMode(cmode, INPUT);

  pinMode(mswitch_1, INPUT);
  pinMode(mswitch_2, INPUT);
  pinMode(mswitch_3, INPUT);
  pinMode(mswitch_4, INPUT);
  pinMode(mswitch_5, INPUT);
 
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Address 0x3C for 128x32
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.clearDisplay(); // Clear display buffer
  // Initialize device.
  dht.begin();
  sensor_t sensor;
  
  // Set delay .
  delayMS = 10;
}

void relayOnOff(int relay){

    switch(relay){
      case 1: 
             if(toggleState_1 == 0){
              digitalWrite(relay_1, HIGH); // turn on relay 1
              toggleState_1 = 1;
              }
             else{
              digitalWrite(relay_1, LOW); // turn off relay 1
              toggleState_1 = 0;
              }
             delay(100);
      break;
      case 2: 
             if(toggleState_2 == 0){
              digitalWrite(relay_2, HIGH); // turn on relay 2
              toggleState_2 = 1;
              }
             else{
              digitalWrite(relay_2, LOW); // turn off relay 2
              toggleState_2 = 0;
              }
             delay(100);
      break;
      case 3: 
             if(toggleState_3 == 0){
              digitalWrite(relay_3, HIGH); // turn on relay 3
              toggleState_3 = 1;
              }else{
              digitalWrite(relay_3, LOW); // turn off relay 3
              toggleState_3 = 0;
              }
             delay(100);
      break;
      case 4: 
             if(toggleState_4 == 0){
              digitalWrite(relay_4, HIGH); // turn on relay 4
              toggleState_4 = 1;
              }
             else{
              digitalWrite(relay_4, LOW); // turn off relay 4
              toggleState_4 = 0;
              }
             delay(100);
      break;
      case 5: 
             if(toggleState_5 == 0){
              digitalWrite(relay_5, HIGH); // turn on relay 5
              toggleState_5 = 1;
              }
             else{
              digitalWrite(relay_5, LOW); // turn off relay 5
              toggleState_5 = 0;
              }
             delay(100);
      break;      
      default : break;      
      }
  
}
String modeDecode(int count){
  if (count == 0){
    return " Manual Mode ";
  }
  else if (count == 1){
    return " Bluetooth Mode ";
  }
  else if (count == 2){
    return " Infrared Mode ";
  }
  else if (count == 3){
    return " Auto Mode ";
  }
}

void loop() {
  
  if (digitalRead(cmode) == HIGH){
    display.clearDisplay();
    display.setCursor(5,2);
    display.print("Set Mode ");
    display.display();
    delay(2000);
    Serial.println("inloop = 0");
    while(1){
      if (digitalRead(smode) == HIGH){  
        delay(300);      
        if (switchMode < 3){
          switchMode= switchMode + 1;
          Serial.println(switchMode);
        }
        else{
          switchMode =0;
          Serial.println(switchMode);
        }
        displayText= modeDecode(switchMode);
        display.clearDisplay();
        display.setCursor(5,2);
        display.print("Set Mode: ");
        display.println();
        display.drawLine(0,18, display.width()-1,18, WHITE);
        display.setCursor(15,24);
        display.print(displayText);     
        display.display();
      }
      if(digitalRead(cmode) == HIGH){
        delay(1000);
        Serial.println("inloop = 1  ");
        Serial.println(switchMode);
        display.clearDisplay();
        displayDelay = 500;
        break;
      }
    } 
  }     
  else{

  if (displayDelay == 500) {    
      displayDelay = 0;
      display.clearDisplay();
      // Get temperature event and print its value.
      sensors_event_t event;
      dht.temperature().getEvent(&event);
      if (isnan(event.temperature)) {
        Serial.println(F("Error reading temperature!"));
      }
      else {        
        display.setCursor(8,2);
        display.print(event.temperature);
        display.print(" C    ");     
        display.display();
        temperature = event.temperature;
      }    
      // Get humidity event and print its value.
      dht.humidity().getEvent(&event);
      if (isnan(event.relative_humidity)) {
        Serial.println(F("Error reading humidity!"));
      }
      else {
        display.print(event.relative_humidity);
        display.print(" %");     
        display.display();
        display.drawLine(0,18, display.width()-1,18, WHITE);
        displayText= modeDecode(switchMode);
        display.setCursor(15,24);
        display.print(displayText);   
        display.display();
        Serial.print(switchMode);
      }
  }
  displayDelay ++;
  if (switchMode == 0){  // Manual Mode
    Serial.print(F("Manual mode: "));
    if (digitalRead(mswitch_1) == HIGH){
      delay(200);
      relayOnOff(1);      
    }
    else if (digitalRead(mswitch_2) == HIGH){
      delay(200);
      relayOnOff(2);
    }
    else if (digitalRead(mswitch_3) == HIGH){
      delay(200);
      relayOnOff(3);
    }
    else if (digitalRead(mswitch_4) == HIGH){
      delay(200);
      relayOnOff(4);
    }
    else if (digitalRead(mswitch_5) == HIGH){
      delay(200);
      relayOnOff(5);
    }
  }
  else if (switchMode == 1){   //Bluetooth Mode
    if(Serial.available()>0)
   {     
      char data= Serial.read(); // reading the data received from the bluetooth module
      switch(data)
      {
        case 'Z': relayOnOff(1);break; // when Z is pressed on the app Turn on Relay 1
        case 'z': relayOnOff(1);break; // when z is pressed on the app Turn off Relay 1
        case 'Y': relayOnOff(2);break; // when Y is pressed on the app Turn on Relay 2
        case 'y': relayOnOff(2);break; // when y is pressed on the app Turn off Relay 2
        case 'W': relayOnOff(3);break; // when W is pressed on the app Turn on Relay 3
        case 'w': relayOnOff(3);break; // when w is pressed on the app Turn off Relay 3
        case 'V': relayOnOff(4);break; // when V is pressed on the app Turn on Relay 4
        case 'v': relayOnOff(4);break; // when v is pressed on the app Turn off Relay 4
        case 'U': relayOnOff(5);break; // when U is pressed on the app Turn on Relay 5
        case 'u': relayOnOff(5);break; // when u is pressed on the app Turn off Relay 5
        default : break;
      }
      Serial.println(data);
   }
   delay(100);
  }
  else if (switchMode == 2){  // Infrared Mode
    Serial.println("IR MODE");
      if (irrecv.decode(&results)) {
        switch(results.value){
          case 0x80BF49B6: 
                    relayOnOff(1);
          break;
          case 0x80BFC936: 
                    relayOnOff(2);
          break;
          case 0x80BF33CC: 
                    relayOnOff(3);
          break;
          case 0x80BF718E: 
                    relayOnOff(4);
          break;
          case 0x80BFF10E: 
                    relayOnOff(5);
          break;
          default : break;      
          }    
    irrecv.resume(); // Receive the next value
    }
  }
  else if (switchMode == 3){  //Auto Mode
    Serial.println("Auto Mode");
    int sensorVal = analogRead(A2);
  if (sensorVal >= ldrOn){
      if(toggleState_3 == 0){
        digitalWrite(relay_3, HIGH); // turn on relay 3
        toggleState_3 = 1;
      }
      else if(toggleState_4 == 0){
        digitalWrite(relay_4, HIGH); // turn on relay 4
        toggleState_4 = 1;
      }
  }
  else if (sensorVal < ldrOff){
    if(toggleState_3 == 1){
        digitalWrite(relay_3, LOW); // turn on relay 3
        toggleState_3 = 0;
      }
     else if(toggleState_4 == 1){
        digitalWrite(relay_4, LOW); // turn on relay 4
        toggleState_4 = 0;
      }
  }
    
    if (temperature >= maxTemp){
      if(toggleState_1 == 0){
        digitalWrite(relay_1, HIGH); // turn on relay 1
        toggleState_1 = 1;
      }
      else if(toggleState_2 == 0){
        digitalWrite(relay_2, HIGH); // turn on relay 2
        toggleState_2 = 1;
      }
  }
  else if (temperature < minTemp){
    if(toggleState_1 == 1){
        digitalWrite(relay_1, LOW); // turn on relay 1
        toggleState_1 = 0;
      }
    else if(toggleState_2 == 1){
        digitalWrite(relay_2, LOW); // turn on relay 1
        toggleState_2 = 0;
      }
    }
   }
  }
}

Schematics

Arduino Controlled Smart Relay V3 Circuit
Arduino Controlled Smart Relay V3 Circuit diagram
Circuit3 e oh7qmemalh

Comments

Similar projects you might like

SMART CUBE: A New Way to Control Your Home

Project tutorial by Alberto Sartori

  • 17,361 views
  • 8 comments
  • 35 respects

Grovey sLocks - Access Control Through A Smart Door Lock

Project tutorial by gerrikoio

  • 2,420 views
  • 1 comment
  • 8 respects

Control a Lamp with MKR Relay Proto Shield

Project tutorial by David Escobar

  • 2,654 views
  • 0 comments
  • 1 respect

Control Arduino Uno Using ESP8266 WiFi Module and Blynk App

Project tutorial by Adithya TG

  • 63,866 views
  • 18 comments
  • 59 respects

Zoned Climate Control with MediaTek's LinkIt™ Smart 7688

Project tutorial by BuddyC

  • 9,353 views
  • 5 comments
  • 23 respects

Arduino Temperature Control

Project tutorial by Team pandhoit

  • 41,045 views
  • 13 comments
  • 52 respects
Add projectSign up / Login