DS1307 Real time clock on breadboard

Dear all,

Heres my simple real time clock on a breadboard.
Using the DS1307 http://www.glacialwanderer.com/_blog/blog2008/04_April/DS1307.pdf
The breakout boards you can buy seemed a bit expensive so I decided to hook up the cheap components on a breadboard to try out on my Arduino.
The Crystal is 32768kHz.
The battery is a 3v coin cell in a holder.

Heres a pic

Heres a schematic of the breadboard.
The photo doesn't completely match the schematic but the wiring and pins are correct.

Thanks to Electronic Breadboard Templates - ElectronicBreadboardTemplates for the inkscape template.

I used the code from this forum topic http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1191209057
You need to install the DS1307 library from there.
This code was compiled on version 13 of the ide with a 328p but will work with a standard arduino.

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

int rtc[7];

void setup()
{
  Serial.begin(9600);
/*
  RTC.stop();
  RTC.set(DS1307_SEC,1);
  RTC.set(DS1307_MIN,57);
  RTC.set(DS1307_HR,17);
  RTC.set(DS1307_DOW,4);
  RTC.set(DS1307_DATE,18);
  RTC.set(DS1307_MTH,2);
  RTC.set(DS1307_YR,9);
  RTC.start();
*/
}

void loop()
{
  RTC.get(rtc,true);

  for(int i=0; i<7; i++)
  {
    Serial.print(rtc[i]);
    Serial.print(" ");
  }
  Serial.println();

  delay(1000);

}

To initialy set the time uncomment the RTC.set lines and change the values.
Once youve done this the clock is set and the 3v battery keeps the time.

Eventually its going to be incorporated in my main project.
A temperature logging device with one wire temp sensors ,web server, serial lcd display, buttons and using the 328p. But its an ever evolving project. Once I get the time tied up with the temp data, Ill be nearly there.

If Ive made a glaring mistake let me know.

Gordon

nice summary - was quite useful to me (as i had the same in mind ^^)
Maybe two small additions:

  1. It seems the chip does not run without a 3V supply on Pin 3 - so you have to either hook up a 3V battery on it or attach it to the 3V Pin of the arduino (of course data is not saved the second way).

  2. I very much doubt that you used a crystal that "is 32768kHz." ;0) Its a 32.768kHz crystal ,0). Its not a big story, but i was wondering as it didnt run (see 1) if i had used the wrong one ;0)

Thanks again for that post :0)

I thought Id put it together as Id found the info from various sources.

1, Yes I missed the decimal point, 32.768kHz

2, Thats why they recomend the button cell.
If you dont mind setting the time on reboot of the arduino then the real time library is easier and doesnt have external components.
I wanted something that would survive power down so used the 3v button cell and holder.
I hear they can last nearly 9 years?
Some button cell holders have longer leads underneath that make it easier to plug in to the breadboard.

Ive just put an Atmel 168 on a breadboard and got it working. Next im going to put the 168 and DS1307 on stripboard ready to make a binary clock. Always wanted a binary clock.

Gordon

Don't forget that the DS1307 has a programmable clock pulse output pin avalible on it's pin 7. I think it defaults to 1 sec clock output.

If you wire that to a jumper pin and then onto a Arduino I/O pin you have a source for contious clock for your applications. If wire it to pins 2 or 3 you can use it as a freerunning interrupt source.

Lefty

Here's a little function that sets the clock pulse output pin on the DS1307 . . .

void Set_Square_Wave(int rate){  // set the square wave output on pin 7 of the DS1307 chip
  rate = rate + 144;                         // add 0x90 (dec 144) to rate
  Wire.beginTransmission(0x68);              // write the control register
  Wire.send(0x07);                           // register address 07H)
  Wire.send(rate);                           // 90H=1Hz, 91H=4kHz, 92H=8kHz, 93H=32kHz
  Wire.endTransmission();
}

Pass it 0-3 (maybe 1-4 :)) and get 1 of the 4 outputs. Always thought it could come in handy, but never thought of using it for interrupts - nice idea.

No , the DS1307 will work without the 3v battery .
It just resets after each reboot , so is o.k for the set up tests where the need a clean chip.

Interestering.
I tried yesterday the time i wrote that small paragraph. without three volt on that pin it didnt 'update' the time while running 0o...

All I can say is that my DS1307 does, but will just revert back after every power off.

So far my LED clock which is using the DS1307 for timing looks to be almost working.
4 * 74LS47, and a MCP23016-1/SP , I think I went mad with the chip count over programming complexity .
I could have done the same thing with just 2 * MCP23016-1/SP chips, But looking at other LED shields they don`t even have that.

GordonEndersby
the code that you posted in the first post work with ds1337?

There is code for the ds1337 in the thread I quoted.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1191209057

Gordon

I tried to set the year of DS1307 with #include <DS1307.h>
till 2012 its OK
but e.g.

RTC.set(DS1307_YR,13);
or RTC.set(DS1307_YR,98);
dont work.
or date 31

what is the problem ?

Thanks Gordon for the excellent photos. Here is a link to three code sets tested on the duemilanove and the mega. The first set allows you to set the arduino time manually using epoch time. The second set lets you set the arduino clock and the 1307 real time clock using epoch time, and the third set is the basic code to set the arduino using an rtc1307 that has been set to current time. Hope this helps.

Hi GordonEndersby

Just remember when you are using a webpage to set the time, i'ts a byte and not at char there is used to set the time.

Running without a 3v cell...

I tried yesterday the time i wrote that small paragraph. without three volt on that pin it didnt 'update' the time while running 0o...

You need to link the pin to ground that would otherwise go to the positive terminal on the battery.

nice to see activity here today, who recognized my problem ? #10

Shiwa, i think the year is only one byte like RTC.set(DS1307_YR,13); = 2013; You also need to set them in order like

Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();

alm 7100 thank you, I will try, if I have some time

Sorry, I hadnt seen the updates added to my original post.
I hadnt tried anything higher than 2010 on my setup.

Gordon

I used the next instructions:

RTC.stop();
RTC.set(DS1307_SEC,0);
RTC.set(DS1307_MIN,03);
RTC.set(DS1307_HR,21);
RTC.set(DS1307_DOW,7);
RTC.set(DS1307_DATE,1);
RTC.set(DS1307_MTH,5);
RTC.set(DS1307_YR,10);
RTC.start();

it works fine ;D , tried again today to be sure..

-----------> BUT if I change :

RTC.stop();
RTC.set(DS1307_SEC,0);
RTC.set(DS1307_MIN,03);
RTC.set(DS1307_HR,21);
RTC.set(DS1307_DOW,7);
RTC.set(DS1307_DATE,1);
RTC.set(DS1307_MTH,5);
RTC.set(DS1307_YR,13); //<--------------- 13 !!
RTC.start();

I see on the display the year 2000 !!! not 2013... >:(

Perhaps this sort of high level instructions does not work all the way ?

From the retail boards that I've seen, there is normally a capacitor (Looks to be 0.1uF.), and two resistors (On the Ladyada version they are 2.2K). What does this do?

From what I understand the resistors are in line with the serial clock and serial data. I also understand that the cap is for power smoothing? I don't know if it's on the battery line, or the regurlar supply... Can someone please confirm or correct this?

EDIT: From what I understand from the datasheet, the resistors are pull up resistors that go from VCC (+5V) to the serial data and the serial clock pins.