Arduino Projects, RTC Automatic Home Appliances Control Using Arduino

RTC Automatic Home Appliances Control Using Arduino

RTC Automatic Home Appliances Control Using Arduino

Introduction

Being able to regulate household appliances is a major comfort in the age of smart homes. Consider having your home’s lights, fans, and other appliances run on autopilot. We’ll look at utilizing an Arduino board and a Real-Time Clock (RTC) module to build an Automatic Home Appliances Control system in this blog article. This project gives your house real-time scheduling for your appliances in addition to adding an automation aspect.

Components Required

Proteus Simulation

Open the simulation file on Proteus 8. Here, an Arduino UNO is used as a Microcontroller. Four push buttons are used for settings. A buzzer is used for alarm and is connected with pin 13 of the Arduino UNO. A 16×2 LCD is used as a display. Four 220 volts loads are used in parallel for testing. They are connected to Arduino by relay circuits. DS3232 is used for real time clock. To run the simulation, first compile the code and copy the hex file address from there. Paste the hex file address in Arduino UNO and then run the simulation.

Circuit Diagram

In this circuit diagram, a 5 volts and 2 ampere adapter is used for power source. The main power source is 220 volts that is for our loads. For loads, we have used a fan, a switch and light bulbs. To operate other part of circuit, power adapter is used, that will provide 5 volts DC input. Four channel relay module is used for four loads. The relay also works on 5 volts. A 16×2 LCD is used as a display. Four push buttons are used and are connected to the Analog pins A0, A1, A2, A3 of the Arduino UNO. The DS3231 timer module is used. Its outputs are also connected to analog pins A4 and A5 of the Arduino UNO. A buzzer is connected with pin 13 for indication.

Arduino IDE Code

#include <DS3231.h>//Memanggil RTC3231 Library
#include <Wire.h>  // i2C Conection Library
#include <LiquidCrystal.h> //Libraries
#include <EEPROM.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Arduino pins to lcd

#define bt_clock  A0
#define bt_up     A1
#define bt_down   A2
#define bt_timer  A3

#define relay1 8
#define relay2 9
#define relay3 10
#define relay4 11

#define buzzer 13

// Init DS3231
DS3231  rtc(SDA, SCL);

// Init a Time-data structure
Time  t; //pencacah string time()

int hh = 0, mm = 0, ss = 0, dd = 0, bb = 0, set_day;
int yy = 0;
String Day = "  ";

int StartHH  = 0, StartMM  = 0, FinishHH  = 0, FinishMM  = 0, setMode = 0, setAlarm = 0, alarmMode=1;
int Start1HH, Start1MM, Finish1HH, Finish1MM;
int Start2HH, Start2MM, Finish2HH, Finish2MM;
int Start3HH, Start3MM, Finish3HH, Finish3MM;
int Start4HH, Start4MM, Finish4HH, Finish4MM;

int timer1, timer2, timer3, timer4;

int stop =0, mode=0, flag=0;

void setup(){
rtc.begin(); // memulai koneksi i2c dengan RTC

pinMode(bt_clock, INPUT_PULLUP);
pinMode(bt_up,    INPUT_PULLUP);
pinMode(bt_down,  INPUT_PULLUP);
pinMode(bt_timer, INPUT_PULLUP);

pinMode(relay1, OUTPUT);digitalWrite(relay1, HIGH);
pinMode(relay2, OUTPUT);digitalWrite(relay2, HIGH);
pinMode(relay3, OUTPUT);digitalWrite(relay3, HIGH);
pinMode(relay4, OUTPUT);digitalWrite(relay4, HIGH);

pinMode(buzzer, OUTPUT);
  
  lcd.begin(16, 2); // Configura lcd numero columnas y filas
  lcd.setCursor(0,0);  //Show "TIME" on the LCD

  lcd.setCursor (0,0);
  lcd.print(" Real Time Clock ");
  lcd.setCursor (0,1);
  lcd.print(" 4 Load  Control ");
  delay (2000);
  lcd.clear();

stop=EEPROM.read(50);  
if(stop==0){  
}else{
mode=1;WriteEeprom ();delay (20);
mode=2;WriteEeprom ();delay (20);
mode=3;WriteEeprom ();delay (20);
mode=4;WriteEeprom ();delay (20);
mode=0;
}
EEPROM.write(50,0); 

ReadEeprom();
//Set RTC Untuk Pertama kali
//rtc.setDOW(2);     // Set Day-of-Week to SUNDAY
//rtc.setTime (00, 9, 50); 
//rtc.setDate(12, 11, 2017);  
}

void loop(){  
t = rtc.getTime();
Day = rtc.getDOWStr(1);

if (setMode == 0){
hh = t.hour,DEC;
mm = t.min,DEC;
ss = t.sec,DEC;
dd = t.date,DEC;
bb = t.mon,DEC;
yy = t.year,DEC;
//if(t.hour>12){hh=t.hour-12;}// for removing 24 houre
//else{hh=t.hour;}
}  

  if(setAlarm==0){
  lcd.setCursor(0,0); 
  lcd.print((hh/10)%10);
  lcd.print(hh % 10); 
  lcd.print(":");
  lcd.print((mm/10)%10);
  lcd.print(mm % 10);
  lcd.print(":");
  lcd.print((ss/10)%10);
  lcd.print(ss % 10); 
  lcd.print("  T:"); 
  lcd.print(rtc.getTemp(),0);
  lcd.write(223); 
  lcd.print("C");
  lcd.print("  "); 

  lcd.setCursor(1,1);
  lcd.print(Day);
  lcd.print(" ");
  lcd.print((dd/10)%10);
  lcd.print(dd % 10); 
  lcd.print("/");
  lcd.print((bb/10)%10);
  lcd.print(bb % 10);
  lcd.print("/"); 
  lcd.print((yy/1000)%10);
  lcd.print((yy/100)%10);
  lcd.print((yy/10)%10);
  lcd.print(yy % 10);
  }


setupClock();
setTimer();
delay (100);
blinking();

//Timer1 ON
if (timer1==1 && alarmMode==1 && hh==Start1HH && mm==Start1MM)  {digitalWrite(relay1, LOW);}
//Timer1 OFF
if (timer1==1 && alarmMode==1 && hh==Finish1HH && mm==Finish1MM){digitalWrite(relay1, HIGH);}

//Timer2 ON
if (timer2==1 && alarmMode==1 && hh==Start2HH && mm==Start2MM)  {digitalWrite(relay2, LOW);}
//Timer2 OFF
if (timer2==1 && alarmMode==1 && hh==Finish2HH && mm==Finish2MM){digitalWrite(relay2, HIGH);}

//Timer3 ON
if (timer3==1 && alarmMode==1 && hh==Start3HH && mm==Start3MM)  {digitalWrite(relay3, LOW);}
//Timer3 OFF
if (timer3==1 && alarmMode==1 && hh==Finish3HH && mm==Finish3MM){digitalWrite(relay3, HIGH);}

//Timer4 ON
if (timer4==1 && alarmMode==1 && hh==Start4HH && mm==Start4MM)  {digitalWrite(relay4, LOW);}
//Timer4 OFF
if (timer4==1 && alarmMode==1 && hh==Finish4HH && mm==Finish4MM){digitalWrite(relay4, HIGH);}

delay (100);
digitalWrite(buzzer, LOW);
}

void blinking (){
//BLINKING SCREEN
//Set Clock
if (setAlarm <2 && setMode == 1){lcd.setCursor(0,0);  lcd.print("  ");}
if (setAlarm <2 && setMode == 2){lcd.setCursor(3,0);  lcd.print("  ");}
if (setAlarm <2 && setMode == 3){lcd.setCursor(6,0);  lcd.print("  ");}
if (setAlarm <2 && setMode == 4){lcd.setCursor(1,1);  lcd.print("   ");}
if (setAlarm <2 && setMode == 5){lcd.setCursor(5,1);  lcd.print("  ");}
if (setAlarm <2 && setMode == 6){lcd.setCursor(8,1);  lcd.print("  ");}
if (setAlarm <2 && setMode == 7){lcd.setCursor(11,1); lcd.print("    "); }

//Set Timer
if (setMode == 0 && setAlarm == 1 && mode==0){lcd.setCursor(2,1); lcd.print("  "); }
if (setMode == 0 && setAlarm == 2 && mode==0){lcd.setCursor(6,1); lcd.print("  "); }
if (setMode == 0 && setAlarm == 3 && mode==0){lcd.setCursor(10,1); lcd.print("  "); }
if (setMode == 0 && setAlarm == 4 && mode==0){lcd.setCursor(13,1); lcd.print("  "); }

if (setMode == 0 && setAlarm == 1 && mode>0){lcd.setCursor(11,0); lcd.print("  "); }
if (setMode == 0 && setAlarm == 2 && mode>0){lcd.setCursor(14,0); lcd.print("  "); }
if (setMode == 0 && setAlarm == 3 && mode>0){lcd.setCursor(11,1); lcd.print("  "); }
if (setMode == 0 && setAlarm == 4 && mode>0){lcd.setCursor(14,1); lcd.print("  "); }
}

//Seting Jam ,Tanggal,Alarm/Timer
void setupClock (void) {
   
    if (setMode == 8){
    lcd.setCursor (0,0);
    lcd.print ("Set Time Finish ");
    lcd.setCursor (0,1);
    lcd.print ("Set Date Finish ");
    delay (1000);
    rtc.setTime (hh, mm, ss);
    rtc.setDate (dd, bb, yy);  
    lcd.clear();
    setMode = 0;
    }
    
 if (setAlarm >0){alarmMode=0;}
    
 if(digitalRead (bt_clock) == 0 && flag==0) {flag=1;
 if(setAlarm>0){WriteEeprom(); setAlarm=1; mode =5;}
 else{setMode = setMode+1;}
 digitalWrite(buzzer, HIGH);
 }
  
 if(digitalRead (bt_timer) == 0 && flag==0){flag=1;
 if(setMode>0){setMode=8;}
  else{
  setAlarm = setAlarm+1;
  if(setAlarm>4){setAlarm=1; 
  WriteEeprom ();
  mode=mode+1;
  ReadEeprom();
  }
  } 
  lcd.clear();
  digitalWrite(buzzer, HIGH);
  } 

    if(setAlarm == 1 && mode==5){
    lcd.setCursor (0,0);
    lcd.print ("Set Timer Finish");
    lcd.setCursor (0,1);
    lcd.print ("-EEPROM Updated-");
    delay (2000); 
    lcd.clear();
    setAlarm=0;
    mode =0;
    alarmMode=1;
    }

if(digitalRead (bt_clock) == 1 && digitalRead (bt_timer) == 1){flag=0;}
  
 if(digitalRead (bt_up) == 0){                          
            if (setAlarm<2 && setMode==1)hh=hh+1; 
            if (setAlarm<2 && setMode==2)mm=mm+1;
            if (setAlarm<2 && setMode==3)ss=ss+1;
            if (setAlarm<2 && setMode==4)set_day=set_day+1;
            if (setAlarm<2 && setMode==5)dd=dd+1;
            if (setAlarm<2 && setMode==6)bb=bb+1;
            if (setAlarm<2 && setMode==7)yy=yy+1;
            //Timer
            if (mode==0 && setMode==0 && setAlarm==1)timer1=1;
            if (mode==0 && setMode==0 && setAlarm==2)timer2=1;
            if (mode==0 && setMode==0 && setAlarm==3)timer3=1;
            if (mode==0 && setMode==0 && setAlarm==4)timer4=1;
             
            if (mode>0 && setMode==0 && setAlarm==1)StartHH=StartHH+1;
            if (mode>0 && setMode==0 && setAlarm==2)StartMM=StartMM+1;
            if (mode>0 && setMode==0 && setAlarm==3)FinishHH=FinishHH+1;
            if (mode>0 && setMode==0 && setAlarm==4)FinishMM=FinishMM+1;

if(hh>23)hh=0;
if(mm>59)mm=0;
if(ss>59)ss=0;
if(set_day>7)set_day=0;
if(dd>31)dd=0;
if(bb>12)bb=0;
if(yy>2030)yy=2000;
if(StartHH>23)StartHH=0;
if(StartMM>59)StartMM=0;
if(FinishHH>23)FinishHH=0;
if(FinishMM>59)FinishMM=0;
rtc.setDOW(set_day);
digitalWrite(buzzer, HIGH);
 }        

if(digitalRead (bt_down) == 0){                                      
            if (setAlarm<2 && setMode==1)hh=hh-1; 
            if (setAlarm<2 && setMode==2)mm=mm-1;
            if (setAlarm<2 && setMode==3)ss=ss-1;
            if (setAlarm<2 && setMode==4)set_day=set_day-1;
            if (setAlarm<2 && setMode==5)dd=dd-1;
            if (setAlarm<2 && setMode==6)bb=bb-1;
            if (setAlarm<2 && setMode==7)yy=yy-1;
            //Timer
            if (mode==0 && setMode==0 && setAlarm==1)timer1=0;
            if (mode==0 && setMode==0 && setAlarm==2)timer2=0;
            if (mode==0 && setMode==0 && setAlarm==3)timer3=0;
            if (mode==0 && setMode==0 && setAlarm==4)timer4=0;
            
            if (mode>0 && setMode==0 && setAlarm==1)StartHH=StartHH-1;
            if (mode>0 && setMode==0 && setAlarm==2)StartMM=StartMM-1;
            if (mode>0 && setMode==0 && setAlarm==3)FinishHH=FinishHH-1;
            if (mode>0 && setMode==0 && setAlarm==4)FinishMM=FinishMM-1;

if(hh<0)hh=23;
if(mm<0)mm=59;
if(ss<0)ss=59;
if(set_day<0)set_day=7;
if(dd<0)dd=31;
if(bb<0)bb=12;
if(yy<0)yy=2030;
if(StartHH<0)StartHH=23;
if(StartMM<0)StartMM=59;
if(FinishHH<0)FinishHH=23;
if(FinishMM<0)FinishMM=59;
rtc.setDOW(set_day);
digitalWrite(buzzer, HIGH); 
 }
 
}

