Real Time Clock Module Issues

Hi all,

I am having trouble with my Tiny RTC module. I have SCL connected to A5 and SDA to A4. VCC and ground are connected (5V) and I have a LIR2032 backup battery in. I am trying to set the clock with the following code:

/*
 * TimeRTCSet.pde
 * example code illustrating Time library with Real Time Clock.
 *
 * RTC clock is set in response to serial port time message 
 * A Processing example sketch to set the time is inclided in the download
 */

#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t


void setup()  {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");      
}

void loop()
{
  if(Serial.available())
  {
     time_t t = processSyncMessage();
     if(t >0)
     {
        RTC.set(t);   // set the RTC and the system time to the received value
        setTime(t);          
     }
  }
   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);
}

/*  code to process time sync messages from the serial port   */
#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message

time_t processSyncMessage() {
  // return the time if a valid sync message is received on the serial port.
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ; 
    Serial.print(c);  
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();          
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
        }
      }   
      return pctime; 
    }  
  }
  return 0;
}

When I run this code and set the time, it gets set correctly. However, when I then try to read the time back in another program, the time always resets to 17:18:09 and the date has been either 4/2/2037 or 6/29/2037. Here is the code for reading the time:

// include the library code:

#include <Arduino.h>
#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 4, 5, 6, 7);

void setup()  {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  //setTime(17,37,0,26,7,2014);
  
  lcd.begin(20, 4);
}

void loop()
{
  lcd.clear();
  if(hour()>12){
    lcd.print(hour()-12);
  }
  else{lcd.print(hour());}
  lcd.print(":");
  lcd.print(minute());
  lcd.print(" ");
  lcd.print(month());
  lcd.print("/");
  lcd.print(day());
  lcd.print("/");
  lcd.print(year());
  
  
   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);
}

All help is appreciate! Thanks!

author=rossmon link=topic=257374.msg1819594#msg1819594 date=1406422645]

I am having trouble with my Tiny RTC module.

#include <Arduino.h>

If nothing else, I suspect the code is dated. Try this article

http://bildr.org/2011/03/ds1307-arduino/

So I tried using the code from that website you referred me to to set the date on the RTC (see below), but when I ran it, the time that's printed to the serial monitor is 165/165/165 45:165:165. I ran the program to read the date as well and it gave me the same thing.

Any thoughts?

Also, I am running an ATMega328 on a Duemilanove board.

Thanks

//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527


void setup(){
  Wire.begin();
  Serial.begin(9600);
  setDateTime(); //MUST CONFIGURE IN FUNCTION
}

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

void setDateTime(){
  byte second =      45; //0-59
  byte minute =      40; //0-59
  byte hour =        0; //0-23
  byte weekDay =     2; //1-7
  byte monthDay =    1; //1-31
  byte month =       3; //1-12
  byte year  =       11; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start 

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}

I down loaded the second code you posted and it ran as intended for me on an R3 uno. No 165/165/165 45:165:165. Just the time and date given in the sketch.

I will try it later on a Duemilanove with a 328 and if its any different I'll get back to you. It shouldn't be.

Does the first sketch you showed still set the time and date as intended? You were only having problems with read back at that time.

If it doesn't work, it's possible that your wire.h library got corrupted and you may need a new version.

rossmon:
, the time that's printed to the serial monitor is 165/165/165 45:165:165.

I think that is a power problem. It is more commonly seen when displaying to LCD. Maybe the RTC is trying to charge its battery. It may be just a poor connection. If you are using USB power, it might be time to change to a wall wart.

cattledog:
Does the first sketch you showed still set the time and date as intended? You were only having problems with read back at that time.

Yes, it was only in the first sketch. Where is the best place to get the most up to date wire.h library?

Nick_Pyner:
I think that is a power problem. It is more commonly seen when displaying to LCD. Maybe the RTC is trying to charge its battery. It may be just a poor connection. If you are using USB power, it might be time to change to a wall wart.

There were some other signs that running off of the USB wasn't getting enough power. I just ordered some power plug's to power it from a 9V battery. I will report back once I have tried it with 9V power source.