InduinoX User Guide - Working with RTC DS1307

The DS1307 on the InduinoX is meant to let you explore I2C communication on the Arduino. We shall see how to work with the DS1307 by using a Library[Part 1] and then by using our code based on the Arduino Wire Library [Part 2]

I2C Communication
I2C is short form for 'Inter Integrated Circuit' I2C Communication is Communication Bus standard developed by Phillips for standardising Communication between Integrated Circuits. For Eg. In a circuit, there could be a number of ICs each offering specific functionality[RTC, Temperature Sensor, EEPROM, etc] and they can all communicate on a single I2C Bus and provide combined functionalities. Each device on the I2C Bus would have a unique address by which it can be addressed.

Here's an Interesting Introduction from NXP



I2C on Arduino
The I2C Bus uses 2 lines for Communication - SDA(Serial Data) & SCL (Serial Clock). On the InduinoX / Arduino, these are available on  SDA (Analog Input 4) & SCL (Analog Input 5). The I2C bus can be accessed using the 'Wire' Library of Arduino. First, lets try out a Library


How to use Libraries in Arduino - An Overview

To use any library you download, unzip the downloaded file and copy its contents to the libraries folder inside your arduino directory. You can check the library by opening the arduino ide and going to Sketch -> Import Library Option, if your library is in the proper location, it will show up here. Next if there is an example provided with the library (it will be inside a folder called example inside the base folder of the library) it will show up under the libraries name in the File->Examples Menu.


Using the RTC Library
There are a number of libraries available to work with the DS1307, the one from Ladyada is our library of choice for this example. You can download it from here  => RTC Library

Unzip the downloaded file and copy its contents to the libraries folder inside your arduino directory. Open the Arduino IDE [close and reopen it if you had it open!], Open File->Examples->RTClib->ds1307

Compile & Upload the program to your InduinoX and open the Serial monitor. Set the baud rate of the Serial monitor to '57600' and voila you see the time!

Now Lets Modify the code and Build a simple project - Timer Controlled LED. The LED on pin 13 will  switch ON at a specific time and switch OFF at another specific Time.

Here's a Video the project in action


Here's the code for the project
 /*  
 Switches ON and OFF a LED at a Preset Time of Hour based on RTC  
  */  
 #include <Wire.h>  
 #include "RTClib.h"  
 RTC_DS1307 RTC;  
 #define ON_HOUR 13// Hour of the On Time  
 #define ON_MIN 0// Minute of the On Time  
 #define OFF_HOUR 13// Hour of the Off Time  
 #define OFF_MIN 1// Minute of the Off Time  
 void setup()  
 {  
  Serial.begin(9600);  
  pinMode(13,OUTPUT); // Pin of the LED to be Switched ON / OFF  
  Wire.begin();  
  RTC.begin();  
  RTC.adjust(DateTime("DEC 31 2011","12:59:45")); // Setting the time to a fixed value. If you want to use the system time comment this line and use the option below  
  // following line sets the RTC to the date & time this sketch was compiled  
  // RTC.adjust(DateTime(__DATE__, __TIME__)); //uncomment this line to set time to system time  
 }  
 void loop()  
 {  
  DateTime now = RTC.now();// Getting the current Time and storing it into a DateTime object  
  if(now.hour()==ON_HOUR && now.minute()==ON_MIN)  
   digitalWrite(13,HIGH);  
  if(now.hour()==OFF_HOUR && now.minute()==OFF_MIN)  
   digitalWrite(13,LOW);  
  Serial.print(now.year(), DEC);  
  Serial.print('/');  
  Serial.print(now.month(), DEC);  
  Serial.print('/');  
  Serial.print(now.day(), DEC);  
  Serial.print(' ');  
  Serial.print(now.hour(), DEC);  
  Serial.print(':');  
  Serial.print(now.minute(), DEC);  
  Serial.print(':');  
  Serial.print(now.second(), DEC);  
  Serial.println();  
  delay(1000);  
 }