Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.

Tutorial 16: Arduino Clock

Created on: 6 August 2012

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.

The time and date can be set using the Arduino serial monitor window. An optional battery can be used to back up the time and date settings in the real time clock chip so that the time and date are not lost if the Arduino power is unplugged.

Can't see the video? View on YouTube →

Prerequisites

Preferably you should follow all the beginner tutorials in order, but as a minimum: if using a LCD display, complete Tutorial 12: Arduino LCD. You should know how to use an 8 pin IC, e.g. from tutorial 5.

Read about crystals and coin batteries.

Components

Arduino Uno board, USB cable, wire links and a breadboard.

To just build the clock circuit you will need:

Qty Part Designator Notes Type
2 2k2 resistors (red - red - red) R1, R2 1/4W 5% or better Resistors
1 100n C1 Non-polarized Capacitor
1 32.768kHz crystal X1 32.768kHz watch crystal Crystal
1 PCF8563P U1 Real time clock IC Semiconductor

Books that may interest you:

C Programming with Arduino Book Ultimate Arduino MEGA 2560 Hardware Manual Ultimage Arduino Uno Hardware Manual
Watch crystals
32.768kHz Crystals

To add the battery backup for the real time clock IC, you will need:

Qty Part Designator Notes Type
2 1N4148 D1, D2 1N4148 diodes Semiconductor
1 CR2032 coin battery holder BT1 Battery holder
1 3V CR2032 battery BT1 Battery

To use a LCD to display the time and date, you will need:

Qty Part Designator Notes Type
1 47 ohm resistor (yellow - violet - black) 1/4W 5% or better Resistor
1 10k potentiometer Trimpot or panel mount Potentiometer
1 MSC-C162DYLY-4N (Truly), PC1602LRS-FWA-B (Powertip) or similar HD44780 compatible LCD 16 character by 2 line LCD display LCD

Circuit Diagram

Two circuit diagrams are shown. The first shows only the RTC chip (PCF8563) connected to the Arduino. The second circuit diagram shows the optional battery backup added. To add the LCD, follow tutorial 12.

Arduino Clock Circuit
Arduino Clock Circuit
Arduino Clock Circuit with Battery Backup
Arduino Clock Circuit with Battery Backup

Building the Circuit

First interface the PCF8563 to the Arduino as shown in the first circuit diagram above. The PCF8563 is packaged in an 8 pin DIP (Dual In-line Package). The circuit can then be tested using the Arduino IDE serial monitor window as explained in the sections below.

Interface the LCD next as per Tutorial 12: Arduino LCD. Use the same circuit diagram and connections for the LCD as shown in the tutorial.

The battery backup circuit can be added at any time.

The complete breadboard circuit with LCD and battery backup is shown below. Click the picture for a bigger image.

The Arduino clock circuit built on breadboard
Arduino Clock Circuit Built on a Breadboard

Programming the Arduino

The clock sketch is too long to list here, so download the clock.zip file that contains the Arduino sketch for this project. Copy the clock folder from the zipped file to your Arduino sketchbook folder. Alternatively, copy the text from the enclosed file and paste it into the Arduino IDE. Verify and upload the clock sketch to the Arduino.

Operating the Circuit

If a LCD is interfaced to the Arduino and the clock sketch is loaded, the time and date should be displayed on the LCD immediately. Open the Arduino serial monitor window to see the time and date displayed and updated every second. The time and date can be set as described below.

Changing the Time and Date

When the Arduino serial monitor window is open, the time and date can be set by sending 's' to the Arduino. The image below shows the menu that is displayed in the serial monitor window after sending the 's' character.

Changing the RTC time in the serial monitor window
Changing the RTC Time in the Serial Monitor Window

After the menu is showing, select what you want to change from the menu. E.g. to change the hour value of the time, send '4' and you will be prompted to send the new time. Always send two numbers for any setting, i.e. don't send 2 to set the hour to 2, send 02. Change as many parameters as you want and then send '7' to write all the changes in one go.

The video below demonstrates changing the year parameter of the date.

Can't see the video? View on YouTube →

Fault Finding

Some people are experiencing problems with this tutorial. This tutorial seems to work on some hardware, but not on others – this problem still needs to be resolved.

If you are having trouble with getting the sketch to work then go to the Testing the PCF8563 Real Time Clock IC using Arduino article to do some testing / fault finding. Go to the Blog entry for this article for help and to report any problems.

As an Amazon Associate I earn from qualifying purchases: