Tutorial 16: Arduino Clock

In this tutorial, the Arduino displays the time and date on a LCD (optional) and in the Arduino IDE serial monitor window. A PCF8563 real time clock (RTC) IC is used to generate the time and date.




Arduino clock circuit

The Arduino Clock Circuit on Breadboard

 

28 thoughts on “Tutorial 16: Arduino Clock

  1. Tutorial 16: Arduino Clock. how do i go about using an lcd and push button to set Time and Date instead of the serial monitor window

    • Hi mk
      Look at how a digital watch uses buttons to set the time and date, and then rewrite the sketch to do the same. With the LCD and a few buttons, you will not be able to display all the information as is done to the serial monitor window.

      You will need more than a single push button. Holding in the first push button for a couple of seconds should put the Arduino into edit mode. Pushing the first button after that can move it between number fields. The second push button can be used to increment each field. Holding in the first push button can then be used to set the time, or a third button can do that.

  2. Hello,
    Thank you for the tutorial.
    But if I try it, I get following in the serial monitor:
    Send “s” to set the date and time.
    3?:00:21
    00/01/2007
    The time and date is showed only once and not every second.
    If I try to set the time, the output is completly different.
    For exemple, I set the year to 2013 and I get following:
    Send “s” to set the date and time.
    08:61:13
    07/08/20>8
    Thank you for your help.
    KR,
    Luk

    • Hi Luk
      Are you using the exact same RTC chip and a 32.768kHz crystal?
      When you set the year, only use the last two digits of the year, e.g. for 2013, use 13
      When entering data, be sure to enter it exactly as requested.

      • Hi,
        Thank you for your response.
        I think I use the same chip and crystal.
        Bought them here:
        http://www.benl.ebay.be/itm/300819167847?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2648
        http://www.benl.ebay.be/itm/330811734786?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649
        I read the tutorial very carefully and did averything exactly as shown. I use 2 digits indeed. Same problem with all other inputs (sec., min,…)
        Thank you a lot for your help.
        Kind regards,
        Luk

        • Those parts look like they are the right ones. What version of the Arduino software are you using?
          There is also the possibility that the RTC chip is faulty.

          • Hello,
            I’m using the arduino 1.0.2 software for windows, combined with a arduino nano.
            How could I check if the RTC is faultly? I bought 10 of then, but they all give the same result.
            Kind regards,
            Luk

          • Do you have an Arduino Uno to test the circuit on?
            Something else to test:
            The wires between the RTC chip and Arduino could be picking up interference and corrupting the data. Keep these wires as short as possible. Try to keep them the same length. You can also try twisting them together.

            To test the RTC chip, you would need to write a sketch. If I have some time, I can write a test sketch for you – will see what I can do.

          • Thanks a lot for your answer!
            I’ll try first with the Uno. I thought the Nano would work because the same pins (A4 A5) are normally used for I2C. We’ll see. I’ll keep you informed.
            Kr,
            Luk

    • It makes the address easier to read. If you look at the datasheet for the PCF8563 you will find that it mentions write address as A2h. It might be confusing when reading the sketch long after writing it and seeing 0x51. Of course you could use 0x51 and then just comment it to explain what is going on.

  3. Hi,

    Thanks for the tutorial on how to set up an arduino clock.
    I seem to be getting the same error as luk above and I dont understand as to where it is coming from.

    Send ā€œsā€ to set the date and time.
    3?:00:21
    00/01/2007

    Is there any update on how to resolve this issue?

    Regards,

    Hugh

    • I have just built the first circuit of this tutorial (without battery and LCD) and tested it using an Arduino Uno and have had no problems – it worked first time.

      I tested it on Linux using Arduino software 1.03 and on Windows 7 using Arduino software 1.03

  4. Hi,
    I am using an arduino uno and software version 1.0.3.
    The circuit I have built is identical to the one showen in the example.
    Thank you very much for your time .

    Regards,
    Hugh

    • Hi Hugh

      Try removing the 2k2 resistors, and if that does not work then try replacing them with 10k resistors.

      What revision Arduino Uno board do you have?

  5. Hi,
    Neither Nano nor Uno worked.
    I did test 2 but never the led went one nor flickered…
    I have no multimeter that can measure frequency…
    Kr,
    Luk

    • Hi Luk
      Thanks for your feedback. It is very strange that the capacitor caused the circuit to stop working. The capacitor is just a decoupling capacitor for the power to the IC.

      • Hi,
        I’m really sorry, after going through the circuit again, I saw I wrongly connected the decoupling capacitor…stupid…
        So this means everything is working now, the way you explained in your tutorial.
        I have another question. Is it usefull to put a decoupling capacitor before the LCD screen too? And might a bulk capacitor be usefull?
        Thank you again for this really great tutorial, I enjoyed it a lot!
        Kr,
        Luk

        • Hi Luk
          Glad to hear that you got it working. Decoupling caps are usually placed at every IC and I would normally put one near the LCD too. Bulk caps such as an electrolytic are normally used in a circuit where the circuit is far from the power supply or parts of the circuit are far from the power supply.

  6. Hi,
    I want to thank you again for the excellent tutorial.

    I want to use the RTC in a setup that triggers a relay after a certain delay set. The delay can be between 1 and 59 hours. So the user picks for example 47 hours. After 47 hours from now, the relay will be triggered.
    If now is 30/09/2013 18:22:13 then the relay will switch at 03/10/2013 03:22:13.
    I use an array end_time[7] to store the actual RTC time(now) + delay(47h).
    But how can it be done? I know how to go from BCD to decimal and vice versa, no problem. But, like our example, if the delay changes days, months and maybe years, how can the end_time be defined in a simple way?

    I hope you understand what I’m trying to explain :-s

    Thank’s a lot in advance!
    Grtz,
    Luk

    • Hi Luk

      It would be much easier just to count seconds. If you store your seconds in a 32-bit unsigned integer, then you can count up to 4,294,967,295 seconds, or in hours: 4,294,967,295 / 60 mins / 60 seconds = 1,193,046.471 hours

      In an embedded system, I would normally use a timer on the microcontroller to generate an interrupt every second to keep track of the count. I have not looked into how to do this for the Arduino.

      It may be better just to keep track of minutes if using the RTC – or hours, if you only need hours, then you can use a smaller integer. You then only need to write code that tracks when a minute or hour rolls over.

  7. I was wondering why you need the crystal? The datasheet for the PCF8563 says it ‘contains an on-chip 32.768 kHz oscillator’. Unless I’m misunderstanding what that means, then the use of the crystal seems unnecessary. I’m pretty new to electronics so I am probably misunderstanding something.

    Thanks,
    Charlie

      • I always thought that the crystal and oscillator were parts of a single component. I guess I never really thought about what exactly an oscillator consists of. I’m guessing the crystal simply ‘vibrates’and the oscillator interprets the vibration?

        Thanks,
        Charlie

Leave a Reply

Your email address will not be published. Required fields are marked *