void setTimer (){
//Timer
if (setMode == 0 && setAlarm >0 && mode>0){
  lcd.setCursor (0,0);
  lcd.print("Load ");
  lcd.print(mode);
  lcd.print(" On :");
  lcd.setCursor (11,0);
  lcd.print((StartHH/10)%10);
  lcd.print(StartHH % 10);
  lcd.print(":");
  lcd.print((StartMM/10)%10);
  lcd.print(StartMM % 10);

  lcd.setCursor (0,1);
  lcd.print("Load ");
  lcd.print(mode);
  lcd.print(" Off:");
  lcd.setCursor (11,1);
  lcd.print((FinishHH/10)%10);
  lcd.print(FinishHH % 10);
  lcd.print(":");
  lcd.print((FinishMM/10)%10);
  lcd.print(FinishMM % 10);
 }

if (setMode == 0 && setAlarm >0 && mode==0){
  lcd.setCursor (0,0);
  lcd.print(" L1  L2  L3  L4 ");
  lcd.setCursor (0,1);
    if(timer1==1){lcd.print("  A");}
  else{lcd.print("  D");}
   
    if(timer2==1){lcd.print("   A");}
  else{lcd.print("   D");}
  
    if(timer3==1){lcd.print("   A");}
  else{lcd.print("   D");}
  
    if(timer4==1){lcd.print("   A");}
  else{lcd.print("   D");}
}
}

void ReadEeprom() {
Start1HH=EEPROM.read(11);Start1MM=EEPROM.read(12);Finish1HH=EEPROM.read(13);Finish1MM=EEPROM.read(14);
Start2HH=EEPROM.read(21);Start2MM=EEPROM.read(22);Finish2HH=EEPROM.read(23);Finish2MM=EEPROM.read(24);
Start3HH=EEPROM.read(31);Start3MM=EEPROM.read(32);Finish3HH=EEPROM.read(33);Finish3MM=EEPROM.read(34);
Start4HH=EEPROM.read(41);Start4MM=EEPROM.read(42);Finish4HH=EEPROM.read(43);Finish4MM=EEPROM.read(44);

if(mode==1){StartHH=Start1HH, StartMM=Start1MM, FinishHH=Finish1HH,FinishMM=Finish1MM;}
if(mode==2){StartHH=Start2HH, StartMM=Start2MM, FinishHH=Finish2HH,FinishMM=Finish2MM;}
if(mode==3){StartHH=Start3HH, StartMM=Start3MM, FinishHH=Finish3HH,FinishMM=Finish3MM;}
if(mode==4){StartHH=Start4HH, StartMM=Start4MM, FinishHH=Finish4HH,FinishMM=Finish4MM;}

timer1=EEPROM.read(1);
timer2=EEPROM.read(2);
timer3=EEPROM.read(3);
timer4=EEPROM.read(4);
}

