Introduction: ON/OFF Timer for Smart Home

About: Electronics Component - PCB [Design, Printing, Inserting] - Electronic Programming

One of the problems for an office worker is a very busy schedule, leaving early and coming home late, so they can not turn on lights or other equipment when night falls. Or it could be, they rush to go to the office, so forget to turn off other electronic equipment. Another problem, a company requires timeliness to automatically turn on or off a device.

Here is one solution to reduce these problems and also reduce the risk of human error by creating a tutorial of the timer module to turn on and off electronic equipment.

Step 1: Materials You Need

Step 2: Setup

Connect all the materials with jumper wires as schematic above.

Step 3: Code

First, access LCD with LCD 3 Wires Module.

#include <Wire.h>
#include <LiquidCrystal_SR.h>

LiquidCrystal_SR lcd(13, 11, 12);

// | |
// | -- Clock Pin
// ---- Data/Enable Pin

// Create a set of new characters
byte armsUp[8] = {0b00100,0b01010,0b00100,0b10101,0b01110,0b00100,0b00100,0b01010}; byte armsDown[8] = {0b00100,0b01010,0b00100,0b00100,0b01110,0b10101,0b00100,0b01010};

void setup(){

lcd.begin(16,2); // initialize the lcd

lcd.createChar (0, armsUp); // load character to the LCD

lcd.createChar (1, armsDown); // load character to the LCD

lcd.home (); // go home

lcd.print("LiquidCrystal_SR");

}

void loop(){

// Do a little animation

for(int i = 0; i <= 15; i++) showHappyGuy(i);

for(int i = 15; i >= 0; i--) showHappyGuy(i);

}

void showHappyGuy(int pos){

lcd.setCursor ( pos, 1 ); // go to position

lcd.print(char(random(0,2))); // show one of the two custom characters

delay(150); // wait so it can be seen

lcd.setCursor ( pos, 1 ); // go to position again

lcd.print(" "); // delete character

}

Second, access RTC I2C DS1307 Module as a time input, then display it to LCD.

#include <Wire.h>
#include <LiquidCrystal_SR.h>
#include <RTClib.h>

RTC_DS1307 rtc;
LiquidCrystal_SR lcd(13, 11, 12);
void setup() {

lcd.begin (16, 2);
rtc.begin();
rtc.adjust(DateTime(2014, 1, 21, 15, 59, 50));
}

void loop() {

DateTime now = rtc.now();
int jam = now.hour();
int menit = now.minute();
int detik = now.second();
lcd.setCursor(2, 0);
lcd.print(konversi(jam));
lcd.setCursor(5, 0);
lcd.print(":");
lcd.setCursor(7, 0);
lcd.print(konversi(menit));
lcd.setCursor(10, 0);
lcd.print(":");
lcd.setCursor(12, 0);
lcd.print(konversi(detik));

}

And then, access Relay Module as an ON/OFF switch.

#define relay1 0
#define relay2 1
#define relay3 2
#define relay4 3

void setup() {

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

}

void loop() {

digitalWrite(relay1, LOW);
delay(1000);
digitalWrite(relay1, HIGH);
delay(1000);

}

Access button module.

#include <Wire.h>
#include <LiquidCrystal_SR.h>
#define tombolL 5
#define tombolU 6
#define tombolO 7
#define tombolD 8
#define tombolR 9

LiquidCrystal_SR lcd(13, 11, 12);
void setup() {

pinMode(tombolL, INPUT_PULLUP);
pinMode(tombolU, INPUT_PULLUP);
pinMode(tombolO, INPUT_PULLUP);
pinMode(tombolD, INPUT_PULLUP);
pinMode(tombolR, INPUT_PULLUP);

lcd.begin (16, 2);
}
void loop() {
if(digitalRead(tombolL) == LOW){
lcd.setCursor(0,1);
lcd.print("a");
}
if(digitalRead(tombolU) == LOW){
lcd.setCursor(0,1);
lcd.print("b");
}
if(digitalRead(tombolO) == LOW){
lcd.setCursor(0,1);
lcd.print("c");
}
if(digitalRead(tombolD) == LOW){
lcd.setCursor(0,1);
lcd.print("d");
}
if(digitalRead(tombolR) == LOW){
lcd.setCursor(0,1);
lcd.print("e");
}
}

