Home     Contact     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
 Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   

9 June 2011
Author: Giorgos Lazaridis
LCD PIC Clock with the Maxim DS1307 RTC

The circuit assembled on a breadboard for test

A friend of mine asked me to design a home automation for him. He wants me to build something that will communicate with his air condition through an IR receiver that it has, and it will act as a programmable timer, to turn it on and off automatically. Then i thought that i should spice it up a little bit, and make a programmable IR universal timer, or something. No matter what, i will certainly need to interface an RTC (stands for Real Time Clock) chip. This chip is responsible for keeping the time.

This circuit will help you understand how to use the built-in I2C communication module of PIC microcontrollers, and also how to use the Maxim DS1307 Real Time Clock chip.

The Maxim DS1307 is one very good solution

Being a PIC circuit designer has one great advantage over using arduino or other development platforms: It gives you the ability to learn how to choose the right chip for your job. The greater the experience i get, the more the parameters i include in my search criteria, parameters like price, package size, functionality, extra features and more. This time, i decided to use the DS1307 RTC chip from Maxim.

The chip uses I2C protocol to communicate with the microcontroller. I will use the PIC 16F1937, because it has enough pins for my future project and it has a built in I2C module, the MSSP. This is a remarkable feature for this microcontroller. The module can be configured to operate with 7 or 10 bit addressable clients. All the hard job is done by hardware, which makes the assembly sheet much more smaller and simpler to follow.

The DS1307 has an extra power pin to connect a 3V coin battery. This provides power to the chip whenever the main power supply is off. The consumption in that case is as low as 500 nA. According to the manufacturer, a lithium battery with 48mAh will back up the chip for more than 10 years in the absence of power!

One thing that has to be taken into account is the crystal. The chip uses an external 32.768kHz crystal for the oscillator. When choosing one to buy, you must choose one with Load Capacitance 12.5 pF. Other value than that may result in faster or slower pulses. Choosing for example a crystal with load capacitance 6pF causes the RTC to be about 3 to 4 minutes per month faster.

Another nice feature with this chip is that it provides 56x8 extra RAM space for data saving. Each time/date set has 7 bytes of data, so you can store 8 full time/date sets (8x7=56). You can store for example alarm notifications.

The Circuit

The circuit is very simple. Click the following schematic to enlarge it:

The circuit with a 7-buttons keypad attached

The LCD occupies pins 21 through 28, except pin 23. This pin (RC4/SDI/SDA/T1G/SEG11) along with pin 18 (SEG6/SCK/SCL/RC3) are used to interface the DS1307 chip. The PIC 16F1937 has a built in I2C hardware module. This module will do all the dirty work to communicate with other chips using I2C protocol. I do not need to use any external library, as the connection is done by hardware.

I added a big 1000uF capacitor in parallel with the battery back-up for only one reason. This capacitor will act as a back-up-back-up power if the main power is turned off and the battery is removed. The capacitor holds enough current to keep the RTC running for some time, even without the coin cell batter.

As mentioned before, the crystal must have 12.5 pF load capacitance to match the internal capacitance of the chip. Otherwise there will be an unwanted timing offset.

There are 7 push buttons to set the time and date. The first 3, are to set the date (day month year), while the other 3 are to set the time (hour min sec). Each time a button is pressed, the value is increased. If the seventh button is pressed simultaneously, then each time a button is pressed the value is decreased.

Before you get a headache - Your clock may run fast or slow!

There is one rule when you start playing with a new chip: Read the datasheet (RTFM)! STUDY the datasheet i might say. A detail may cause you a headache. First of all, something regarding the backup battery. The battery MUST be included in the circuit, otherwise it will not operate normally. If you do not wish to use a backup battery, then you have to ground the VBAT pin.

Another problem that will occur when you prototype this circuit on a breadboard, is that it will probably run fast. Every 5 to 10 minutes, your clock will be 1 to 2 seconds faster than the real time. That is absolutely normal. The reason? Parasitic pulses on the crystal. If you wish to make this circuit and you also wish to be accurate enough, then you HAVE to follow the guidelines of this document:

Application note 58 - Crystal Considerations with Maxim Real-Time Clocks (RTCs)

There is one nice test to see if your circuit is ok, even if it is built on a breadboard. First synchronize it with a clock that you know it runs accurate (NOT WITH YOUR PC CLOCK!!!). Then, remove the external power supply completely, and let the circuit run on the backup batter for a couple of hours. Re-connect the external power supply and see if there is any difference between the 2 clocks. With this trick, you eliminate the parasitic pulses from the cables on the breadboard.

The PIC software

Here are all the files for this project:

 LCD PIC Clock with the Maxim DS1307 RTC - Assembly listing - V1.0

 LCD PIC Clock with the Maxim DS1307 RTC- Hex file - V1.0

Bill Of Materials
R1Resistor 1.5 KOhm 1/4 Watt 5% Carbon Film 
R2Resistor 10 Ohm 1/4 Watt 5% Carbon Film 
R3Resistor 10 KOhm 1/4 Watt 5% Carbon Film 
R4Resistor 4.7 KOhm 1/4 Watt 5% Carbon Film 
R5Resistor 4.7 KOhm 1/4 Watt 5% Carbon Film 
C1Electrolytic Capacitor 1 uF 50 Volts
C2Electrolytic Capacitor 1000 uF 16 Volts
IC1PIC 16F1937 Microcontroller 
IC2DS1307 64 x 8 Serial I2C Real-Time Clock 
Q132.768KHz Crystal 32.768KHz Crystal with 12.5 pF Load Capacitance



  Email (shall not be published)


Notify me of new posts via email

Write your comments below:
BEFORE you post a comment:You are welcome to comment for corrections and suggestions on this page. But if you have questions please use the forum instead to post it. Thank you.


  • At 29 March 2016, 2:35:38 user Ajin. A wrote:   [reply @ Ajin. A]
    • Sir, i want a prgm to give pulse at 7.00 to 7.30 pm using rtc ds 1307 every day for pic16f877a. Pls publish a prgm in aajin1998@gmail.com immediately for our project.

  • At 4 March 2016, 19:22:59 user PALLAVI wrote:   [reply @ PALLAVI]
    • Hello Sir
      I want this project hex for PIC18F4550.Will you send me plz at my gmail: pallavi2629594@gmail.com
      Thank u...

  • At 18 December 2015, 23:59:32 user Yasir Ali wrote:   [reply @ Yasir Ali]
    • Hello Sir
      You do very nice project sir i want this project hex in 12 hour format can u send me this project hex in 12 hour my email yasiralikhi2@gmail.com

  • At 6 May 2014, 19:30:08 user kreso wrote:   [reply @ kreso]
    • hi sir
      I'm doing this project by instructions on this page,and I came across a problem with LCD display, it shows only 16 black boxes and blinking indicator, was wondering if you could help me to solve this problem
      thanks in advance

  • At 22 March 2014, 16:04:21 user Giorgos Lazaridis wrote:   [reply @ Giorgos Lazaridis]
    • @ANIL HOOSERIA I've never test mplab-x actually...

  • At 1 March 2014, 16:31:40 user ANIL HOOSERIA wrote:   [reply @ ANIL HOOSERIA]
    • Hi Giorgos
      An excellent project, very Kind of you for sharing.
      My intention was to study the detail and incorporate into my home controller project.
      Everything works fine from the hex point of view.

      I've managed to get the assembly code to build, however the hex file comes out less than quarter the size of your hex file, with no results, Using Mplab x.

      is it possible to get a build file on x or mplab ide from you, or could i trouble you to see my build file and point out my errors.

      I intend converting to GLCD 128x64 with multiple controller inputs. First though, I need to start working on a successful build of your assembly detail.

      Much appreciated.

      Anil Hooseria

  • At 4 October 2013, 12:39:24 user Giorgos Lazaridis wrote:   [reply @ Giorgos Lazaridis]
    • @aira No that will not work

  • At 4 October 2013, 11:51:43 user aira wrote:   [reply @ aira]
    • hi sir, can i use your command w/ pic16f877a?

  • At 24 June 2013, 10:48:17 user Giorgos Lazaridis wrote:   [reply @ Giorgos Lazaridis]
    • @Naresh Sharma I'm sorry but i do not do circuits on demand

  • At 24 June 2013, 8:44:52 user Naresh Sharma wrote:   [reply @ Naresh Sharma]
    • The idea given by you is really good.. I am very much interested in doing this project. But instead of 16X2 LCD I want to use LED. Can you send the code for the same. It would be a great help to me.
      Thanks in anticipation!

  • At 13 December 2012, 10:16:53 user KnosePIC wrote:   [reply @ KnosePIC]
    • The 1000 µF capacitor is just for tinkering. Can't leave it there over the long term. it has 5-10 µA leakage current, so it sucks the the battery dry in 6 months, instead of the '10 years.' btw, 10 years supposes that the battery loses nothing as it ages. but from the geek department, the batteries lose everything in 5-7-10 years just sitting on the shelf, depending on how well made they are. So the lifetime will be about 6 years typical to keep running the oscillators (keep the accurate time) in the chip's registers. With a leakless battery, after the oscillators exhaust the battery in 10 years, there will still be enough to keep the last time stored for an additional 10 years beyond the oscillator circuit going kaput!

  • At 10 June 2012, 16:52:45 user Giorgos Lazaridis wrote:   [reply @ Giorgos Lazaridis]
    • @Terex-Demag Yes you are right, it is just a demo though for the RTS interface

  • At 10 June 2012, 12:47:01 user Terex-Demag wrote:   [reply @ Terex-Demag]
    • Very good project. In my opinion you use too many switches to control.

  • At 9 April 2012, 22:16:09 user talal wrote:   [reply @ talal]
    • Hi

      for LCD, IC2 S1307 64 x 8 Serial I2C Real-Time Clock and IC1 PIC 16F1937 Microcontroller which is the Pins connecting

  • At 20 January 2012, 17:06:50 user Kammenos wrote:   [reply @ Kammenos]
    • @Pete Goss as a matter of fact, you can use any PIC for this application. The RTC uses I2C for the interface, so a PIC with harware I2C will certainly make your life much easier, otherwise you will have to make the interface in software. PIC16F877 has I2C module (MSSP) so it is a good choice.You may need to alter the code though. Read my code first to get an idea how to setup the module.

  • At 18 January 2012, 23:36:40 user Pete Goss wrote:   [reply @ Pete Goss]
    • @Kammenos. Thanks for your reply. Do you think this can be done with the PIC16F877? My PIC2 can't program the 1937. I'm very new to micro-programming, so it may take me a while. Thanks for the info about matrices. I'm going to use a 3 bit to 8 line decoder (74138) for the 7 days and 4 bits for the 4 times to create a 7 X 4 LED matrix.
      Thanks again

  • At 17 January 2012, 21:07:44 user Kammenos wrote:   [reply @ Kammenos]
    • @Pete Goss the RTC is the best solution you can use for your application indeed, although it can be kinda tricky to impliment. If i were you, i would definitely go with RTC/microcontroller.

      As for the outputs, usually we multiplex the LED outputs. Check out these:

  • At 16 January 2012, 22:53:29 user Pete Goss wrote:   [reply @ Pete Goss]
    • I would like to know if you think this approach of using the RTC chip and a PIC would be suitable for a project I want to construct. An elderly relative has problems taking the correct dosage of medication, The pharmacist can supply a transparent tray with each dosage put into 28 seperate compartments. 4 for each day. The relative always get it wrong. I want some kind of clock circuit that will light up one of 28 LED's placed beneath each compartment of the tray. This would indicate which compartment she should open up. Also a buzzer would sound at the correct time. I'm think that I need a circuit that I could program to to turn on the correct LED every day at 9:00 - 13:00 - 17:00 and 21:00.
      Do you think your approach could do this? Are there enough outputs from the PIC to drive 28 LED's?

  • At 5 October 2011, 18:20:10 user Kammenos wrote:   [reply @ Kammenos]
    • @supra i do not have the code in C

  • At 5 October 2011, 1:10:39 user supra wrote:   [reply @ supra]
    • Can u send me mikroC source code? The mplab can converted to high level languages. It is in ur mplab's folder

  • At 21 July 2011, 14:37:30 user Kammenos wrote:   [reply @ Kammenos]
    • @supra yes

  • At 21 July 2011, 13:07:59 user supra wrote:   [reply @ supra]
    • What's compiler r u using? It is mplab?

  • At 13 June 2011, 17:03:46 user Kammenos wrote:   [reply @ Kammenos]
    • @Fung the button next to the PIC is the reset. I have it there for prototyping to test the code.
      As for the seconds button, you can change the code to do whatever you like.

  • At 13 June 2011, 15:04:31 user Fung wrote:   [reply @ Fung]
    • What is the purpose of the push button that next to the PIC? I found no descriptions about it.

      Typically, the button for secondhands is used to reset the count to "00", same as the watches. But you make it to increase or decrease by 1 second, why?

    reddit this Reddit this

     HOT in heaven!

    NEW in heaven!

    New Theory: AC electric motor working principle

     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS   

    Site design: Giorgos Lazaridis
    © Copyright 2008
    Please read the Terms of services and the Privacy policy