What is RTC (Real Time Clock) ?
It is Similar Clock in Real analog and digital Clock which is continues working until Battery Power is loss.But in Computer and Laptop we shutdown and dissconnect power but here next time we start time is not lost.here the small Real-time clock (RTC) that keeps runs the current time with small Battery Cmos Cell.RTCs are present in almost any electronic device which needs to keep accurate time.
Number of Real Time Clock ICs.
- DS1307 – 64 x 8, Serial, I2C Real-Time Clock.
- DS3234 – Extremely Accurate SPI Bus RTC with Integrated Crystal and SRAM
- DS3231 – Extremely Accurate I2C-Integrated RTC/TCXO/Crystal
- DS1302 – Trickle-Charge Timekeeping Chip
- DS1308 – Low-Current I²C RTC with 56-Byte NV RAM
- PCF8563 – Real-time clock/calendar
- MCP79400 – Real-Time Clock/Calendar with SRAM and Protected EEPROM
RTC Modules online Available in Amazon
[amazon_link asins=’B077N9VWYJ,B00HCB7VYS,B0787G4CS1,B0180GI57A,B01MG8KXO8,B01N133HJL’ template=’ProductCarousel’ store=’diamondrock0a-21′ marketplace=’IN’ link_id=’dc363986-7ac4-11e8-8a4e-41ab9d0b7cdb’]
Making Own RTC
List of Components used in Circuit
- DS1307 RTC chip
- 32.768 Khz generic quartz watch crystal
- 100nF Ceramic Capacitor
- 10K Resistors
- 12mm 3V Lithium Coin Cell (CR2032)
- 12mm Coin Cell Holder
- 5-Pin Male Header
- Circuit Board
Working of RTC
Every RTC use an external 32kHz timing crystal that is used to keep time with low current draw.but some ic we not need crystal.This is the same frequency used in quartz clocks and watches,32khz is equal 215 cycles per second this is best why to use a binary counter which trigger control rigisters update values.there internal memory which maintains seconds, minutes, hours, day, date, month, and year information.The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections of leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. Two programmable time-of-day alarms. To hold memory data a 12mm coin-cell battery for battery backup to avoid data loss. To Read or Set time with Serial interface I2C allow inside ic memory.
Wiring / Connection with Arduino
Programming with Arduino to Set Time Without Library
#include <Wire.h> const int DS1307 = 0x68; // Address of DS1307 see data sheets const char* days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; const char* months[] = {"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December"}; // Initializes all values: byte second = 0; byte minute = 0; byte hour = 0; byte weekday = 0; byte monthday = 0; byte month = 0; byte year = 0; void setup() { Wire.begin(); Serial.begin(9600); delay(2000); // This delay allows the MCU to read the current date and time. Serial.print("The current date and time is: "); printTime(); Serial.println("Please change to newline ending the settings on the lower right of the Serial Monitor"); Serial.println("Would you like to set the date and time now? Y/N"); while (!Serial.available()) delay(10); if (Serial.read() == 'y' || Serial.read() == 'Y'){ // This set of functions allows the user to change the date and time Serial.read(); setTime(); Serial.print("The current date and time is now: "); printTime(); } Serial.println("Thank you."); } // Continuous function for converting bytes to decimals and vice versa void loop() { } byte decToBcd(byte val) { return ((val/10*16) + (val%10));} byte bcdToDec(byte val) { return ((val/16*10) + (val%16));} // This set of codes is allows input of data void setTime() { Serial.print("Please enter the current year, 00-99. - "); year = readByte(); Serial.println(year); Serial.print("Please enter the current month, 1-12. - "); month = readByte(); Serial.println(months[month-1]); Serial.print("Please enter the current day of the month, 1-31. - "); monthday = readByte(); Serial.println(monthday); Serial.println("Please enter the current day of the week, 1-7."); Serial.print("1 Sun | 2 Mon | 3 Tues | 4 Weds | 5 Thu | 6 Fri | 7 Sat - "); weekday = readByte(); Serial.println(days[weekday-1]); Serial.print("Please enter the current hour in 24hr format, 0-23. - "); hour = readByte(); Serial.println(hour); Serial.print("Please enter the current minute, 0-59. - "); minute = readByte(); Serial.println(minute); second = 0; Serial.println("The data has been entered."); // The following codes transmits the data to the RTC Wire.beginTransmission(DS1307); Wire.write(byte(0)); Wire.write(decToBcd(second)); Wire.write(decToBcd(minute)); Wire.write(decToBcd(hour)); Wire.write(decToBcd(weekday)); Wire.write(decToBcd(monthday)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.write(byte(0)); Wire.endTransmission(); // Ends transmission of data } byte readByte() { while (!Serial.available()) delay(10); byte reading = 0; byte incomingByte = Serial.read(); while (incomingByte != '\n') { if (incomingByte >= '0' && incomingByte <= '9') reading = reading * 10 + (incomingByte - '0'); else; incomingByte = Serial.read(); } Serial.flush(); return reading; } void printTime() { char buffer[3]; const char* AMPM = 0; readTime(); Serial.print(days[weekday-1]); Serial.print(" "); Serial.print(months[month-1]); Serial.print(" "); Serial.print(monthday); Serial.print(", 20"); Serial.print(year); Serial.print(" "); if (hour > 12) { hour -= 12; AMPM = " PM"; } else AMPM = " AM"; Serial.print(hour); Serial.print(":"); sprintf(buffer, "%02d", minute); Serial.print(buffer); Serial.println(AMPM); } void readTime() { Wire.beginTransmission(DS1307); Wire.write(byte(0)); Wire.endTransmission(); Wire.requestFrom(DS1307, 7); second = bcdToDec(Wire.read()); minute = bcdToDec(Wire.read()); hour = bcdToDec(Wire.read()); weekday = bcdToDec(Wire.read()); monthday = bcdToDec(Wire.read()); month = bcdToDec(Wire.read()); year = bcdToDec(Wire.read()); }
Result
Programming with Arduino to Read Time Without Library
#include "Wire.h" const char* AMPM; byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; char* days[] = { "","Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; int H; String s, m, d, mth, h; #define DS1307_I2C_ADDRESS 0x68 void setup(){ Wire.begin(); Serial.begin(9600); Serial.println("www.ahirlabs.com"); delay(3000); } void loop(){ rtcgetdate(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); // GET values displaytime(); // Display Time delay(1000); } byte bcdToDec(byte val){ return ( (val/16*10) + (val%16) );} void rtcgetdate(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year){ // Read values from RTC DS1307 Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 7); *second = bcdToDec(Wire.read() & 0x7f); *minute = bcdToDec(Wire.read()); *hour = bcdToDec(Wire.read() & 0x3f); *dayOfWeek = bcdToDec(Wire.read()); *dayOfMonth = bcdToDec(Wire.read()); *month = bcdToDec(Wire.read()); *year = bcdToDec(Wire.read()); } void ampm(){ (hour > 11)?(AMPM="PM"):(AMPM="AM"); } void Convert_Hours(){ if(hour == 12||hour == 0) H=12; else if(hour > 12)H=hour%12; else H=hour; } void displaytime(){ //Here We Get Time ampm(); Convert_Hours(); (H < 10) ? ( h = "0" + String(H)) : ( h = String(H)); // APPLY ZERO + with 1to9 Hours (minute < 10) ? ( m = "0" + String(minute)) : ( m = String(minute) ); // APPLY ZERO + with 1to9 Minutes (second < 10) ? ( s = "0" + String(second)) : ( s = String(second) ); // APPLY ZERO + with 1to9 Seconds (dayOfMonth < 10) ? ( d = "0" + String(dayOfMonth)) : ( d = String(dayOfMonth)); // APPLY ZERO + with 1to9 Day (month < 10) ? ( mth = "0" + String(month)) : ( mth = String(month)); // APPLY ZERO + with 1to9 Month Serial.println(); Serial.println( h + ":" + m + ":" + s + " " + AMPM); // HOURS : MINUTES : SECONDS AM/PM Serial.println( d + ":" + mth + ":" + year + " " + days[dayOfWeek]); //DAY:MONTH:YEAR WEEK }
Result
Programming with Arduino to Set Time With Library
These Programs use a Library : RTClib
#include <Wire.h> #include "RTClib.h" int rtc_sec,rtc_min,rtc_hour,rtc_day,rtc_month,rtc_year; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; //char daysOfTheWeek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; //char months[12][12] = {"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December"}; // Use As Yours requirement char months[12][4] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; RTC_DS1307 rtc; void setup(){ Serial.begin(9600); if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } delay(2000); // This delay allows the MCU to read the current date and time. Serial.print("The current date and time is: "); printTime(); Serial.println("Please change to newline ending the settings on the lower right of the Serial Monitor"); Serial.println("Would you like to set the date and time now? Y/N"); while (!Serial.available()) delay(10); if (Serial.read() == 'y' || Serial.read() == 'Y'){ Serial.read(); setTime(); Serial.print("The current date and time is now: "); printTime(); } Serial.println("Thank you."); } void loop() { printTime(); delay(1000); } //Print Time void printTime(){ DateTime now = rtc.now(); Serial.print(now.day(), DEC); Serial.print('/'); Serial.print(months[now.month()-1]); Serial.print('/'); Serial.print(now.year(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.print(") "); int hr = (now.hour()==23 || now.hour()==12 )? 12 : (now.hour()>12)? now.hour()-12 : now.hour(); Serial.print(hr); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.print((now.hour()>12)? " PM" : " AM"); Serial.println(); } void setTime(){ rtc_min = rtc_hour = rtc_day = rtc_month = rtc_year = 0; Serial.print("Please enter the current year, 00-99. - "); rtc_year = readByte(); rtc_year = 2000+rtc_year; Serial.println(rtc_year); Serial.println("1 - Jan | 2 - Feb | 3 - Mar | 4 - Apr | 5 - May | 6 - Jun "); Serial.println("7 - Jul | 8 - Aug | 9 - Sep | 10 - Oct | 11 - Nov | 12 - Dec "); Serial.print("Please enter the current month,1-12 - "); rtc_month = readByte(); Serial.println(months[rtc_month-1]); Serial.print("Please enter the current day of the month, 1-31. - "); rtc_day = readByte(); Serial.println(rtc_day); Serial.print("Please enter the current hour in 24hr format, 0-23. - "); rtc_hour = readByte(); Serial.println(rtc_hour); Serial.print("Please enter the current minute, 0-59. - "); rtc_min = readByte(); Serial.println(rtc_min); rtc_sec = 0; Serial.println("The data has been entered."); //2014:1:21 at 3:0:0 rtc.adjust(DateTime(rtc_year,rtc_month,rtc_day,rtc_hour,rtc_min, rtc_sec)); } byte readByte() { while (!Serial.available()) delay(10); byte reading = 0; byte incomingByte = Serial.read(); while (incomingByte != '\n') { if (incomingByte >= '0' && incomingByte <= '9') reading = reading * 10 + (incomingByte - '0'); else; incomingByte = Serial.read(); } Serial.flush(); return reading; }
Result
Programming with Arduino to Read Time With Library
#include <Wire.h> #include "RTClib.h" char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; //char daysOfTheWeek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; //char months[12][12] = {"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December"}; // Use As Yours requirement char months[12][4] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; RTC_DS1307 rtc; void setup(){ Serial.begin(9600); if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } delay(2000); // This delay allows the MCU to read the current date and time. } void loop() { printTime(); delay(1000); } //Print Time void printTime(){ DateTime now = rtc.now(); Serial.print((now.day()<10)? "0" : ""); Serial.print(now.day(), DEC); Serial.print('/'); Serial.print(months[now.month()-1]); Serial.print('/'); Serial.print(now.year(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.print(") "); int hr = (now.hour()==23 || now.hour()==12 )? 12 : (now.hour()>12)? now.hour()-12 : now.hour(); Serial.print(hr); Serial.print(':'); Serial.print((now.minute()<10)? "0" : ""); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print((now.second()<10)? "0" : ""); Serial.print(now.second()); Serial.print((now.hour()>12)? " PM" : " AM"); Serial.println(); }
Result
Any Suggestion Please Comment
This is truly useful, thanks.