RTC 모듈을 사용한 Arduino 디지털 시계

문제를 제거하기 위해 도구를 사용해보십시오





이 포스트에서는 RTC 또는 Real Time Clock 모듈을 사용하여 디지털 시계를 구성 할 것입니다. 'RTC'모듈이 무엇인지, Arduino와 인터페이스하는 방법 및 기능을 이해하게됩니다.

으로:



RTC 모듈은 현재 시간을 정확하게 추적하는 회로입니다. 배터리 백업 시스템이 내장되어있어 두 가지 기능을 수행하며, 마이크로 컨트롤러 및 마이크로 프로세서와 통신하여 현재 시간을 제공하고 정전시 시간 유지를위한 백업 회로 역할을합니다.

시간이 가제트의 중요한 기능인 모든 전자 장치에서 RTC를 찾을 수 있습니다.



예를 들어, 우리의 컴퓨터 나 노트북은 전원이 꺼 지거나 배터리가 제거 된 후에도 시간을 유지합니다. 모든 컴퓨터의 마더 보드에서 RTC 회로에 전원을 공급하는 CMOS 배터리를 찾을 수 있습니다.

이 프로젝트에서 사용할 비슷한 종류의 회로입니다.

RTC 모듈은 모든 전자 상거래 사이트 및 지역 전자 프로젝트 상점에서 찾을 수있는 저렴한 장치입니다.

일반적인 RTC 모듈 DS1307의 그림 :

대부분의 RTC 모듈은 구매시 배터리 (CR2032)와 함께 제공됩니다. 크기와 모델이 다를 수 있으며, 위 그림은 귀하에게 동일하지 않을 수 있습니다. 그러나 모델 번호가 DS1307인지 확인하십시오. 이 게시물에 작성된 코드는 DS1307 과만 호환됩니다.

이제 RTC에 대해 알게되었습니다. 이제 디지털 시계 디자인으로 넘어가겠습니다. 이 프로젝트를 진행하기 전에 다음 링크에서 라이브러리를 다운로드하고 IDE에 설치해야합니다.

• DS1307RTC.h

링크 : github.com/PaulStoffregen/DS1307RTC

• TimeLib.h

링크 : github.com/PaulStoffregen/Time

최신 버전을 사용하는 경우 다른 두 라이브러리가 Arduino IDE에 사전 설치되었을 것입니다.

• LiquidCrystal.h

• Wire.h

회로 :

arduino와 LCD 디스플레이 간의 회로 연결은 표준이며 다른 LCD 기반 프로젝트에서도 유사한 연결을 찾을 수 있습니다. 유일한 추가 구성 요소는 RTC입니다.

프로토 타입 중 와이어 혼잡을 줄이기 위해 RTC를 arduino의 아날로그 핀에 직접 삽입 할 수 있습니다. SCl, SDA, Vcc 및 GND를 수 헤더 핀으로 납땜하고 프로토 타입에 표시된대로 A2에서 A5 핀에 삽입합니다.

저자의 프로토 타입 :

Arduino에 RTC를 올바르게 삽입하는 방법 :

RTC의 핀 위치가 다르고 위 그림과 같이 복제 할 수없는 경우 항상 연결에 와이어를 사용할 수 있습니다. 이제 하드웨어 설정이 완료되었습니다. 프로젝트의 소프트웨어 부분으로 이동하겠습니다.

시간 설정 방법 :

RTC 모듈이 프로그래밍되면 arduino에서 제거 되어도 시간이 유지됩니다. 배터리는 최소 2 년 이상 지속되어야합니다.

다음 프로그램이 RTC에서 시간을 설정하는 시간을 조정하는 버튼이 없습니다. 시간은 코드를 컴파일하는 동안 컴퓨터의 시간과 자동으로 동기화되므로 프로그램을 업로드하기 전에 컴퓨터가 올바른 시간으로 설정되어 있는지 확인하십시오.
RTC가 연결된 시간을 설정하려면이 'SetTime'코드를 업로드하십시오.

