DS1307 Real Time Clock RTC With Arduino

In this Tutorial, we will learn about Real Time Clock (RTC) and how Arduino & Real Time Clock IC DS1307 are in put together as a timing device.

Real Time Clock (RTC) is used for monitoring time and maintaining a calendar.
In order to use an RTC, we need to first program it with the current date and time. Once this is done, the RTC registers can be read any time to know the time and date. DS1307 is an RTC which works on I2C protocol. Data from various registers can be read by accessing their addresses for reading using I2C communication.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Things You Need

These are the following things you need for this instructables :

Arduino uno

Ds1307 rtc module

Jumper wires

3.7v coin cell

Step 2: Connections

Please follow the attached schmatics in image section and connect everything According to the schmatics.

Step 3: Coding Part

Programming Arduino to feed RTC with current date and time; and reading the date and time from the RTC.

Here, we will be using DS1307 library by Watterott from GitHub.

Download this library from here. : https://github.com/watterott/Arduino-Libs/archive/...

Extract the library and add the folder named DS1307 to the libraries folder path of Arduino IDE.

Once the library has been added to the Arduino IDE, open the IDE and open the example sketch named Example from the DS1307 library added.

Word Of Caution : In the example sketch, in setup loop, rtc.set() function is used. Pass the current date and time arguments as mentioned to this function. In the example sketch, this statement will be commented. Uncomment it and upload the sketch. Once the sketch is uploaded, uncomment the statement again and upload the sketch. If this is not done, each time the Arduino UNO board resets or is powered on after power off, the date and time you set will be set over and over again and you will not be able to read the exact current time and date.

/*
DS1307 RTC (Real-Time-Clock) Example

Uno A4 (SDA), A5 (SCL) Mega 20 (SDA), 21 (SCL) Leonardo 2 (SDA), 3 (SCL) */

#include "Wire.h"

#include "DS1307.h"

DS1307 rtc;

void setup() { /*init Serial port*/ Serial.begin(9600); while(!Serial); /*wait for serial port to connect - needed for Leonardo only*/

/*init RTC*/ Serial.println("Init RTC...");

/*only set the date+time one time*/ rtc.set(0, 0, 8, 24, 12, 2014); /*08:00:00 24.12.2014 //sec, min, hour, day, month, year*/

/*stop/pause RTC*/ // rtc.stop();

/*start RTC*/ rtc.start(); }

void loop() { uint8_t sec, min, hour, day, month; uint16_t year;

/*get time from RTC*/ rtc.get(&sec, &min, &hour, &day, &month, &year);

/*serial output*/ Serial.print("\nTime: "); Serial.print(hour, DEC); Serial.print(":"); Serial.print(min, DEC); Serial.print(":"); Serial.print(sec, DEC);

Serial.print("\nDate: "); Serial.print(day, DEC); Serial.print("."); Serial.print(month, DEC); Serial.print("."); Serial.print(year, DEC);

/*wait a second*/ delay(1000); }

Copy the above code & upload it to your arduino Board

Step 4: Getting Time

After connecting everything together and uploading the code to your arduino board , open the serial monitor in your arduino ide and then you'll able to get date & time as mine in your serial monitor as you can see i am able to see my Time & date in my serial monitor, for demo output please refer the above image output and have fun adding RTC clock to your project.

Be the First to Share

    Recommendations

    • Instrument Contest

      Instrument Contest
    • Make it Glow Contest

      Make it Glow Contest
    • STEM Contest

      STEM Contest

    Discussions