Hey guys, we are back with another new project. In this article, we are going to teach you how the DS1307 RTC module works with Arduino. The modern machines have their own inbuilt clock which can be used to perform various operations. You can check the full article on our website. We are sharing all the details of the project. There are many types of RTC modules available in the market but we are using a DS1307 RTC module.
How Does it Work?RTC stands for real-time clock. You can open the serial monitor screen for viewing the current date and time. You can found the serial monitor on the top right-hand side on your Arduino IDE application. We are using four of its pins, two for power(GND and VCC) and two for data(SDA and SCL). The RTC module is used for remembering the date and the time. It comes with an inbuilt battery which helps it to keep working in the absence of external power.
- Arduino UNO
- USB cable for uploading the code
- DS1307 RTC module
- Breadboard
- Jumper wires
NOTE: This is the code that you have to upload to the Arduino.
// TECHATRONIC.COM
// RTC LIBRARY LINK
// https://github.com/adafruit/RTClib
#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(9600);
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 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, 3, 3, 20, 0, 0));
}
//rtc.adjust(DateTime(2019, 3, 3, 20, 0, 0));
rtc.adjust(DateTime(F(__DATE__), F(__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(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 future
DateTime 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);
}
Also, check more tutorials on Arduino and Raspberry Pi written by us.
HAPPY LEARNING!
Comments
Please log in or sign up to comment.