And, the main program is consists of the program: setting time on, setting time off and setting the current time.

#include <Wire.h>
#include <LiquidCrystal_SR.h>
#include <RTClib.h>
#include <EEPROM.h>
#define relay1 0
#define relay2 1
#define tombolL 5
#define tombolU 6
#define tombolO 7
#define tombolD 8
#define tombolR 9
unsigned char a, b;
RTC_DS1307 rtc;
LiquidCrystal_SR lcd(13, 11, 12);
char lcdjam[16]; void setup() {
pinMode(relay1, OUTPUT);
digitalWrite (relay1, HIGH);
pinMode(relay2, OUTPUT);
digitalWrite (relay2, HIGH);
pinMode(tombolL, INPUT_PULLUP);
pinMode(tombolU, INPUT_PULLUP);
pinMode(tombolO, INPUT_PULLUP);
pinMode(tombolD, INPUT_PULLUP);
pinMode(tombolR, INPUT_PULLUP);

lcd.begin (16, 2);
rtc.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

}
void loop() {

awal:
int jamOn1 = EEPROM.read(2);
int menOn1 = EEPROM.read(3);
int jamOff1 = EEPROM.read(4);
int menOff1 = EEPROM.read(5);
int jamOn2 = EEPROM.read(6);
int menOn2 = EEPROM.read(7);
int jamOff2 = EEPROM.read(8);
int menOff2 = EEPROM.read(9);
DateTime now = rtc.now();
int jam = now.hour();
int menit = now.minute();
int detik = now.second();
lcd.setCursor(2, 0);
lcd.print(konversi(jam));
lcd.setCursor(5, 0);
lcd.print(":");
lcd.setCursor(7, 0);
lcd.print(konversi(menit));
lcd.setCursor(10, 0);
lcd.print(":");
lcd.setCursor(12, 0);
lcd.print(konversi(detik));

if (digitalRead(tombolO) == LOW) {
delay(250);
a = EEPROM.read(0);
b = EEPROM.read(1);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Setting Jam");
delay(2000);
goto setingjam;
}
if (digitalRead(tombolU) == LOW) {

delay(250);
a = EEPROM.read(2);
b = EEPROM.read(3);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set ON Relay1");
delay(2000);
goto setonjam1;
}
if (digitalRead(tombolL) == LOW) {
delay(250);
a = EEPROM.read(4);
b = EEPROM.read(5);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set OFF Relay1");
delay(2000);
goto setofjam1; }

if (digitalRead(tombolR) == LOW) {
delay(250);
a = EEPROM.read(6);
b = EEPROM.read(7);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set ON Relay2");
delay(2000);
goto setonjam2;
}
if (digitalRead(tombolD) == LOW) {
delay(250);
a = EEPROM.read(8);
b = EEPROM.read(9);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set OFF Relay2");
delay(2000);
goto setofjam2;
}
if (jam == jamOn1 && menit == menOn1) {
digitalWrite(relay1, LOW);
lcd.setCursor(0, 1);
lcd.print("Relay1 ON");
delay(3000);
lcd.clear();
}
if (jam == jamOff1 && menit == menOff1) {
digitalWrite(relay1, HIGH);
lcd.setCursor(0, 1);
lcd.print("Relay1 OFF");
delay(3000);
lcd.clear();
}
if (jam == jamOn2 && menit == menOn2) {
digitalWrite(relay2, LOW);
lcd.setCursor(0, 1);
lcd.print("Relay2 ON");
delay(3000);
lcd.clear();
}
if (jam == jamOff2 && menit == menOff2) {
digitalWrite(relay2, HIGH);
lcd.setCursor(0, 1);
lcd.print("Relay2 OFF");
delay(3000);
lcd.clear();
}
goto awal;

setingjam:
lcd.setCursor(1, 1);
sprintf(lcdjam, "%3d :%3d", a, b);
lcd.print(lcdjam);
if (digitalRead(tombolU) == LOW) {
delay(250);
a++;
}
if (digitalRead(tombolD) == LOW) {
delay(250);
a--;
}
if (digitalRead(tombolR) == LOW) {
delay(250);
b++;
}
if (digitalRead(tombolL) == LOW) {
delay(250);
b--;
}
if (a < 0) {
a = 24;
}
if (a > 24) {
a = 0;
}
if (b < 0) {
b = 60;
}
if (b > 60) {
b = 0;
}
if (digitalRead(tombolO) == LOW) {
delay(250);
rtc.adjust(DateTime(2017, 8, 23, a, b, 0));
lcd.clear();
goto awal;
}

EEPROM.write (0, a);
EEPROM.write (1, b);
goto setingjam;

setonjam1:
lcd.setCursor(1, 1);
sprintf(lcdjam, "%3d :%3d", a, b);
lcd.print(lcdjam);
if (digitalRead(tombolU) == LOW) {
delay(250);
a++;
}
if (digitalRead(tombolD) == LOW) {
delay(250);
a--;
}
if (digitalRead(tombolR) == LOW) {
delay(250);
b++;
}
if (digitalRead(tombolL) == LOW) {
delay(250);
b--;
}
if (a < 0) {
a = 24;
}
if (a > 24) {
a = 0;
}
if (b < 0) {
b = 60;
}
if (b > 60) {
b = 0;
}
if (digitalRead(tombolO) == LOW) {
delay(250);
EEPROM.write (2, a);
EEPROM.write (3, b);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Succes");
delay(2000);
lcd.clear();
goto awal;
}
goto setonjam1;

setofjam1:
lcd.setCursor(1, 1);
sprintf(lcdjam, "%3d :%3d", a, b);
lcd.print(lcdjam);
if (digitalRead(tombolU) == LOW) {
delay(250);
a++;
}
if (digitalRead(tombolD) == LOW) {
delay(250);
a--;
}
if (digitalRead(tombolR) == LOW) {
delay(250);
b++;
}
if (digitalRead(tombolL) == LOW) {
delay(250);
b--;
}
if (a < 0) {
a = 24;
}
if (a > 24) {
a = 0;
}
if (b < 0) {
b = 60;
}
if (b > 60) {
b = 0;
}
if (digitalRead(tombolO) == LOW) {
delay(250);
EEPROM.write (4, a);
EEPROM.write (5, b);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Succes");
delay(2000);
lcd.clear();
goto awal;
}
goto setofjam1;

setonjam2:
lcd.setCursor(1, 1);
sprintf(lcdjam, "%3d :%3d", a, b);
lcd.print(lcdjam);
if (digitalRead(tombolU) == LOW) {
delay(250);
a++;
}
if (digitalRead(tombolD) == LOW) {
delay(250);
a--;
}
if (digitalRead(tombolR) == LOW) {
delay(250);
b++;
}
if (digitalRead(tombolL) == LOW) {
delay(250);
b--;
}
if (a < 0) {
a = 24;
}
if (a > 24) {
a = 0;
}
if (b < 0) {
b = 60;
}
if (b > 60) {
b = 0;
}
if (digitalRead(tombolO) == LOW) {
delay(250);
EEPROM.write (6, a);
EEPROM.write (7, b);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Succes");
delay(2000);
lcd.clear();
goto awal;
}
goto setonjam2;

setofjam2:
lcd.setCursor(1, 1);
sprintf(lcdjam, "%3d :%3d", a, b);
lcd.print(lcdjam);
if (digitalRead(tombolU) == LOW) {
delay(250);
a++;
}
if (digitalRead(tombolD) == LOW) {
delay(250);
a--;
}
if (digitalRead(tombolR) == LOW) {
delay(250);
b++;
}
if (digitalRead(tombolL) == LOW) {
delay(250);
b--;
}
if (a < 0) {
a = 24;
}
if (a > 24) {
a = 0;
}
if (b < 0) {
b = 60;
}
if (b > 60) {
b = 0;
}
if (digitalRead(tombolO) == LOW) {
delay(250);
EEPROM.write (8, a);
EEPROM.write (9, b);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Succes");
delay(2000);
lcd.clear();
goto awal;
}
goto setofjam2;

}
String konversi (byte nilai) {
if (nilai < 10) {
return "0" + String(nilai);
}
else {
return String(nilai);
}
}

Step 4: Check the Video to Know More