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 NonVolatile 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 can operate 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 battery supply. Timekeeping operation continues while the part operates from the backup supply. Note: this module must have the battery installed 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:
NOTE: If you want to get started using the functions of a Real Time Clock before you get the hardware, look in the EXAMPLES for "RTCLIB" for the software sketch "softrtc". This will give you the same functions without the hardware (but the time will drift off much more quickly).


This Brick needs the following 4 connections 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 or YourDuino RoboRED (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 and install the Library as noted above. See ABOUT LIBRARIES. If you get a Verify error like "does not name a type" that means the library is not installed correctly.

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.

/* YourDuinoStarter Example: DS1307 Real Time Clock
 - WHAT IT DOES: Set and/or Run DS1307 Real Time Clock
 - SEE the comments after "//" on each line below
   Function  Arduino Pin  Mega Pin
   -         Gnd          Gnd
   +         +5V          +5V
   SDA       Analog A4    20
   SCL       Analog A5    21
 - V1.02 04/26/2015
   Questions: terry@yourduino.com */

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

/*-----( Declare Constants and Pin Numbers )-----*/
// Not needed: RTC Lib expects connections as above

/*-----( Declare objects )-----*/
RTC_DS1307 rtc;    // Create a RealTimeClock object

/*-----( Declare Variables )-----*/
// NONE because the library handles this...

void setup()   /****** SETUP: RUNS ONCE ******/
  Serial.begin(9600); // Set up for Serial Monitor to be able to see this work
  // Set this in the lower right of the Serial Monitor screen
  Serial.println("YourDuino.com DS1307 Real Time Clock - Set / Run Utility");

  /*----( These lines allow code to work also on Arduino DUE )----- */
#ifdef AVR
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due

  rtc.begin(); // Start the RTC library code

  /*----( SET the date and time.  Comment OUT these lines after setting )----*/
  // Put these "//" in front of the line you do NOT want to use
  // following line sets the RTC to the date & time this sketch was compiled
     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // May 21, 2015 at 6pm you would call: (use 24 hour time)
  //    rtc.adjust(DateTime(2015, 5, 21, 18, 0, 0));

}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
  DateTime now = rtc.now();  // Read data from the RTC Chip

  Serial.print(now.year(), DEC); //
  Serial.print(now.month(), DEC);
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(now.minute(), DEC);
  Serial.print(now.second(), DEC);


}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

//*********( THE END )***********

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 )--------------------------
2015/4/26 19:29:32
2015/4/26 19:29:35
2015/4/26 19:29:38
-----------------( 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.