void WriteEeprom() {
if(mode==1){EEPROM.write(11,StartHH);EEPROM.write(12,StartMM);EEPROM.write(13,FinishHH);EEPROM.write(14,FinishMM);}
if(mode==2){EEPROM.write(21,StartHH);EEPROM.write(22,StartMM);EEPROM.write(23,FinishHH);EEPROM.write(24,FinishMM);}
if(mode==3){EEPROM.write(31,StartHH);EEPROM.write(32,StartMM);EEPROM.write(33,FinishHH);EEPROM.write(34,FinishMM);}
if(mode==4){EEPROM.write(41,StartHH);EEPROM.write(42,StartMM);EEPROM.write(43,FinishHH);EEPROM.write(44,FinishMM);}

EEPROM.write(1,timer1);
EEPROM.write(2,timer2);
EEPROM.write(3,timer3);
EEPROM.write(4,timer4);
}

Explanation

DS3231 for Real-Time Clock (RTC) capability, Wire for I2C connection, LiquidCrystal for LCD interface, and EEPROM for reading and writing to non-volatile memory are among the libraries that are first included in the code. Next, pin assignments for the buzzer, relays, and buttons are defined. The LCD display, pin modes, and RTC are all initialized via the setup function. In addition, it configures the RTC with an initial time if it’s the first run and reads a value from EEPROM to ascertain the system’s status (including whether the user has previously set timers or not). The current time is continually read from the RTC by the main loop and shown on the LCD. It controls the pressing of buttons to set alarms, timers, and clocks. In addition, it controls the display’s flashing effect and modifies the timings in response to human input. The LCD screen’s flashing effect, which shows which portion of the time or date is now being set, is controlled by the blinking function.The clock time, date, and timers are configured using the setupClock and setTimer functions. They process input from the user to change these numbers, then show them on the LCD. The reading and writing of configuration data to the EEPROM is handled by two functions, ReadEeprom and WriteEeprom. Because of this, user settings are kept on the device even after power cycles. Using user-defined timers, the code incorporates logic to switch on and off various relays, which represent appliances. It operates the relevant relays by comparing the current time to each timer’s preprogrammed start and finish times.

Hardware Testing

Conclusion

In summary, this Real-Time Clock module-enhanced Arduino-based Automatic Home Appliances Control system provides a flexible and easy-to-use approach to controlling domestic appliances. The project makes exact appliance scheduling possible by connecting the DS3231 RTC with a variety of relays in a smooth manner, automating daily tasks. The user interface makes it easier to set up numerous timers and clock timings thanks to its LCD display and simple button controls. The project’s practicality is increased by the ability of EEPROM to retain user settings over power cycles. This project not only demonstrates Arduino’s potential for home automation, but it also offers a framework for extending and modifying the features of smart homes.

3 responses to “RTC Automatic Home Appliances Control Using Arduino”

  1. PATRICK Avatar
    PATRICK

    Hello jaweria zafar
    Thank you for this code, it’s good for me. I try to put the 4 times alarms with the seconde but i cant write them in eeprom. Can you helpme ?
    Can i sendind you your code that i have modify for find my error.
    Have a good day
    Patrick
    sorry for my inglish

  2. Dennis Wickerham Avatar

    Where can I find the DS3231 file (Ds3231.h sketch file)?
    For the 4 relay RTC BASED AUTOMATIC HOME APPLIANCES CONTROL.

  3. dede hidayat Avatar
    dede hidayat

    After compiling, many errors occur in the Arduino IDE application and what should I fix?

Leave a Reply

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

3 thoughts on “RTC Automatic Home Appliances Control Using Arduino

  1. PATRICK says:

    Hello jaweria zafar
    Thank you for this code, it’s good for me. I try to put the 4 times alarms with the seconde but i cant write them in eeprom. Can you helpme ?
    Can i sendind you your code that i have modify for find my error.
    Have a good day
    Patrick
    sorry for my inglish

  2. Where can I find the DS3231 file (Ds3231.h sketch file)?
    For the 4 relay RTC BASED AUTOMATIC HOME APPLIANCES CONTROL.

  3. dede hidayat says:

    After compiling, many errors occur in the Arduino IDE application and what should I fix?

Leave a Reply

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