Loading... Please wait...

DS1307 Real Time Clock Module

DS1307 Real Time Clock Module

This example shows how to use a DS1307 Real Time Clock Module with the Time download from the Arduino Plyground here. Note that you must extract the three sub-folders - Time, DS1307RTC and TimeAlarms and make sure they are at the top level in your libraries folder - otherwise you will get compile-time errors. There are many other libraries available and you can use the Wire library to use I2C directly. However the Time library with the supplied DS1307RTC library is perhaps the most Arduino-like way of using the module. We supply a simple sketch to use to both set the RTC and the Arduino to the intial time - see below.

Hardware Required

  • Arduino Board
  • DS1307 Real Time Clock Module supplied in the oddWires kit
  • Jumper wires
  • Breakaway header pins (4)

Circuit

To build the circuit, solder 4 breakway pin headers from the supplied 40-pin header (they snip off easily with pliers).  Connect Vcc to +5V, GND to GND, SDA to Arduino Pin A4 and SCL to Arduino Pin A5.

 

 

Code

/*
 * initialRTCSet
 * example code to set both the Arduino timer
 * and the RTC clock
 *
 */

#include  
#include  
#include   // a basic DS1307 library that returns time as a time_t

void setup()  {
  Serial.begin(9600);
  //      hh  mm  ss  dd  m  yyyy
  setTime(18, 00, 00, 19, 8, 2014); // set Arduino time
  RTC.set(now());                   // now set the RTC to Arduino time
  setSyncProvider(RTC.get);         // and set the SyncProvider to RTC - use this to get back in
                                    // sync after reset - see TimeRTC example  
                                   
}

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

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

 

What's News

newsletter

Copyright 2017 oddWires. All Rights Reserved.
 Sitemap | oddWires