Min menu

Pages

Arduino Real Time Clock Tutorial using DS1307

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


Components Required

  • Arduino UNO  
  • DS1307 RTC Module  
  • 16×2 LCD Display
  • Breadboard  
  • Connecting wires  
  • Power supply  

Circuit Design

The design of the Arduino RTC Interface is quite straight forward. Connect the SDA and SCL pins of the DS1307 RTC to the SDA and SCL pins of Arduino i.e. pins A4 and A5.
A 16×2 LCD is connected in order to display the data and time information. The connections are made as per the circuit diagram

Code

// 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());
}
reaction: