/* FILE HCRTC_Example.cpp
DATE: 23/12/13
VERSION: 0.2
AUTHOR: Andrew Davies
This is an example of how to use the Hobby Components RTC library to read and
write to and from the DS1307 real time clock 24C32 EEPROM. The library is intended
to be used with our RTC clock module (HCMODU0011), but should work fine with any
module that uses a DS1307 device.
You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/
/* Include the wire library */
#include "Wire.h"
/* Include the Hobby Components RTC library */
#include <HCRTC.h>
/* The RTC and EEPROM have fixed addresses of 0x68 and
0x50 so define these in software */
#define I2CDS1307Add 0x68
#define I2C24C32Add 0x50
/* Example string to write to EEPROM */
char ExampleString[] = "Hobby Components";
/* Used in example as an index pointer */
byte index;
/* Create an instance of HCRTC library */
HCRTC HCRTC;
void setup()
{
Serial.begin(9600);
/* Use the RTCWrite library function to set the time and date.
Parameters are: I2C address, year, month, date, hour, minute, second,
day of week */
HCRTC.RTCWrite(I2CDS1307Add, 13, 4, 24, 14, 21, 0, 3);
}
void loop()
{
/************* Example write to EEPROM ***************/
/* Initiate a write sequence.
Parameters are: I2C address, EEPROM start location */
HCRTC.EEStartWrite(I2C24C32Add, 32000);
for (index = 0; index < sizeof(ExampleString); index++)
{
/* Sequentially write example data to the EEPROM */
HCRTC.EEWriteByte(ExampleString[index]);
}
/* End the write sequence */
HCRTC.EEEndWrite();
/* Wait for cached data to finish writing */
delay(10);
/************* Example read from EEPROM ***************/
/* Initiate read sequence.
Parameters are: I2C address, EEPROM start location */
HCRTC.EEStartRead(I2C24C32Add, 32000);
for (index = 0; index < sizeof(ExampleString); index++)
{
/* Sequentially read data from EEPROM and output it to the UART */
Serial.write(HCRTC.EEReadByte(I2C24C32Add));
}
Serial.println();
/* Continuously read the current time and date from the RTC */
while(true)
{
/* Read the current time from the RTC module */
HCRTC.RTCRead(I2CDS1307Add);
/* Output the information to the UART */
Serial.print(HCRTC.GetDay());
Serial.print("/");
Serial.print(HCRTC.GetMonth());
Serial.print("/");
Serial.print(HCRTC.GetYear());
Serial.print(" ");
Serial.print(HCRTC.GetHour());
Serial.print(":");
Serial.print(HCRTC.GetMinute());
Serial.print(":");
Serial.print(HCRTC.GetSecond());
Serial.print(" DOW:");
Serial.println(HCRTC.GetWeekday());
/* Now output the same thing but using string functions instead: */
Serial.print(HCRTC.GetDateString());
Serial.print(" ");
Serial.println(HCRTC.GetTimeString());
/* Wait a second before reading again */
delay(1000);
}
}

FAQ:

What does the SQ pin do?
The DS1307 has a feature where you can output a square wave to a pin. This can at fixed frequencies of 1Hz, 4KHz, 8KHz, and 32KHz. You have to configure an internal register to do this but when configured correctly the square wave will appear on the SQ pin.

What does the DS pin do?
If you take a look at the pictures in this post you'll notice there a unsoldered 3 pads in the top left corner. These allow you to solder an optional DS1820 temperature sensor. If you do this the DS pin provides access to its data out pin.

How do I connect this module to an Arduino?
The module needs to be connected to your Arduino's I2C interface (SDA & SCL pins). For an Arduino Uno, Nano, or Pro Mini you can connect the module as follows:

This is because of the way the print function prints out numbers, rather than anything to do with the RTC library. To pad out the number with an extra zero, a quick and simple way would be to just test if the number is less then 10 and if so, print out an extra zero. For example:

I've recently bought a Tiny RTC DS1307 module from your ebay store and I've a problem since the module loses time when I power off Arduino.
I've tried with the library (HCRTC) and the example sketch that you have provided and every time I disconnect Arduino from power the time is reset to 1/1/2000.

For testing I've uploaded the HCRTC_Example sketch, then I've reuploaded the sketch with line

From the information you have given us you don't appear to be doing anything wrong. It really does sound like the DS13072 is not getting power from the battery. The most likely cause is a bad contact in the battery holder but the fact that you can measure the battery voltage at the BAT pin suggests this is not the case. If possible, with the module not powered, could you carefully measure the voltage at pin 3 (VBAT) of the DS13072? This is the 3rd pin down from the pin marked with a dot.

Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Sorry, I just realised my question could be misinterpreted... Would you be able to measure the voltage on the pin of the IC itself? Its the IC marked DS13072. The pin you are looking for is pin 3 which is the 3rd one down from the pin with a dot next to it. This may be a little fiddly as the pins are very close together. You will also need to do this with no power applied to the module (including IO connections from your Arduino), but with the battery connected. This will tell us if the chip itself is getting power from the battery. Everything you have said suggests that the device is not getting the battery backup.

I wouldn't get too hung up about what happens when you remove the 5V supply because if we assume that the device is also not getting the 3V from the battery then it's probably just getting enough leakage current from the data pins on the Arduino, but not enough to keep the clock circuitry running. The battery voltages you measure seem sensible to me.

Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Yes, I've already measured it on the IC (in the image I've measured voltage between pins highlighted in red) with the module disconnected from any power source (other than the module battery) obtaining 2.94V.

Ok, it sounds like you do have a faulty module. I'm going to pull one out of stock and check that nothing has changed to stop the software from working but this is unlikely. I can't find you on our system from your forum details, could you email us either your order number or account number if you bought it via our website, or your eBay user name if you bought it from our eBay shop. The email address is sales@hobbycomponents.com

Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Thanks for confirming your account. I've pulled one out of stock and it works fine with the library so it does look like you have one with a weird fault. I'm going to contact you via email to arrange a replacement or refund.

Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.