Tuesday, November 10, 2009

DS1307 Module

I've been doing a few projects lately using the DS1307 IC. If you don't already know, the DS1307 is a real time clock chip (as opposed to an unreal time clock chip, which is often used by musicians and people attending events on Facebook). This means that you just have to attach a quartz watch crystal to it, and it'll keep time for you. RTC chips are divided into two categories; those that keep time by counting seconds (usually as a 32 bit number) since some specific start point, and those that count seconds, minutes, hours, days, months, and years.

The DS1307 is of the latter type. This means that it stores the time as seconds : minutes : hours (12 or 24 hour) : day of the week : day of the month : month : year. It also has some smarts so it knows how many days are in each month, and even knows about leap years out to 2100.

This is great, and super useful, but every time I need to pull the chip out of the breadboard to move it, or even just accidentally remove power, it loses the current date and time. Since most of my projects aren't serious enough to warrant wiring up buttons and coding a way to reset the clock, I have to instead plug my Arduino into my laptop with a USB cable, reflash a DS1307 clock setting program I threw together, send it a 13 digit number over the serial port, reflash the firmware for whatever project I'm currently working on, and move forward. Can you see how this would get just a little annoying after the twentieth time? I also have been using the SRAM on the clock to store data, and losing that isn't catastrophic, but annoying as well.

Lucky me, the DS1307 has a neat little feature, where it has an extra pin that you can connect to a Lithium 3V battery, or tie to ground if you're not using it. Sparkfun has a board that has a surface mount DS1307 on top, and a battery clip underneath, which would be useful, but at $20, is a little rich for an inconvenience. I then saw an idea where you do the same thing, just with the DIP package, and all on one side of a piece of perf board.

Parts list
  • Perf board - 5 x ~17 (I used a leftover piece from Radio Shack, but any would do)
  • 5x right angle male header. (sourced locally at Halted, 14 cents) This is so I can turn the board on end and plug the board into my breadboards when I'm done, which is the only place I plan on using this.
  • 1x 8P3 DIP socket (sourced locally, ~30 cents), because soldering ICs hurt my soul.
  • 1x DS1307 RTC (Digikey, $3.74)
  • 1x 12.5pF 32.768kHz crystal (Digikey, 32 cents)
  • 1x CR2032 battery clip (Digikey, 96 cents)
  • 1x CR2032 cell (Digikey, 40 cents, USPS won't ship | Locally, $4.50, I hate you RiteAid)
  • Some wire, solder, soldering iron, etc.
Wiring it up
The DS1307 is nice enough to have 4 of the 5 IO pins on one side (5 - SDA, 6 - SCL, 7 - SQW, 8 - VCC). The fifth pin is for the ground connection, which you need to jump from the other side of the IC with some wire. The battery clip needs to be connected between VBAT and GND, so that'll take at least one piece of wire from the far end. I soldered the crystal straight to the perf board and DIP socket. You should end up with something that looks like this:

Plug in the DS1307 and a CR2032, and you're ready to set the time once, and use until DST.

Results
It works quite well when it's plugged into a project, and I power down everything else. It's super nice not having to try and keep this powered while I'm mucking around with all the other wiring on a board.

I have had a problem when it's just sitting loose. It seems that when VCC isn't pulled to ground, the chip only keeps time half as fast as reality, which makes it less real time, which personally, I find quite offensive. For the second revision, I would probably add a resistor, or even just a bypass cap (0.1μF) between the VCC and GND lines to improve this. While plugged into a project, the rest of the project keeps the VCC line very much at ground, so it's not a very big issue as far as I see it.

Considering that I managed to put this together for easily half of what the Sparkfun board costs, and that I got to break out the soldering iron, which is always welcome, I'd call this project a success.

2 comments:

  1. For my DS1307 project, there is a problem like that when I unplug the power Vcc 5V, the coin cell 3V doesn't work with RTC, I mean it cannot run without Vcc 5V. After we supply 5V, RTC will start count from the time that we set in sketch. So if you have any ideas or solution pls help? all responds would be appreciated !

    ReplyDelete
  2. Does the sketch set the time when it starts? You don't want it to set the time every time the sketch starts because it would cause exactly what you are experiencing. Remove the time setting part from your sketch and use a different sketch to set the time on the DS1307 as a one-off.

    ReplyDelete