RTC DS1307 HowTo

Written by Federico E. Galli

An RTC (Real Time Clock) is basically a clock. If you need to calculate exactly the time and if you need to know the current hour, you need an RTC.

The goal of this howto is the creation of a board pcb RTC, to use with your favourite microcontroller. I have chosen Arduino.
The circuit is based on the DS1307 ic, produced by Maxim-IC: it works with the i2c bus(using just two ports on Arduino) and it saves the time with the help af a CR3032 battery. It needs few components to be used and it's simple to program it.

Components needed in this how-to:
1x rtc DS1307
1x Cristal 32.768kHz , 12.5pF
1x Battery 3v. Choose a CR2032 with battery holder
1x Cap, needed for Vcc filtering: 10uF or 22uF is ok.
1x Breadboard

Optionals but needed to print a professional PCB (follow the l'howto "How to print your own pcb"):
1x Socket for ICs 8pin
1x Strip of male pins

DS1307 Eagle Scheme

Connect the components on the breadboard as described:

  • Pin 1 and Pin 2 from the DS1307 to the quartz. The Pin1 of an IC as the DS1307 is usually marked with a circle. The quartz has no "direction", that means it can go in 'either way'.
  • Pin 3 to the positive of the 3volt battery. Negative is connected to Pin 4
  • Pin 4 to GND on Arduino
  • Pin 5 to analog pin 4 (A4) on Arduino (SDA)
  • Pin 6 to analog pin 5 (A5) on Arduino (SCL)
  • Pin 7, unconnected
  • Pin 8 to +5volt on Arduino

Double check every connection and power on Arduino.

If you haven't already downloaded the source code, schemes and projects, get them from my repository on github

Now, with the Arduino IDE, load the example sketch ds1307_rtc.pde. You can find it into the file previously downloaded. (Unzip the directory under libraries into the arduino folder or in the sketchbook folder).

nb: the ds1307_rtc.pde file is located into the package downloaded from github, you can find a link also at the bottom of this page

Open the Arduino IDE serial console et voilà, if we see dates and hours (obviously not the current one) our circuis was well assembled and we can move ahead. If it's not working, double check again every connection.

This sketch allows to debug the time and to sync our RTC with the computer time.

Now, let's sync: create a file named set_rtc.py and past this code:

import serial
import os,sys,time
ser=serial.Serial("/dev/ttyUSB0",9600,timeout=1)
ser.write(time.strftime('%Y-%m-%d-%H-%M-%S-%w-',time.localtime()))
ser.close()

selecting first the right device in place of /dev/ttyUSB0 (this is the arduino serial port). The code set_rtc.py is written for linux, dependencies are python and pyserial. Run "python set_rtc.py" while leaving opened the serial windows in the Arduino IDE.

We should notice that our RTC is now writing the current hour, read from the computer. Switch off and unconnect the circuit and, if it's installed the battery, we can use it in every project that needs an RTC!
Looking for a more stable system? Print a PCB!

This is a picture of the finished product (Board and components). Follow the "how to print a pcb board" if you are not an expert (Under Construction)

rtc-front.jpg

rtc-battery.jpg

rtc-back.jpg

Source Files and Schemes
blog comments powered by Disqus