Arduino Real Time Clock Tutorial Using DS1307

by protocol_indus in Circuits > Arduino

776 Views, 4 Favorites, 0 Comments

Arduino Real Time Clock Tutorial Using DS1307

learn to code youtube thumbnail .png

In the Arduino Real Time Clock Tutorial, we will learn about Real Time Clock (RTC) and how Arduino and Real Time Clock IC DS1307 are interfaced as a time keeping device. If you recall, we have already implemented an Arduino Alarm Clock using RTC DS1307 in an earlier project.

But that project didn’t cover the basics of Real Time Clock or RTC, the specifications of DS1307 RTC IC and how to interface a Real Time Clock like DS1307 or DS3231 with Arduino.


A special library called “RTClib” is used in the programming and it can be downloaded from this link. Make sure that it is downloaded first and added to the Arduino library database.

Supplies

Arduino.jpg
RTC_DS1307_Module.jpg
arduino-compatible-16x2-lcd-display-ld001.jpg
bread.jpg
jump.jpg
  • Arduino UNO 
  • DS1307 RTC Module  
  • 16×2 LCD Display 
  • Breadboard  
  • Connecting wires  
  • Power supply

Schematic Diagram

Arduino-Real-Time-Clock-DS1307-Tutorial-Circuit-3.png

This shows the way of way in which the components are to be wired with arduino

Working of Real Time Clock Module

A simple project where Arduino UNO is interfaced with DS1307 Real Time Clock is implemented here. In this project, we will be programming the DS1307 RTC with the current date and time and see whether it actually keeps that data even if the power supply to Arduino is removed.

In order to upload the data and time into the DS1307 RTC IC, we have used a feature available in the RTClib library, where the Arduino will upload the date and time from the computer while uploading the code.


Programming

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

	#include <Wire.h>
	#include <LiquidCrystal.h>
	#include "RTClib.h"

	


	RTC_DS1307 rtc;

	LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // (rs, e, d4, d5, d6, d7)

	


	char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

	


	void setup () 

	{

	  Serial.begin(9600);

	  lcd.begin(16, 2);

	  

	  if (! rtc.begin()) 

	  {

	    lcd.print("Couldn't find RTC");

	    while (1);

	  }

	


	  if (! rtc.isrunning()) 

	  {

	    lcd.print("RTC is NOT running!");

	  }

	  

	    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time

	    //rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));// to set the time manualy 

	  

	}

	


	void loop () 

	{

	    DateTime now = rtc.now();

	    

	    lcd.setCursor(0, 1);

	    lcd.print(now.hour());

	    lcd.print(':');

	    lcd.print(now.minute());

	    lcd.print(':');

	    lcd.print(now.second());

	    lcd.print("   ");

	


	    lcd.setCursor(0, 0);

	    lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);

	    lcd.print(" ,");

	    lcd.print(now.day());

	    lcd.print('/');

	    lcd.print(now.month());

	    lcd.print('/');

	    lcd.print(now.year());

	   

	}

Downloads

Applications

  • With the help of Arduino Real Time Clock interface, we can implement several project related to data logging, alarms, clock, etc.
  • Since the RTC Module DS1307 is backed with a battery, it will continue to maintain time even in the event of power fail.