Arduino -Turn on / off anything at a specific time. (Trigger a Relay with DS3231 RTC)



In this tutorial we will learn how to turn on / off anything at a specific time. For this we will use DS3231 RTC module and Relay module. Like time switch. Trigger a Relay with DS3231 Real Time Clock.

-----------------------------------------------

Required Hardware

Arduino Board -- https://goo.gl/RcRmi1
DS3231 RTC  -- https://goo.gl/Euk1nh
Relay Module -- https://goo.gl/8gG2k9
Coin Battery -- https://goo.gl/XSTkCi
Breadboard -- https://goo.gl/ExxNWy
Jumper Wires -- https://goo.gl/94xaTi

4 Port USB Hub -- https://goo.gl/U8wou4

-----------------------------------------------

Deals Electronics -- https://goo.gl/eepuZs
Deals Toys & Hobbies -- https://goo.gl/nEc1oi
Deals Cell Phone -- https://goo.gl/f3eScP
Deals Computer & Networking -- https://goo.gl/RWR1tK

-----------------------------------------------

Get the Library -- https://goo.gl/A7ER6U


Get the Code
#include <DS3231.h>

int Relay = 4;

DS3231  rtc(SDA, SCL);
Time t;

const int OnHour = 12;
const int OnMin = 24;
const int OffHour = 12;
const int OffMin = 25;

void setup() {
  Serial.begin(115200);
  rtc.begin();
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);
}

void loop() {
  t = rtc.getTime();
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (1000);
  
  if(t.hour == OnHour && t.min == OnMin){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
}