Using Arduino with a DS1307 Real Time Clock

by Lewis Loflin

Explanation of program:

On power up or reset the "setup" is executed once, setting up the hardware and writing the time/date to the Ds1307. Then the "loop" section will run over and over. The Ds1307 is read and sent via the serial port to a computer running for example Hyper Terminal. This demonstrates the use of the Wire.h library, serial ports, and the Ds1307.

For the basic structure of an Arduino program see page 7 of Arduino Programming Notebook by Brian Evans

The 1 Hz output from the Ds1307 has been connected to digital pin 2 in order to update the output to once a second. This can be a 10K resistor or an LED-1k resister combination to observe the output from the Ds1307.

Basic I2C setup
Basic I2C setup.

The DS1307 is a low-power clock/calendar with 56 bytes of battery-backed SRAM. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year.

The DS1307 operates as a slave device on the I2C bus. Access is obtained by implementing a START condition and providing a device identification code (0x68) followed by a register address. Subsequent registers can be accessed sequentially. The Ds1307 comes in an 8-pin dip package. The Ds1307 counts in BCD format.

Bit 7 of Register 0 is the clock halt (CH) bit. When this bit is set to 1, the oscillator is disabled. When cleared to 0, the oscillator is enabled. In other words unless this is cleared the clock will not run!

DS1307 pin out
Ds1307 pin out

Only the first 8 bytes (0x00 - 0x07) are used by the clock itself while the other 56 bytes can be used a scratchpad RAM.


Pins 1, 2 Connections for Standard 32.768kHz Quartz Crystal. No capacitor or other parts needed.

Pin 3 VBAT Backup Supply Input for Any Standard 3V Lithium Cell Diodes in series between the battery and the VBAT pin may prevent proper operation!

Pin 4 GND Ground

Pin 5 SDA Serial Data Input/Output. SDA is the data input/output for the I2C serial interface. The SDA pin is open drain and requires an external pull-up resistor. Arduino pin 4.

Pin 6 SCL Serial Clock Input. SCL is the clock input for the I2C interface and is used to synchronize data movement on the serial interface. Arduino pin 5.

7 SWQ/OUT Square Wave/Output Driver. When enabled, the SQWE bit set to 1, the SQW/OUT pin outputs one of four square-wave frequencies (1Hz, 4kHz, 8kHz, 32kHz). The SQW/OUT pin is open drain and requires an external pull-up resistor. SQW/OUT operates with either VCC or VBAT applied. An LED and 220 ohm resistor in series tied to VCC will produce a 1 HZ blink. This is a good way to tell if the clock chip is working.

8 VCC (5 volts)

For more information on the Ds1307 see Dallas Ds1307 Real Time Clock I2C plus RAM

atmega168/arduino in connections

#include <Wire.h> // specify use of Wire.h library.

#define ONE_HZ_SW 2 // one Hz square wave from Ds1307
#define blinkPin 13
#define Sw0 4
#define Sw1 5
#define Sw2 6

void setup()

  Wire.begin(); // join i2c bus (address optional for master)
  digitalWrite(blinkPin, 0);

  pinMode(ONE_HZ_SW, INPUT);
  pinMode(Sw0, INPUT);  // for this use a slide switch
  pinMode(Sw1, INPUT);  // N.O. push button switch
  pinMode(Sw2, INPUT);  // N.O. push button switch

  digitalWrite(ONE_HZ_SW, HIGH);
  digitalWrite(Sw0, HIGH); // pull-ups on
  digitalWrite(Sw1, HIGH);
  digitalWrite(Sw2, HIGH);

void loop()

  // wait or HIGH
  while(!digitalRead(ONE_HZ_SW)) {
  Wire.write(0);  // location pointer

  Wire.requestFrom(0x68, 7); // send 7 bytes
  byte secs =;
  byte mins =;
  byte hrs =;
  byte day =;
  byte date =;
  byte month =;
  byte year =;

  // hours, minutes, seconds

  Serial.print("The time is "); 
  if (hrs < 10) Serial.print("0");
  if (mins < 10) Serial.print("0");
  Serial.print(mins, HEX);
  if (secs < 10) Serial.print("0");
  Serial.println(secs, HEX);

  // use MM-DD-YYYY

  Serial.print("The date is "); 
  if (month < 10) Serial.print("0");
  if (date < 10) Serial.print("0");
  Serial.print(date, HEX);
  if (year < 10) Serial.print("0");
  Serial.println(year, HEX);

  if (!(digitalRead(Sw0))) set_time(); // hold the switch to set time
  while(digitalRead(ONE_HZ_SW)) {
  } // wait or LOW

// toggle the state on a pin
void toggle(int pinNum) 
  int pinState = digitalRead(pinNum);
  pinState = !pinState;
  digitalWrite(pinNum, pinState); 

void set_time()   {
  byte minutes = 0;
  byte hours = 0;

  while (!digitalRead(Sw0))  // set time switch must be released to exit
    while (!digitalRead(Sw1)) // set minutes
      if ((minutes & 0x0f) > 9) minutes = minutes + 6;
      if (minutes > 0x59) minutes = 0;
      Serial.print("Minutes = ");
      if (minutes >= 9) Serial.print("0");
      Serial.println(minutes, HEX);


    while (!digitalRead(Sw2)) // set hours
      if ((hours & 0x0f) > 9) hours =  hours + 6;
      if (hours > 0x23) hours = 0;
      Serial.print("Hours = ");
      if (hours <= 9) Serial.print("0");
      Serial.println(hours, HEX);

    Wire.beginTransmission(0x68); // activate DS1307
    Wire.write(0); // where to begin
    Wire.write(0x00);          //seconds
    Wire.write(minutes);          //minutes
    Wire.write(0x80 | hours);    //hours (24hr time)
    Wire.write(0x06);  // Day 01-07
    Wire.write(0x16);  // Date 0-31
    Wire.write(0x11);  // month 0-12
    Wire.write(0x13);  // Year 00-99
    Wire.write(0x10); // Control 0x10 produces a 1 HZ square wave on pin 7. 

Setting Hours and Minutes

Assuming the switches are wired as shown above, close Sw0 and while closed Sw1 and Sw2 will increment the hours and minutes as shown on the terminal. Release/open Sw0 and new time will be written to the Ds1307.

Added September 20, 2013:

Added June 7, 2013:

You Tube Arduino Microcontroller Video Series March 2012: