How to install RTC library in Arduino Library manager
First you have add the library in to Arduino IDE as shown in the figure 5 below. Go the “Sketch” menu and select “include Library” then click “Manage Library”
Select the above menu to install the library
This should open a dialog box as shown in the figure 6 below. Enter “RTClib” in the text box. The RTClib will be shown. Click install button, this will install the “RTClib” in to Arduino IDE.
Select RTC Library
Connecting DS1307 with Arduino
The connection details are show in the below figure 7. Now upload the following code which sets up time and date for RTC device. After setting up the data and time the RTC device will maintain time for long time. Bring up the serial console to check the print statements and also check the date and time printed.
Interfacing RTC with Arduino UNO
Testing RTC Program
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include <Wire.h> #include "RTClib.h" RTC_DS1307 rtc; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; void setup () { while (!Serial); // for Leonardo/Micro/Zero Serial.begin(57600); if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } 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(F(__DATE__), F(__TIME__))); // this code reads date & time from PC // Below line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: // rtc.adjust(DateTime(2019, 10, 5, 3, 0, 0));}
}
voidloop
() {
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(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.
print(
") ");
Serial.
print(now.hour(), DEC);
Serial.
print(
':');
Serial.
print(now.minute(), DEC);
Serial.
print(
':');
Serial.
print(now.second(), DEC);
Serial.
println();
Serial.
print(
" since midnight 1/1/1970 = ");
Serial.
print(now.unixtime());
Serial.
print(
"s = ");
Serial.
print(now.unixtime() /
86400L);
Serial.
println(
"d");
// calculate a date which is 7 days and 30 seconds into the futureDateTime future (now + TimeSpan(
7,
12,
30,
6));
Serial.
print(
" now + 7d + 30s: ");
Serial.
print(future.year(), DEC);
Serial.
print(
'/');
Serial.
print(future.month(), DEC);
Serial.
print(
'/');
Serial.
print(future.day(), DEC);
Serial.
print(
' ');
Serial.
print(future.hour(), DEC);
Serial.
print(
':');
Serial.
print(future.minute(), DEC);
Serial.
print(
':');
Serial.
print(future.second(),DEC);
Serial.
println();
Serial.
println();
delay(
3000);
}
You can set the date and time in two ways. First is to set them on your PC by using the following code:
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
and the second way is to set date and time manually using the following code:
rtc.adjust(DateTime(YEAR, MONTH, DAY, HOUR , MINUTE, SECOND));
You need to set up date and time in your projects just once, and after that, you should delete the related line from your code. Otherwise, date and time will be set any time you turn the system on and it can cause some mistakes. Uploading the code on your Arduino takes a few seconds and it can cause a few seconds delay in your time, compared to real time. So we suggest you to set your time a few seconds earlier before uploading the code.