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.
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.
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.
nice summary - was quite useful to me (as i had the same in mind ^^)
Maybe two small additions:
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).
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)
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.
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.
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.
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.
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.
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();
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.