#include  #include  #include  int P=A3 //Assign power pins for RTC int N=A2 const char *monthName[12] = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' } tmElements_t tm void setup() { pinMode(P,OUTPUT) pinMode(N,OUTPUT) digitalWrite(P,HIGH) digitalWrite(N,LOW) bool parse=false bool config=false // get the date and time the compiler was run if (getDate(__DATE__) && getTime(__TIME__)) { parse = true // and configure the RTC with this info if (RTC.write(tm)) { config = true } } Serial.begin(9600) while (!Serial)  // wait for Arduino Serial Monitor delay(200) if (parse && config) { Serial.print('DS1307 configured Time=') Serial.print(__TIME__) Serial.print(', Date=') Serial.println(__DATE__) } else if (parse) { Serial.println('DS1307 Communication Error :-{') Serial.println('Please check your circuitry') } else { Serial.print('Could not parse info from the compiler, Time='') Serial.print(__TIME__) Serial.print('', Date='') Serial.print(__DATE__) Serial.println(''') } } void loop() { } bool getTime(const char *str) { int Hour, Min, Sec if (sscanf(str, '%d:%d:%d', &Hour, &Min, &Sec) != 3) return false tm.Hour = Hour tm.Minute = Min tm.Second = Sec return true } bool getDate(const char *str) { char Month[12] int Day, Year uint8_t monthIndex if (sscanf(str, '%s %d %d', Month, &Day, &Year) != 3) return false for (monthIndex = 0 monthIndex < 12 monthIndex++) { if (strcmp(Month, monthName[monthIndex]) == 0) break } if (monthIndex >= 12) return false tm.Day = Day tm.Month = monthIndex + 1 tm.Year = CalendarYrToTm(Year) return true } 

이 코드가 업로드되면 직렬 모니터를 열면 시간이 설정되었음을 알리는 성공 메시지가 나타납니다.

이것은 RTC와 arduino 간의 연결이 정확하고 시간이 설정되었음을 나타냅니다.

이제 LCD에 시간을 표시하기 위해 다음 코드를 업로드하십시오.

//------------Program Developed by R.Girish-------// #include  #include  #include  #include  LiquidCrystal lcd(12, 11, 5, 4, 3, 2) int P=A3 int N=A2 void setup() { lcd.begin(16,2) pinMode(P,OUTPUT) pinMode(N,OUTPUT) digitalWrite(P,HIGH) digitalWrite(N,LOW) } void loop() { tmElements_t tm lcd.clear() if (RTC.read(tm)) { if(tm.Hour>=12) { lcd.setCursor(14,0) lcd.print('PM') } if(tm.Hour<12) { lcd.setCursor(14,0) lcd.print('AM') } lcd.setCursor(0,0) lcd.print('TIME:') if(tm.Hour>12) //24Hrs to 12 Hrs conversion// { if(tm.Hour==13) lcd.print('01') if(tm.Hour==14) lcd.print('02') if(tm.Hour==15) lcd.print('03') if(tm.Hour==16) lcd.print('04') if(tm.Hour==17) lcd.print('05') if(tm.Hour==18) lcd.print('06') if(tm.Hour==19) lcd.print('07') if(tm.Hour==20) lcd.print('08') if(tm.Hour==21) lcd.print('09') if(tm.Hour==22) lcd.print('10') if(tm.Hour==23) lcd.print('11') } else { lcd.print(tm.Hour) } lcd.print(':') lcd.print(tm.Minute) lcd.print(':') lcd.print(tm.Second) lcd.setCursor(0,1) lcd.print('DATE:') lcd.print(tm.Day) lcd.print('/') lcd.print(tm.Month) lcd.print('/') lcd.print(tmYearToCalendar(tm.Year)) } else { if (RTC.chipPresent()) { lcd.setCursor(0,0) lcd.print('RTC stopped!!!') lcd.setCursor(0,1) lcd.print('Run SetTime code') } else { lcd.clear() lcd.setCursor(0,0) lcd.print('Read error!') lcd.setCursor(0,1) lcd.print('Check circuitry!') } delay(500) } delay(500) } //------------Program Developed by R.Girish-------// 

이 작업이 완료되면 시간과 날짜가 LCD에 표시되고 실행되는 것을 볼 수 있습니다.

노트 : 'SetTime'코드는 DS1307RTC의 예제 코드에서 수정되어 RTC 모듈의 와이어 연결을 최적화합니다. 원본 코드를 업로드해도 시간이 설정되지 않습니다.

Arduino를 사용한 디지털 알람 시계

위에서 우리는 RTC 모듈을 사용하여 기본 Arduino 클록을 구축하는 방법을 배웠으며, 다음 섹션에서는 Arduino를 사용하여 디지털 알람 클록 회로로 업그레이드 할 수있는 방법을 조사합니다.

