DS1307 problem with 165

Hello there.
I use the sample code from the website,here are code below.

//
// Maurice Ribble 
// 4-17-2008
// http://www.glacialwanderer.com/hobbyrobotics

// This code tests the DS1307 Real Time clock on the Arduino board.
// The ds1307 works in binary coded decimal or BCD.  You can look up
// bcd in google if you aren't familior with it.  There can output
// a square wave, but I don't expose that in this code.  See the
// ds1307 for it's full capabilities.

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

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

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

// Stops the DS1307, but it has the side effect of setting seconds to 0
// Probably only want to use this for testing
/*void stopDs1307()
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.write(0x80);
  Wire.endTransmission();
}*/

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second,        // 0-59
                   byte minute,        // 0-59
                   byte hour,          // 1-23
                   byte dayOfWeek,     // 1-7
                   byte dayOfMonth,    // 1-28/29/30/31
                   byte month,         // 1-12
                   byte year)          // 0-99
{
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   Wire.write(0);
   Wire.write(decToBcd(second));    // 0 to bit 7 starts the clock
   Wire.write(decToBcd(minute));
   Wire.write(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   Wire.write(decToBcd(dayOfWeek));
   Wire.write(decToBcd(dayOfMonth));
   Wire.write(decToBcd(month));
   Wire.write(decToBcd(year));
   Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
          byte *minute,
          byte *hour,
          byte *dayOfWeek,
          byte *dayOfMonth,
          byte *month,
          byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();
  
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second     = bcdToDec(Wire.read() & 0x7f);
  *minute     = bcdToDec(Wire.read());
  *hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  *dayOfWeek  = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month      = bcdToDec(Wire.read());
  *year       = bcdToDec(Wire.read());
}


void setup()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  Serial.begin(9600);
  
  // Change these values to what you want to set your clock to.
  // You probably only want to set your clock once and then remove
  // the setDateDs1307 call.
  second = 45;
  minute = 3;
  hour = 11;
  dayOfWeek = 52;
  dayOfMonth = 27;
  month = 12;
  year = 16;
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

void loop()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print("  Day_of_week:");
  Serial.println(dayOfWeek, DEC);

  delay(2000);
}

But the time i got are wrong,and keep show up the same number like this:

And the circuit i wired is here:

How can i fix that? The battery i used is CR2032 3V.But the result is same to without battery.
Thank you for read this question!

The clock does not know the time until after you first set the time. Have you set the time?

Not sure what value those pull up resistors are, 22 something ? however the DS board has inbuilt pull up resistors , so you do not need yours, remove them and try again.

Also to me, it looks like you have the I2c connections the wrong way around.

The battery is not needed in order to test run ok.

Also be aware that the DS board has a charging circuit built in, so its trying to charge your Non chargable battery.
For safe long term use, use a Rechargeable LR2032 or disconnect the charging circiut.

000145.jpg

Hello ricky101.
Thanks for your advice.It work after i took off the resistance! But here's my problem.
The time that show up on monitor is the time i set,but when i close the window and open again ,it show the time i set at the beginning.Means it will not continue counting the time. (i put the battery on)

Hi,

The code you program in contains the time to set the Ds1307 to.

When you start the Serial Monitor, its like pressing the Reset Button, it Restarts the program from the beginning, so you set the clock back to the orignal time

What you do, is run your code once to Set the DS to the time you want, as long as its powered it will retain the time.

To allow you to restart the Serial monitor or to upload some modified code without resetting the DS then you need to comment out this line; which is what sets the time and then compile and upload it.

so // setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

Also you might want to look as some more modern code examples for the DS like this one, much simpler code to follow.

https://arduino-info.wikispaces.com/DS1307_RealTime_Clock_Brick

Thanks a lot, ricky101.
The code work greatly after i annotated :
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
Also i found that i have to upload the code twice if i need to reset the current time.
The second time need to annotate the setDateDs1307(.....
Now the DS1307 can keep time by battery. Thank you very much :slight_smile: