Let’s add a DS1307 real-time clock to our Freetronics Arduino-compatible board.
Updated 18/03/2013 – this is also perfect for the Freetronics Eleven board.
Now and again I find myself making another kind of clock or timing device using the Arduino system, and each one has been making use of the Maxim DS1307 real-time clock IC. However every time another clock is being worked on, my DS1307 real-time clock shield needs to come out to play. Although in itself it is a nice shield, at the end of the day – the less you have the better. Originally I used a Freetronics TwentyTen board – which has now been superseded by their Eleven board, however they’re both identical for the purposes of this tutorial.
So what to do? As regular readers will know, my preferred board is the Freetronics Eleven, and within this we have a solution to the following problem:
Image may be NSFW.
Clik here to view.
The Freetronics team have thoughtfully provided a prototyping area in their board – and that will be a perfect home for the real time clock system. Being a cheapskate and a masochist – instead of following others by using a smaller RTC module I will instead use parts already in stock (except for the battery) and install my own circuit. So, as always – we need a plan. The circuit itself is quite simple, the DS1307 data sheet has a fine example on page thirteen, and here is my interpretation:
Image may be NSFW.
Clik here to view.
So the parts required for our clock circuit will be:
- IC1 – Maxim DS1307 I2C real-time clock IC
- 8-pin IC socket
- R1~R3 – 10k ohm 1% metal film resistors
- X1 – 32.768 kHz crystal
- B1 – Panasonic CR1220 3v battery with solder pins (Farnell part number 1298944) [data sheet one and two]
- One header pin (from those 40-way strips)
- some thin black single-core wire
The CR1220 battery was chosen over the usual CR2032 due to the smaller diameter. According to the DS1307 data sheet, the battery should last around ten years if it has a capacity of 48 mAh. Our CR1220 is 35 mAh – which will do nicely, perhaps seven years or so. That will have to do. Don’t forget to check the voltage of the battery before installation – it should be just over three volts.
Now to get everything arranged in the prototyping area. When doing this it pays to always have the schematic in front of you as well so you can refer to it when necessary. Planning to use protoboard of any size requires a good plan as well. After spending some time considering component placement, the final layout was as follows:
Image may be NSFW.
Clik here to view.
Each square on the grid represents one hole on the board. After you see the images below, everything will make sense. Before soldering away, it will pay to give the prototyping area a quick clean with some PCB cleaner.
Now it is finally time to get soldering. The first items were the battery, crystal and the resistors. Although the battery was designed to be soldered, I am always a little wary when applying heat to them. Two seconds with the hot iron was enough.
When soldering in the crystal (or anything else), try to keep in mind what the leads will be connecting to. For example, the crystal legs will need to connect to pins 1 and 2 of the IC socket. So bend the crystal leads in the direction of the respective IC socket pins. Doing so will make creating solder joins between them much easier:
Image may be NSFW.
Clik here to view.
The resistors were simple enough. Keep the excess clippings to make jumpers with later. Also notice how the right hand leg of R3 was bent around and brought back up to the top row – this is to help make connections with the 5V rail link:
Image may be NSFW.
Clik here to view.
The next item was the IC socket. Nothing to worry about there, just drop it in and solder away. Don’t forget to bridge the crystal pins to socket pins one and two, and the battery positive pin to IC socket pin three.
Next for the SQW pin. The DS1307 can also output a nice square wave at either 1Hz, 4.096 kHz, 8.192 kHz or 32.768 kHz, with the resulting signal being found on pin 7. It isn’t something really used that often, but you never know. So I soldered in one of these pins, which should make it easy enough to use later on:
Image may be NSFW.
Clik here to view.
Note that if you are using the SQW function, the DS1307 will merrily pulse away once it is set, until the power is cut – the square-wave generator is autonomous to the I2C bus once it has been set. And it remembers (as long as the backup battery is fine). For example, you can upload a sketch to set the SQW to 4.096 kHz, remove power, yank out the ATmega328, power up – and the SQW is still active.
Next we turn the board over, and solder in our jumper wires:
Image may be NSFW.
Clik here to view.
The lead on the top runs from the right-hand side of the pull-up resistors R1~R3 (when facing the top of the board) to the 5V pad. The bottom lead runs from pin four of the IC socket to the GND pad. The negative pin of the battery is also bent over and soldered to the GND pad. Also, connect all the resistors together as shown in the above image (below the TX pin). The next step is turn the board back over and make some more wired connections, the first being pin eight of the IC socket to the resistors and then to the 5V link on the rear:
Image may be NSFW.
Clik here to view.
The next are somewhat longer, they are the leads for the I2C bus. Run a wire from next to IC socket pin six all the way to (and through) the bottom-right hole of the TwentyTen (when facing the top); this will be the SCL line and soldered to analogue 5. Repeat again from IC socket pin five, this is the SDA line (as above) for analogue 4. The joints you have to solder them onto are not that large, however it can be done. Before soldering the wires in, heat up the existing joint to melting point then let it cool again – this makes actually soldering the wire in a lot easier:
Image may be NSFW.
Clik here to view.
And there we have it. At this stage, don’t plug the board in. Do some quality control: check that the soldered joints are complete; check that solder has bridged where you need it, and not where you don’t; use the continuity function (‘beeper’) of a multimeter to spot-check for shorts, and also follow the new 5V and GND lines to ensure they are connected correctly. And finally, insert the DS1307 IC into the socket.
Image may be NSFW.
Clik here to view.
OK – now for some test timing. If you have not worked with the DS1307 IC before, there is a full explanation of how it works within our Arduino tutorials. Here’s a sketch you can use to test the real-time clock. Once you have uploaded that sketch, open the serial monitor box at 9600 bps, and you should have something like this:
Image may be NSFW.
Clik here to view.Now let's check the 1 Hz output from the SQW pin:
Recall that you can generate four frequencies with your DS1307, here is an example sketch that does just that:
and here is the result – measured on a freqency counter:
My frequency counter is around twenty-two years old, please be patient with it as the sampling rate is not the best.
Image may be NSFW.
Clik here to view.
In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitter, Google+, subscribe for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other – and we can all learn something.
The post Add a real-time clock to the Freetronics Eleven appeared first on tronixstuff.