Thursday, March 21, 2013

Building Real Time Clock (RTC) module based on DS1307 for Arduino

In this post I’m going to build a Real Time Clock (RTC) module based on popular DS1307 RTC chip for using with Arduino or any other micro-controller platform. DS1307 uses I2C protocol to communicate with the microcontroller.

RTC Module:

This is the circuit we have to build and it has few readily available components. The 3V coil cell battery is used to give backup power to the RTC.  Make sure to solder a 4 pin connector for Vcc,GND,SCL,SDA connections. 1kOhm pull up resistors are used for the I2C bus in the circuit.

DSCF5835_640x480

I got the 32.768kHz Crystal and 3V CR2032 coil cell battery holder from an old computer mother board. I think they are available at any electronic component shop.

DSCF5814_640x480DSCF5823_640x480

After collecting parts I built the circuit on a piece of veroboard. Total cost was around Rs:70 ($0.55). I used 4pin connector for above mentioned connections. If you don’t have a coin cell battery, make sure to short the two battery pins as on the picture,but you won’t have the backup power in case of a power failure.

DSCF5829_640x480

Arduino Board:

I’m using my Techduino Pro board which I build on an earlier post along with the my modified CP2102 USB to UART board. Please note that my Arduino is working at 3.3V.

Connections:

I connected the two boards as follows, You can get Arduino –> ATMEGA328 pinout from here.Even though DS1307 datasheet tells that the operating voltage of the chip is 5V, It worked well even at the 3.3V as I used here. Please be aware of that to avoid unnecessary outcomes.

 

RTC Module Arduino
Vcc +3.3V or +5V (As you need)
GND GND
SDA SDA (Analog Pin 4)
SCL SCL (Analog Pin 5)

DSCF5834_640x480

Coding:

You can just write a code using Wire library. Make sure that DS1307 sends 7 BCD digits containing date & time. I was lazy to reinvent the wheel and opted to use the DS1307 RTC library for the Arduino. Get it from here and extract to the libraries folder in your Arduino IDE folder (Rename it to RTC after extracting).I modified the example file to show how to use it.

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
    Serial.begin(57600); // UART initialize
    Wire.begin();
    RTC.begin();
   // RTC.adjust(DateTime("MAY 21 2013","20:40:45")); //you can use this to set initial date/time as you wish
 
}

void loop () {
    DateTime now = RTC.now();
    
// Sending date & time to UART every second    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
   delay(1000);
}

scrn

4 comments:

  1. can we use the same circuit with pull up resistor as 10k (in place of 1k in the above figure ) for raspberry pi

    ReplyDelete
    Replies
    1. It determines the I2C bus maximum clock speed. I think it will work well. Can't remember the I2C speeds of RTC.Please check the datasheets too. It will work in most cases :) Give it a try ;)

      Delete
  2. Hi, is it necessary to connect the two 1 kOhm resistors? coz i found a link also to interface DS1307 with arduino which he did not connect the resistors.
    Here's the link: http://forum.arduino.cc/index.php?topic=8833.0
    Your help will be greatly appreciated (: Thank you!

    ReplyDelete