레이블이 RTC인 게시물을 표시합니다. 모든 게시물 표시
레이블이 RTC인 게시물을 표시합니다. 모든 게시물 표시

2017년 8월 8일 화요일

RTC 모듈로 릴레이 작동시키기

http://cafe.naver.com/diymaker/11 에 사용한 RTC 모듈을 간단하게 릴레이를 작동시키는 타이머로 사용하는 방법입니다.

Blynk 앱으로는 간단하게 되는데 웹으로 지원하는 IoT 사이트에서는 원격으로 타이머처리를 할려니 안되는 곳도 있고 

설정이 복잡하더군요.


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

// PIN definitions
#define RELAY_PIN 2

// FSM states
#define STATE_OFF  0
#define STATE_ON   1

// Timer settings
#define START_TIME  2144
#define END_TIME    2146

// variables
RTC_DS1307 RTC;
int fsm_state;

void setup() {
  
  Serial.begin(57600);
  Serial.println("SimpleTimer running...");
  Serial.println();

  Wire.begin();
  RTC.begin();  

  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
  fsm_state = STATE_OFF;
}

void loop() {

  DateTime now = RTC.now();
  int nowHourMinute = now.hour() * 100 + now.minute();

  // FSM states
  switch(fsm_state) {
    
    case STATE_OFF:
      if(nowHourMinute > START_TIME && nowHourMinute < END_TIME) {
        Serial.print(now.hour(), DEC);
        Serial.print(':');
        Serial.print(now.minute(), DEC);
        Serial.println(", it's time to wake up!");
        digitalWrite(RELAY_PIN, HIGH);
        fsm_state = STATE_ON;
      }
      break;
    
    case STATE_ON:
      if(nowHourMinute > END_TIME) {
        Serial.print(now.hour(), DEC);
        Serial.print(':');
        Serial.print(now.minute(), DEC);        
        Serial.println(", it's time to go to sleep!");
        digitalWrite(RELAY_PIN, LOW);
        fsm_state = STATE_OFF;
      }    
      break;
  }
}


ds1307 RTC 모듈로 시계구현 하기

ds1307 RTC 모듈은 뒤에 코인형 밧데리를 끼워서 시간 진행상태를 유지해주는 모듈입니다.

RTC 라이브러리를 아래링크에서 받아서 아두이노에 설치합니다.

RTC 모듈의 SDA 핀은 아두이노 우노의 A4, SCL 핀은 아두이노 우노의 A5에 연결합니다.


// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
 
#include <Wire.h>
#include "RTClib.h"
 
RTC_DS1307 RTC;
 
void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__)); //현재 시간설정
  }
 
}
 
void loop () {
    DateTime now = RTC.now();
 
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
 
    delay(1000);
}

그러면 아래와 같이 시리얼 모니터에 나옵니다.