RTC DS1307 with Arduino

Last Updated on March 16, 2024

arduino rtc ds1307

Every real time applications need one RTC chip. In this tutorial you can get idea and knowledge about RTC DS1307 (Real Time Clock chip) and interfacing with Arduino development board.




The DS1307 RTC IC is 8pin dual inline package chip and it is very simple to use with low cost and easy available. Normally this IC counts date and time perfectly, it continue counting even if the power failure when the battery backup provided. 3V coin cell battery enough to provide individual supply to DS1307 IC.

DS1307 RTC Pinout

ds1307 rtc ic

Block Diagram of DS1307

block diagram rtc DS1307

This RTC IC contains more than seven internal blocks, the first one is oscillator and divider it is connected with standard 32.768 KHz quartz crystal, the internal oscillator circuitry is designed for operation with a crystal having a specified capacitance of 12.5 pF. The power control block have Vcc, Gnd and Vbat pins, here Vcc and Gnd pins provides primary power supply.

When the voltage is applied within normal limits, the device is fully accessible and data can be written and read. When the battery supply connected (Vbat) without primary supply read and writes are inhibited, but the time keeping function continues.

The DS1307 RTC communicates with external world through serial bus interface I²C protocol. SCL (Serial Clock), SDA (Serial Data).

pdf-icon

Datasheet – DS1307

 

RTC module

12708-02

(https://www.sparkfun.com/products/12708)

Circuit Diagram of RTC module

rtc module circuit diagram

This RTC module circuit has few external components like 32.768 KHz quartz crystal element, filter capacitor 0.1uF and pullups resisters 4.7KΩ, this RTC provides I²C address about 0x68. This little breakout that uses the DS1307 to keep track of the current year, month, day as well as the current time. The module comes fully assembled and includes a small CR1225 Lithium coin cell battery that will run the RTC for a minimum of 9 years (17 years typical) without an external 5V power supply. (Sparkfun)

Arduino and RTC DS1307 Hookup

rtc arduino

Connect 5V and Gnd supply form arduino power pins and carefully connect SDA pin of RTC with Arduino analog pin A4 and then SCL pin of RTC with arduino analog pin A5. Now the setup is ready put the bellow arduino code on it.

Arduino CODE for RTC 

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527


void setup(){
  Wire.begin();
  Serial.begin(9600);
  setDateTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){
  printDate();
  delay(1000);
}

void setDateTime(){

  byte second =      45; //0-59
  byte minute =      40; //0-59
  byte hour =        0; //0-23
  byte weekDay =     2; //1-7
  byte monthDay =    1; //1-31
  byte month =       3; //1-12
  byte year  =       11; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start 

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}




This program is designed to display time in serial terminal of arduino IDE, you can edit this to display time on LCD.

3 thoughts on “RTC DS1307 with Arduino

    1. Thank you for your interest 🙂 you can post your prototype here!
      Just photo comment in theorycircuit Facebook page.

Leave a Reply

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