DS1307 not keeping time while arduino is off

Hello,

I'm experiencing a weird phenomenon here. I have wired and programmed the DS1307 breakout board from lady ada and for some reason the clock is not keeping the time while the arduino is without power. As soon as I power the arduino back it starts exactly where it left. I don't know what's wrong since I bought this like a week ago.

I have it connected in the following way:

Analog pin 2: set to LOW for GND Analog Pin 3: set to HIGH for +5v Analog pin 4: for SDA Analog pin 5: for SCL

If I compile and upload the code to the arduino it keeps the same time as my computer until I power off my arduino. For example after uploading to the arduino the time is 9:00:00 and about a minute later it will show 9:1:0, which is ok. If at that moment I disconect the arduino from the USB and connect it again 5 minutes alter it will show 9:1:1 and keep counting. seems that it it keeping the time at the moment that it lost power and resuming as soon as power is applied again.

Could it be a bad battery? Even though is new?

Please any help will be greatly appreciate it, this is driving me crazy.

Your battery is not making contact read the instructions where it shows houw to solder a bump I you need more help post in the adafruit support forums

One thing to keep in mind is that a lot of pre-written code has an initial "setup" section that actually sets the clock.

Essentially, you run ONCE with this enabled (un commenetd out) to set the initial time on the chip and then you comment that section out. From then on, you rely on battery and and time keeping on the chip.

It sounds like you still have that part of the code not commented out. I'm only guessing, but that is what it sounds like to me.

Looking at the sample code from Adafruit aka Ladyada, you can see here is what I'm talking about below. It's commented out in this case.

if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }

It sounds like you still have that part of the code not commented out. I'm only guessing, but that is what it sounds like to me.

Yes, a very common mistake for newcomers using a RTC. It's a catch 22 in that the RTC must be set initially at least once, but then never again unless you have to change the time/data information.

I ended up making two RTC sketches, one that loads an initial date/time and one that doesn't and named them such that I wouldn't confuse the which to load.

Lefty

Thanks for the fast reply, I love this forum. Lady Ada, it’s an honor to have a reply from you and Pwillard. BTW, is there a way that I can move this to the appropriate Lady Ada forum section? Kind of newbee, it’s my first , ahem… second post.

NOw, based on both suggestions I did the solder bump on the board per the instructions when I assembled the kit, I took the battery out just to double check and yes there is the solder bump and it fits snuggly (unless for some reson the battery is not making good contact with it) here are a few pictures of it. Also I’am initializing the DS1307 with the line that Pwillard said. It is part of my code which I’m posting here:

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"
#include <SoftwareSerial.h>    // library to make a soft serial on any defined Pin

#define RTC5vPin 17 // pin 17 (analog pin 3) will be used to provide +5v to the RTC 
#define RTCgndPin 16 // pin 18 (analog pin 2) will be used to provide GND to the RTC
#define LCDtxPin 2  // pin 2 to tranmit ro the serial LCD

RTC_DS1307 RTC;
SoftwareSerial LCD = SoftwareSerial(2,LCDtxPin); // create an LCD object with soft serial on pin 2.

void setup () {

    pinMode(RTC5vPin, OUTPUT);        // sets Pin for HIGH output
    digitalWrite(RTC5vPin, HIGH);     // turn pin on (5v) to power RTC
    pinMode(RTCgndPin, OUTPUT);       // sets Pin for output
    digitalWrite(RTCgndPin, LOW);     // turn pin on LOW (GND) to power RTC

    Wire.begin();                     // Initialize I2C
    [glow]RTC.begin();[/glow]                      // Initiaize RTC object
    [glow]RTC.adjust(DateTime(__DATE__, __TIME__));[/glow] // Sets time per compouter compile time & date
  
    pinMode(LCDtxPin, OUTPUT);         // Set defined Pin as an Output Pin for transmission
    LCD.begin(9600);                   // Initialize LCD comms
    setLCD2x16();                      // Set LCD dimensions
    clearLCD();                        // Clear LCD
}

void loop () {
    [glow]DateTime now = RTC.now();[/glow]         //fetch time snapshot to now
    clearLCD();                       // Clear LCD
    LCD.print("Date: ");              // Print Date: string to LCD
    [glow]LCD.print(now.month(), DEC);[/glow]      // Print month numeric value ie. 12
    LCD.print('/');                   // Print date character divider /
    [glow]LCD.print(now.day(), DEC);[/glow]        // Print day numeric value ie. 25
    LCD.print('/');                   // Print date character divider /
    [glow]LCD.print(now.year(), DEC);[/glow]       // Print year numeric value ie. 2010
    
    LCD.print("Time: ");              // Print Time: string to LCD
    [glow]LCD.print(now.hour(), DEC);[/glow]       // Print hour value ie. 11
    LCD.print(':');                   // Print time character divider :
    [glow]LCD.print(now.minute(), DEC);[/glow]     // Print minute numeric value ie. 22
    LCD.print(':');                   // Print time character divider :
    [glow]LCD.print(now.second(), DEC);[/glow]     // Print second numeric value ie. 30
    delay(1000);                      // Delay for one second 
} 

void setLCD2x16(){
   LCD.print(0x7C, BYTE);   //command flag
   LCD.print(4, BYTE);    //position
   delay(600);
}
void clearLCD(){
   LCD.print(0xFE, BYTE);   //command flag
   LCD.print(0x01, BYTE);   //clear command.
}

Again thanks for the fast responce.

Also I forgot, I measure the battery voltage and it's a good and solid 3.219v.

@Richard, I'm using those because on the Lady Ada website it says that you can power it up from those ports. But powering it directly from the +5 and Gnd pins gives the same behavior.

Folks, a fellow Arduinite (Arduino) from the Lady Ada forum just helped me realize that once you set the time/date at compilation time you need to comment the setup line and recompile and upload. Now it's working flawless.

THanks to all the great people that helped me with this. Long live the Arduino!!! ;D

WHY are you powering the DS1307 from an Arduino logic output pin? Doesn’t make any sense???

Sure it does - makes life easier when it comes to physical connections sometimes. See, for example, the common Arduino-Wii nunchuck connection that uses analog pins 2-3-4-5 for power ground and I2C. I’ve personally used a digital pin to power an SPI temerature sensor, so that I could use a 6 pin straight header to connect it.

In this case, the DS1307 consumes 1.5mA, which is well within an ATmega’s drive capability.

-j

Well… lets just say that sometimes things work… and then they don’t.

I think the point is that you always want to keep your power source as “clean” as possible. Clean being as close to the 5Volts or whatever your device needs.

The Arduino digital PINS are intended mostly for LOGIC level work, so HIGH does not always (and doesn’t have to) equal Vcc (5Volts). So the question is… what can the device you are driving “tolerate”?

I’ll bet you will eventually encounter a situation where this just doesn’t work well.

As with everything… there are “best practices” and “good enough’s”. Your method is admittedly good enough for what you have done so far… but is is also not very close to “best practice”.

I could go on… but I’ll stop now.

Glad to see you got it working.