This Electronic Brick (Available Here) allows you to have the correct time and date available for your project. Date/Time is saved even when your project is powered off. This is really needed for Home Automation, Greenhouse and Aquarium applications etc.


The DS1307 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 over an I²C, 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, but the library only supports the 24-hour mode.

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. Note: this module must be installed with the battery to work normally and retain data and time with power off.. The battery is a button (type CR1220), the positive upward:


This unit requires a software library to support it. We are now using the library originated by Adafruit. The original library can be downloaded HERE:
In case of problems downloading, here is a local copy:


This Brick needs the following 4 connections (shown bottom-to-top order above) to an Arduino or Mega:


Arduino Pin

Mega Pin








Analog A4



Analog A5


There are several ways to make these connections:
  • Easiest Way: Use a 4-pin cable plugged into a Sensor Shield (Black wire to - )
  • Easy Way: Use a 4-pin section of a Cablemaker to make connections. Note the colors on each end.
  • Or your own wiring...


Like most complex devices, the easiest way to program it is with a Library someone else has written. Download the Library as noted above. See ABOUT LIBRARIES.

Here is a test Software Sketch you can cut and paste to test this brick. Also you can check the EXAMPLES folder of the library.

/* YourDuino.com Example Software Sketch
 DS1307 Real Time Clock test
 -----(Arduino) (Mega) ------
 SDA  Analog A4   20
 SCL  Analog A5   21
 Based on: http://github.com/adafruit/RTClib
 Also see: http://www.ladyada.net/learn/breakoutplus/ds1307rtc.html
 terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <Wire.h>
#include "RTClib.h"

/*-----( Declare objects )-----*/

void setup()   /*----( SETUP: RUNS ONCE )----*/
  Serial.begin(9600);  // Ready to print to Serial Monitor  
  Serial.println("YourDuino DS1307 RTC Test. http://yourduino.com");
  Wire.begin();        // Start the Wire (I2C communications)
  RTC.begin();         // Start the RTC Chip
 /* Set the date / time to the time this program was compiled.
    Comment this OUT, AND upload, to let the clock just run.  */ 
//  RTC.adjust(DateTime(__DATE__, __TIME__));
}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
  DateTime now = RTC.now();  //Get the current data

  Serial.print("The year is ");
  Serial.print(now.year(), DEC);
  Serial.print(" Month = ");
  Serial.print(now.month(), DEC);
  Serial.print(" Day = ");
  Serial.print(now.day(), DEC);
  Serial.print(" Time = ");
  Serial.print(now.hour(), DEC);
  Serial.print(now.minute(), DEC);
  Serial.print(now.second(), DEC);

  Serial.println("EXAMPLES of Other Calculations:");
 // UNIX time is often used as Timestamps 
  Serial.print("UNIX Time since midnight 1/1/1970 = ");
  Serial.print(" seconds, which is ");
  Serial.print(now.unixtime() / 86400L);

  // calculate a date which is 7 days and 30 seconds into the future
  DateTime future (now.unixtime() + 7 * 86400L + 30);

  Serial.print(" Future Date: now + 7 Days + 30 Sec: ");
  Serial.print(future.year(), DEC);
  Serial.print(future.month(), DEC);
  Serial.print(future.day(), DEC);
  Serial.print(' ');
  Serial.print(future.hour(), DEC);
  Serial.print(future.minute(), DEC);
  Serial.print(future.second(), DEC);


Cut and paste this example into a blank Arduino IDE page. UNcomment the line that SETS the clock, and Verify (compile) it. Then upload it.
After uploading the program, start the Serial Monitor (Make sure it's set to 9600 in the box on lower right).
The output should look like this:
---------------------( COPY )--------------------------
YourDuino DS1307 RTC Test. http://yourduino.com
The year is 2012 Month = 5 Day = 30 Time = 11:2:15
EXAMPLES of Other Calculations:
UNIX Time since midnight 1/1/1970 = 1338375735 seconds, which is 15490days
Future Date: now + 7 Days + 30 Sec: 2012/6/6 11:2:45
-----------------( END COPY )----------------------
To Have Clock run on it's own, and be used in other applications: Comment OUT the line in the above example that SETS the time, and upload that version. The clock will keep running.