자명종이 필요없는 분도 있고, 자연스럽게 일어나는 분도 있고, 자명종이 몇 번 울린 후 일어나시는 분도 있고, 스누즈 버튼을 여러 번 눌러 대학에 가거나 늦게 일하시는 분도 있습니다. 몇 가지 변명.

제안 된 재미있는 작은 알람 시계 프로젝트는 아침 기상 동안 게으름으로 문제에 직면 할 수 있습니다. 대부분의 알람 시계에는 스누즈 버튼이 있으며 사용자가 응답하지 않을 경우 미리 정해진 알람 차단 시간이 있습니다.

이 알람 시계는 지연 버튼 (스누즈 버튼)없이 설계되었으며 사용자가 버튼을 누를 때까지 알람이 꺼지지 않습니다.

이 시계는 12 시간 형식의 시간과 DD / MM / YYYY 형식의 날짜를 표시 할 수 있습니다.

시간과 날짜는 16 x 2 LCD 디스플레이에 표시됩니다. RTC 또는 실시간 클록 시간 모듈은 시간 추적을 처리하고 오랜 정전 후에도 정확한 시간을 유지할 수 있습니다.

5 개의 버튼이 제공되며 누가 그 기능에 대해 간략히 설명하겠습니다. Arduino 프로젝트의 두뇌는 원하는 모델을 선택할 수 있으며, 크기가 작기 때문에 Arduino pro mini 또는 Arduino nano를 권장합니다.

이제 회로도를 살펴 보겠습니다.

위의 회로도는 Arduino가 연결을 표시하고 10K 전위차계를 회전하여 디스플레이 대비를 조정하는 것입니다.

다음은 나머지 회로입니다.

회로는 9V 500mA 벽면 어댑터에 전원을 공급할 수 있습니다.

5 개의 버튼의 기능 :

S1-알람을 중지하는 데 사용됩니다 (리셋 버튼이기도 함).
S2-알람 설정에 사용됩니다. S2를 길게 누르면 알람 설정 메뉴로 이동합니다.
S3-시간을 늘리는 데 사용됩니다.
S4 – 분을 증가시키는 데 사용됩니다.
S5 – 경보 상태를 전환하는 데 사용됩니다. 오른쪽 하단의 LCD 디스플레이에“*”가 있으면 알람이 ON이고,“*”가 없으면 알람 통계가 OFF입니다.

알람 설정 방법에 대한 자세한 내용은 기사 하단에 설명되어 있습니다.

아래 라이브러리 파일을 다운로드하십시오.

Link1 : github.com/PaulStoffregen/DS1307RTC
Link2 : github.com/PaulStoffregen/Time

이제 RTC 모듈에 시간을 설정해야합니다. 시간은 PC에서 RTC 모듈로 동기화됩니다.

아래 코드를 업로드하여 시간을 설정하고 직렬 모니터를 엽니 다.

//------------------------------------------------// #include  #include  #include  const char *monthName[12] = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' } tmElements_t tm void setup() { bool parse=false bool config=false // get the date and time the compiler was run if (getDate(__DATE__) && getTime(__TIME__)) { parse = true // and configure the RTC with this info if (RTC.write(tm)) { config = true } } Serial.begin(9600) while (!Serial)  // wait for Arduino Serial Monitor delay(200) if (parse && config) { Serial.print('DS1307 configured Time=') Serial.print(__TIME__) Serial.print(', Date=') Serial.println(__DATE__) } else if (parse) { Serial.println('DS1307 Communication Error :-{') Serial.println('Please check your circuitry') } else { Serial.print('Could not parse info from the compiler, Time='') Serial.print(__TIME__) Serial.print('', Date='') Serial.print(__DATE__) Serial.println(''') } } void loop() { } bool getTime(const char *str) { int Hour, Min, Sec if (sscanf(str, '%d:%d:%d', &Hour, &Min, &Sec) != 3) return false tm.Hour = Hour tm.Minute = Min tm.Second = Sec return true } bool getDate(const char *str) { char Month[12] int Day, Year uint8_t monthIndex if (sscanf(str, '%s %d %d', Month, &Day, &Year) != 3) return false for (monthIndex = 0 monthIndex < 12 monthIndex++) { if (strcmp(Month, monthName[monthIndex]) == 0) break } if (monthIndex >= 12) return false tm.Day = Day tm.Month = monthIndex + 1 tm.Year = CalendarYrToTm(Year) return true } //----------------------------------------// 

이제 시간을 RTC로 성공적으로 설정했습니다.
다음으로 다음 기본 코드를 업로드해야합니다.

//------------Program Developed by R.Girish-------// #include  #include  #include  #include  #include  const int rs = 7 const int en = 6 const int d4 = 5 const int d5 = 4 const int d6 = 3 const int d7 = 2 const int buzzer = 8 boolean alarm = false boolean outloop = true const int setAlarm = A0 const int Hrs = A1 const int Min = A2 const int ok = A3 const int HrsADD = 0 const int MinADD = 1 const int ALsave = 2 int HrsVal = 0 int MinVal = 0 int H = 0 int M = 0 int S = 0 int i = 0 int j = 0 int k = 0 LiquidCrystal lcd(rs, en, d4, d5, d6, d7) void setup() { Serial.begin(9600) lcd.begin(16, 2) pinMode(buzzer, OUTPUT) pinMode(setAlarm, INPUT) pinMode(Hrs, INPUT) pinMode(Min, INPUT) pinMode(ok, INPUT) digitalWrite(setAlarm, HIGH) digitalWrite(Hrs, HIGH) digitalWrite(Min, HIGH) digitalWrite(ok, HIGH) } void loop() { tmElements_t tm lcd.clear() if (EEPROM.read(ALsave) == false) { lcd.setCursor(15, 1) lcd.print('') } if (EEPROM.read(ALsave) == true) { lcd.setCursor(15, 1) lcd.print(F('*')) } if (RTC.read(tm)) { if (tm.Hour >= 12) { lcd.setCursor(14, 0) lcd.print('PM') } if (tm.Hour < 12) { lcd.setCursor(14, 0) lcd.print('AM') } lcd.setCursor(0, 0) lcd.print('TIME:') H = tm.Hour if (tm.Hour > 12) { if (tm.Hour == 13) { lcd.print('01') } if (tm.Hour == 14) { lcd.print('02') } if (tm.Hour == 15) { lcd.print('03') } if (tm.Hour == 16) { lcd.print('04') } if (tm.Hour == 17) { lcd.print('05') } if (tm.Hour == 18) { lcd.print('06') } if (tm.Hour == 19) { lcd.print('07') } if (tm.Hour == 20) { lcd.print('08') } if (tm.Hour == 21) { lcd.print('09') } if (tm.Hour == 22) { lcd.print('10') } if (tm.Hour == 23) { lcd.print('11') } } else { lcd.print(tm.Hour) } M = tm.Minute S = tm.Second lcd.print(':') lcd.print(tm.Minute) lcd.print(':') lcd.print(tm.Second) lcd.setCursor(0, 1) lcd.print('DATE:') lcd.print(tm.Day) lcd.print('/') lcd.print(tm.Month) lcd.print('/') lcd.print(tmYearToCalendar(tm.Year)) } else { if (RTC.chipPresent()) { lcd.setCursor(0, 0) lcd.print('RTC stopped!!!') lcd.setCursor(0, 1) lcd.print('Run SetTime code') } else { lcd.clear() lcd.setCursor(0, 0) lcd.print('Read error!') lcd.setCursor(0, 1) lcd.print('Check circuitry!') } } if (digitalRead(setAlarm) == LOW) { setALARM() } if (H == EEPROM.read(HrsADD) && M == EEPROM.read(MinADD) && S == 0) { if (EEPROM.read(ALsave) == true) { sound() } } if (digitalRead(ok) == LOW) { if (EEPROM.read(ALsave) == true) { EEPROM.write(ALsave, 0) alarm = false delay(1000) return } if (EEPROM.read(ALsave) == false) { EEPROM.write(ALsave, 1) alarm = true delay(1000) return } } delay(1000) } void setALARM() { HrsVal = EEPROM.read(HrsADD) MinVal = EEPROM.read(MinADD) lcd.clear() lcd.setCursor(0, 0) lcd.print(F('>>>>SET ALARM<<<')) lcd.setCursor(0, 1) lcd.print(F('Hrs:')) lcd.print(EEPROM.read(HrsADD)) lcd.print(F(' Min:')) lcd.print(EEPROM.read(MinADD)) delay(600) while (outloop) { if (HrsVal > 23) { HrsVal = 0 lcd.clear() lcd.setCursor(0, 0) lcd.print(F('>>>>SET ALARM<<<')) lcd.setCursor(0, 1) lcd.print(F('Hrs:')) lcd.print(HrsVal) lcd.print(F(' Min:')) lcd.print(MinVal) } if (MinVal > 59) { MinVal = 0 lcd.clear() lcd.setCursor(0, 0) lcd.print(F('>>>>SET ALARM<<<')) lcd.setCursor(0, 1) lcd.print(F('Hrs:')) lcd.print(HrsVal) lcd.print(F(' Min:')) lcd.print(MinVal) } if (digitalRead(Hrs) == LOW) { HrsVal = HrsVal + 1 lcd.clear() lcd.setCursor(0, 0) lcd.print(F('>>>>SET ALARM<<<')) lcd.setCursor(0, 1) lcd.print(F('Hrs:')) lcd.print(HrsVal) lcd.print(F(' Min:')) lcd.print(MinVal) delay(250) } if (digitalRead(Min) == LOW) { MinVal = MinVal + 1 lcd.clear() lcd.setCursor(0, 0) lcd.print(F('>>>>SET ALARM<<<')) lcd.setCursor(0, 1) lcd.print(F('Hrs:')) lcd.print(HrsVal) lcd.print(F(' Min:')) lcd.print(MinVal) delay(250) } if (digitalRead(setAlarm) == LOW) { EEPROM.write(HrsADD, HrsVal) EEPROM.write(MinADD, MinVal) lcd.clear() lcd.setCursor(0, 0) lcd.print(F('Alarm is Set for')) lcd.setCursor(0, 1) lcd.print(EEPROM.read(HrsADD)) lcd.print(F(':')) lcd.print(EEPROM.read(MinADD)) lcd.print(F(' Hrs')) delay(1000) outloop = false } } outloop = true } void sound() { lcd.clear() lcd.setCursor(0, 0) lcd.print('Wakey Wakey !!!') lcd.setCursor(0, 1) lcd.print('Its Time now.....') for (j = 0 j < 10 j++) { for (i = 0 i < 2  i++) { digitalWrite(buzzer, HIGH) delay(150) digitalWrite(buzzer, LOW) delay(150) } delay(400) } for (k = 0 k < 10 k++) { for (i = 0 i < 4  i++) { digitalWrite(buzzer, HIGH) delay(150) digitalWrite(buzzer, LOW) delay(150) } delay(250) } while (true) { digitalWrite(buzzer, HIGH) delay(150) digitalWrite(buzzer, LOW) delay(150) } } //------------Program Developed by R.Girish-------// 

