Subhajit Das
Published © GPL3+

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

AdvancedFull instructions provided16 hours2,467
How to Make Smart Home Using Arduino Control Relay Module

Things used in this project

Hardware components

Arduino 101 & Genuino 101
Arduino 101 & Genuino 101
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

PCB Holder, Soldering Iron
PCB Holder, Soldering Iron

Story

Read more

Schematics

Arduino Controlled Smart Relay V3 Circuit

Arduino Controlled Smart Relay V3 Circuit diagram

Code

Arduino Code Smart Relay V3

Arduino
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;
      }
    }
   }
  }
}

Credits

Subhajit Das

Subhajit Das

3 projects • 2 followers
I have studied Electrical Engineering. I do love to make different DIY electronics projects & share it on my YouTube channel Tech StudyCell.

Comments