Arduino and DS1307

Hi

I am trying to use the RTC exanple in the time library. I am sure I have my DS1307 connected correctly but When I upload the sketch it is hanging on the following line

setSyncProvider(RTC.get);

I am sure I have the DS1307 wired correctly.

Using the timRTC.pde example I am certain it is hanging on the above code line. I have also tried 3 different DS1307 chips but all behave the same.

Can anyone help

Is the RTC connected to a backup battery? If not they might behave in unexpected ways.

Hi

Yes I have it set up as follows

Pin 1 - Crystal Pin 2 - Crystal Pin 3 - Battery + Pin 4 - Gnd Pin 5 - Arduino pin 4 Pin 6 - Arduino pin 5 (with 10k Pullupto 5V+) Pin 8 - 5V+

OK, Can you measure the voltage of the battery?

Not 100% clear from your connection list:

DS1307 Pin 6 to Arduino [u]Analog[/u] Pin A5 and DS1307 Pin 5 to Arduino [u]Analog[/u] Pin A4, right?

Negative Battery contact to GND, right?

Regards,

Dave

Is the chip the right way round? Do you have the pins wired up in the correct order? (Sorry if this sounds annoying but it’s always best to ask to ensure that it’s not the issue)

The voltage of the backup battery should not affect it. I would suggest connecting the battery connection to ground to ensure it’s not a funny battery though.

If you have a multimeter then check the voltage between 5v and gnd (on the chip) to maks sure that power is getting to the chip.

From 1307 datasheet:

VBAT – Battery input for any standard 3V lithium cell or other energy source. Battery voltage must be held between 2.0V and 3.5V for proper operation.

From 1307 datasheet: Quote: VBAT – Battery input for any standard 3V lithium cell or other energy source. Battery voltage must be held between 2.0V and 3.5V for proper operation.

Yes, for backup, it should work fine with a flat battery becuase when it is powered by the MCU, it does not use the battery. That is why you can ground that pin if you don't need backup.

Mowcius

Thanks for your replies,

I have tested voltages on the chip etc as follows

Battery = 3v steady

On the DS1307 between pin 4 (Gnd) and Pin 3 3V+ battery = 3v

Between pin 4 (gnd) and Pin 8 (5v+) = 4.98V

I have the DS1307 Pin 5 connected to Arduino Analog pin 4 and pin 6 on the DS1307 to arduino analog pin 5 with a 10k pullup resistor to 5v+

I am sure I have the chip the right way round there is a notch to indicate the top and a little indented circle to show which side is pin 1.

In my code which I have added to this post if I just try and read values it loops through and I get the following response

0:0:0 0/0/2000

If include the code to set the time then it prints the ‘i am setting time message’ and then hangs. Nothing else is displayed

#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>

void setup()
{
  Serial.begin(9600);
  Serial.println("I am setting Time");
/*
  RTC.stop();
  RTC.set(DS1307_SEC,1);       
  RTC.set(DS1307_MIN,23);     
  RTC.set(DS1307_HR,12);       
  RTC.set(DS1307_DOW,4);       
  RTC.set(DS1307_DATE,5);       
  RTC.set(DS1307_MTH,3);        
  RTC.set(DS1307_YR,9);         
  RTC.start();
*/
  Serial.println("Setting Time Complete");
}

void loop()
{
  Serial.println("I am reading time");
 /* Serial.print(RTC.get(DS1307_HR,true)); */
  Serial.print(RTC.get(DS1307_HR,false)); 
  Serial.print(":");
  Serial.print(RTC.get(DS1307_MIN,false));
  Serial.print(":");
  Serial.print(RTC.get(DS1307_SEC,false));
  Serial.print("      ");                
  Serial.print(RTC.get(DS1307_DATE,false));
  Serial.print("/");
  Serial.print(RTC.get(DS1307_MTH,false));
  Serial.print("/");
  Serial.print(RTC.get(DS1307_YR,false)); 
  Serial.println();
  Serial.println("Reading time complete");
  delay(1000);

}

Sorry

Posted the wrong code that was my old code. Here is what I am currently using.

And it stops after the ‘I am getting time’

#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h> 


void setup()  {
  Serial.begin(9600);
  Serial.println("I am Getting time");
  setSyncProvider(RTC.get);   
  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);   
        setTime(t);          
     }
  }
  Serial.print("I am displaying time");
   digitalClockDisplay();  
   delay(1000);
}

void digitalClockDisplay(){
  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){
  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;
}

Well the code works fine here so I think we'll have to say that unfortunately if sounds like a duff DS1307 chip.

Mowcius

Disconnect the battery, does it display the time? If so, are you running from USB power?

I had similar troubles running from USB, connecting a DC supply solved the probems.

Your code works fine for me on my RTC shield.

I am not using any pull-up resistors on the I2C signals. Instead I just rely on the internal pull-ups.

I would suggest trying without the 10k you are using. And if you want to use external resistors, use something smaller like 4.7k. Make sure you put them on both the Clock and Data line, not just one of them.

I brought two more DS1307's and still the same problem. However If i connect the circuit to my Arduino Mega it works fine. Does this mean my Duemilanove has a problem ?

I brought two more DS1307's and still the same problem. However If i connect the circuit to my Arduino Mega it works fine. Does this mean my Duemilanove has a problem ?

Also with the Arduino Mega it appears to work but does not set the time on the RTC if i use T1289663280 it should set it on the RTC and Arduino, but it does not if I reset the Arduino the time goes back to 0:0:0 31:12:1999

The DS1307 is connected with a battery backup etc, so should return the correct time when reset.

Hmm, yeah sounds like something funny is going on with your duemilanove. Does the 'old' DS1307 code work (where it sets the time with the sketch)?

Use that sketch to set the time, then load up the other sketch and see if it has kept the time you set.

Have you tested the analogue input pins work correctly ? Connect them to ground and take an analogue reading then connect to +5V and take another reading. You should get zero then 1023 (or close to that)

Just an idea.