위 코드를 업로드 한 후 디스플레이에 정확한 시간과 날짜가 표시되어야합니다.

이제 알람을 설정하는 방법을 살펴 보겠습니다.
• 알람 메뉴가 보일 때까지 S2를 길게 누릅니다.
• S3 및 S4를 눌러 각각 시간과 분을 조정합니다.
• 원하는 시간 설정 후 S2를 다시 한 번 누릅니다. '알람이 xx : xx 시간으로 설정되었습니다'라고 표시됩니다.
• 알람이 켜져 있으면 디스플레이에 '*'기호가 표시되고 알람이 꺼져 있으면 '*'기호가 표시되지 않습니다.
• S5를 0.5 초 동안 눌러 알람을 켜거나 끌 수 있습니다. '*'가 사라질 때까지 (다시 표시됨) 길게 누르지 말고 0.5 초만 누르면 알람 상태가 전환됩니다.

중요 사항:

시계에 알람을 설정하는 가장 일반적인 실수는 의도하지 않게 오전 / 오후를 토글하는 것입니다. 이로 인해 원하는 시간에 알람이 울리지 않습니다.

이 문제를 해결하기 위해 제안 된 알람 시계 설정은 24 시간 시계 형식으로 설계되었습니다.

LCD에 표시되는 시간은 오전 / 오후 형식으로 12 시간이지만이 프로젝트에서 알람을 설정할 때는 0 ~ 23 시간 사이에서 24 시간 형식으로 설정해야합니다.

예 : 오후 9시에 알람을 설정하려면 21 시간 0 분을 설정해야합니다. 오전 5시 : 5 시간 0 분 등.

저자의 프로토 타입 :

이 프로젝트가 마음에 드십니까? 이 프로젝트에 대한 질문이 있으시면 언제든지 의견을 남겨 주시면 빠른 답변을 받으실 수 있습니다.

비디오 클립:




이전 : 콤바인 수확기 곡물 탱크를위한 비콘 레벨 표시기 회로 다음 : 고전력 SG3525 순수 사인파 인버터 회로 3 개