How to use DS1307 Real Time Clock with Arduino

How to use DS1307 Real Time Clock with Arduino
Most microcontrollers, including the Arduino, have a built-in timer  that can keep track of longer time periods like minutes or days. However this timer only keeps track of time since the Arduino was last powered which means that whenever the power is turned off, the timer is set back to zero.
Another challenge is that if you wanted to set the time on the Arduino using the inbuilt timer You’d have to program in the date and time and you could have it count from that point on. But if it lost power, you’d have to reset the time. This is rather hectic and is where the Real Time Clock (RTC) comes in handy.
A real time clock is basically just like a watch – it runs on a battery and keeps time for you even if you reprogram your microcontroller or disconnect it from USB or a power plug.It can count leap-years and knows how many days are in a month, but it doesn’t take care of Daylight Savings Time (because it changes from place to place)

The DS1307 RTC Module

The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I2C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.

ds1307 rtc module
DS1307 RTC uses I2C communication and has four pins that are used to connect to the Arduino as follows;
  • GND –  This is the ground pin and is connected to Arduino GND.
  • .VCC is power supply and connected to 5V of the Arduino.
  • SCL is the clock pin and is connected to Arduino analog pin A5.
  • SDA is the data pin and is connected to Arduino analog pin A4.
  • The down side of this RTC module is the external 32kHz crystal for time-keeping. This crystal’s oscillation frequency is affected by external temperature which in the long run leads to loss of some minutes in the time keeping process. This problem is solved in a more accurate DS3231 rtc module.
  • DS3231 RTC interfacing with Arduino.
  • Schematic for DS1307 RTC with Arduino

    ds1307 rtc with arduino with lcd

    CODE

    Before writing the code we need to include the RTClib library in the Arduino IDE. This library  can be downloaded from here. Make sure that it is downloaded first and added to the Arduino library database.
    Other important libraries include the Wire.h and LiquidCrystal_I2C.h libraries that are essential for the working of the i2c LCD. To learn more about these libraries check here.
    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>
    #include "RTClib.h"
    RTC_DS1307 rtc;
    LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
    char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    void setup () 
    {
      Serial.begin(9600);
      lcd.begin (16,2);  // initialize the lcd 
      lcd.backlight();//To Power ON the back light
      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("TIME");
        lcd.print(" ");
        lcd.print(now.hour());
        lcd.print(':');
        lcd.print(now.minute());
        lcd.print(':');
        lcd.print(now.second());
        lcd.print("  ");
    
        lcd.setCursor(0, 0);
        lcd.print("DATE");
        lcd.print(" ");
        //lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
        //lcd.print(" ");
        lcd.print(now.day());
        lcd.print('/');
        lcd.print(now.month());
        lcd.print('/');
        lcd.print(now.year());
        lcd.print("  ");   
    }
    

    2 thoughts on “How to use DS1307 Real Time Clock with Arduino

    1. what is mean from “LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);” ?
      because i usually use “LiquidCrystal_I2C lcd(0x27, 16, 2);” , please the explanation ! thank’s.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top