Let’s make an Arduino real time clock shield
Hello fellow Arduidans
[update - You may also find this post interesting, we add the DS1307 circuit directly to a Freetronics TwentyTen Arduino-compatible board]
Today we are going to make a real time clock Arduino shield. If you have been following my tutorials, in weeks 7, 8 and onwards we have been making use of the Maxim DS1307 real time clock chip. Although it is a very interesting part to use, implementing it has not been so easy, therefore the reason for this shield. So let’s go!
First of all, we need create our circuit diagram. Thankfully the Maxim DS1307 data sheet [pdf] has this basics laid out on page one. From examining a DS1307 board used in the past, the pull-up resistors used were 10k ohm metal films, so I’m sticking with that value. The crystal to use is 32.768 kHz, and thankfully Maxim have written about that as well in their application notes [pdf], even specifying which model to use. Phew!
So here is the circuit diagram we will follow (click on it to enlarge):
Which gives us the following shopping list:
- One arduino protoshield pack. I like the yellow ones from Freetronics, however others may prefer this one
- X1 – 32.768 kHz crystal – Citizen America part CFS206. You should probably order a few of these, I broke my first one very quickly…
- IC1 – Maxim DS1307 real time clock IC
- 8-pin IC socket
- CR2032 3v battery
- CR2032 PCB mount socket
- R1~R3 – 10k ohm metal film resistors
- C1 – 0.1 uF ceramic capacitor
And here are our parts, ready for action:
The first thing to do is create the circuit on a solderless breadboard. It is much easier to troubleshoot possible issues before soldering the circuit together. Here is the messy test:
Messy or not, it worked. Instead of writing another sketch, the example 7.3 from arduino tutorial seven was used. Here is a copy: example 7.3.pdf.
The next step is to consider the component placement and wiring for the protoshield. Please note that my board will most likely be different to yours, so please follow the schematic and not my board positioning. Try not to rush this step, and triple-check your layout against the schematic. As my protoshield has a green and red LED as well, I have wired the square-wave output to the green LED. You can never have too many blinking lights…
At this point I celebrated the union of tea and a biscuit. After returning to the desk, I checked the layout once more, and planned the solder bridges. All set – it was time to solder up. If you have the battery in the holder for some reason, you should remove it now, as they do not like getting warm. Furthermore, that crystal is very fragile, so please solder it in quickly.
And here we are – all soldering done except for the header sockets. At this point I used the continuity function of the multimeter to check the solder joints and make sure nothing was wrong with the circuit.
Final checks passed, so on with the headers. To make this easier, I stick some header pins in the sockets, then place the whole lot in a solderless breadboard to keep it straight. Well, it works for me:
Just a side note – always make sure you have enough consumables, the right tools, etc., before you start a project. This is how much solder I had left afterwards…
Moving on … in with the battery and the DS1307 – we’re done!
It is now time for the moment of truth – to insert the USB cable and re-run the sketch… and it worked! The blinking LED was too bright for me, so I de-soldered the wire. If you are making a shield, congratulations to you if yours worked as well. If it did not, don’t be afraid to hit me up via email or our Google Group with your questions. Note that if you are using this shield, you cannot use analog pins 4 and 5 – they are being used as the I2C bus. Time to clear up the desk and wash my hands.
Now to put this shield to work. Last week we made an LCD module shield – so let’s pile up the shields and make a digital clock. We can re-use the sketch from arduino tutorial example 7.4, with the liquidcrystal() corrected to use the LCD shield pins. Here is the modified sketch: ds1307shielddemo.pdf.
And my post wouldn’t be complete without a video, so here are our new shields in action!
So there we have it. Another useful shield, and proof that the Arduino system makes learning easy and fun. High resolution photos are available on flickr. If you have any questions or comments, please leave them below, or consider joining our Google Group!
As always, thank you for reading and I look forward to your comments and so on. Please subscribe using one of the methods at the top-right of this web page to receive updates on new posts!














John, great tutorial. Lack of an RTC in most micocontrollers has always bugged me.
It looks like there would be room on that shield for a SD card reader. It would make a nice data acquisition solution (e.g., tracking temperature during the day and recording time/temp on the SD).
Hello Jeff
Thank you for your kind words. You are right about the SD card reader, but doing so is a little bit outside of my expertise (at this stage!). One of the reasons for my blog is to teach myself something, then try to explain it to others, which helps me to learn more effectively. Perhaps I am a masochist, trying to reinvent my own wheels – but it’s fun and I’m learning.
With regards to your suggestion the genius ladyada has beat us both to it – http://www.adafruit.com/index.php?main_page=product_info&cPath=17_21&products_id=243
Take care
John
The link to the schematic enlargement is broken. Is the schematic now correct? (pull up resistor)
Thanks!
-Migs
Howdy Migs
Sorry about that, all fixed now. The image that is there now is the correct circuit diagram. You can match it against the DS1307 data sheet, http://www.maxim-ic.com/datasheet/index.mvp/id/2688.
Enjoy the weekend
John
hi, Is it possible with this project to turn a lamp on or off?
Hello
Yes you can… there are several ways. If your AC is 110V the easiest way would be with a powerswitch tail from adafruit. Otherwise there is a good project at http://www.practicalarduino.com that will help you as well.
Cheers
John
Please suggest a Arduino schematic for each of the following circuits:
A broken beam between an IR LED & a photodector turns an LED ON
A broken beam between an IR LED & a photodector turns an LED OFF
Please suggest component part nr and resistor values
Greeting from Jeff and Denmark
Hello
Here is someone who is working on IR beam – you could modify their work perhaps.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1222684079
cheers
john
hello to everyone !
can someone help me with the c source code of a real ds1307 based clock with leds ?
i will be very greatful. thanks
hello,
is it possible to used arduino uno in this project?
Yes
Hello, I am trying to make the clock using RTC DS1307, max7219 & a bunch of 7 segment LEDs & am using your code for it.. For some reason, the time just hangs up at the time that i have written in the program.. (there is no “ticking” of the clock.). The comments on that page (http://tronixstuff.wordpress.com/2010/07/09/review-maxim-max7219-led-display-driver-ic/) is closed, so am using this page (apologies).
Can you please help me on what exactly is going wrong? Is there anything else I need to change in the program except for “setDateDs1307…”?
You need to run the sketch twice, first remove the “//” from before setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year); then upload the sketch. Immediately after this, add the “//” and upload the sketch. This ensures that the time doesn’t revert to the initial values when the sketch runs again. For some reason a whole bunch of people were arguing with the comments on that article so I shut them down.
Thanks for the speedy reply!! it was the ground wire from the RTC that was bothering.. Alls just fine now..
n the RTC is just rocking!! tick tick tick.. thanks for the wonderful tutorial!
Thanks for the article.
Where do I set the ports if I don’t want to use A4 and A5?
Just realized it’s connecting to SDA and SCL on the micro…