Wednesday, 20 April 2016 23:41

Arduino based LCD Digital Clock

This is an Arduino based digital Clock to display real time on a 16x2 LCD using a DS1307RTC module. The date and time can be set via serial interface or push button switches.


  • Shows date and time on serial monitor.
  • Shows date and time on a mini LCD.
  • Alarm clock - turn ON or turn OFF an IO pin.
  • Settings can be set via serial interface or push button switches.
  • Keeps time running after electricity failure.
  • Using an Arduino UNO & a LCD Keypard Shield.
  • Using a DS1307 RTC module.


Software Library

You need to download and install the following libraries before you can upload sketch to your Arduino.

  • Time.h
    The Time library adds timekeeping functionality to Arduino with or without external timekeeping hardware. This library is often used together with TimeAlarms and DS1307RTC.

  • DS1307RTC.h
    Allows you to access real time clock (RTC) chips compatible with the DS1307.

Time Library

The Time library includes functions that allows a sketch to get the time and date very convenient. It also provides time as a standard C time_t so elapsed times can be easily calculated and time values shared across different platforms.

The time_t is a data type for storing system time values, it returns the time since 00:00:00 GMT, Jan. 1, 1970 measured in seconds.

The following code shows how to extract years, months, days, hours, minutes and seconds as integer values from a time_t value.

time_t t; //defince t variable as time_t data type
t = now(); // Store the current time in time variable t
int yyyy = year(t);
byte mm = month(t);
byte dd= day(t);
byte hh = hour(t);
byte nn = minute(t);
byte ss = second(t);


To set the date and time, we use

setTime(t); // set a date & time to the give time t


or another way to set the time

 setTime(hh, mm, ss, dd, mm, yyyy);


You cannot write a value (hour; minute; second; etc.) to the time_t variable directly, however you can use the tmElements_t to change the values of time_t variable indirectly.

tmElements_t (time elements) is a structure that allows access directly to date & time like years, months, days, hours, minutes and seconds. Example below shows how to write values to tmElements_t.

tmElements_t tm;
tm.Year = yyyy;
tm.Month = mm;
tm.Day = dd;
tm.Hour = hh;
tm.Minute = nn;
tm.Second = ss;


After the tmElements_t has the correct date and time stored on it, you are ready to assign its values (integer and byte) to time_t variable.

t = makeTime(tm); //t variable has the values same as tm

makeTime() is a function which takes tmElements_t (time elements) as input and outputs time_t.


The Time library also includes __DATE__ and __TIME__ string which allows you to get the compile date and time.

  • __DATE__ 
    The date (Mmm dd yyyy) when the source file was compiled, eg. Jan 12 2015
  • __TIME__ 
    The time (hh:mm:ss) when the source file was compiled, eg. 12:34:56


DS1307RTC Library

The DS1307RTC library includes functions that allows a sketch to access real time clock (RTC) such as DS1337 & DS3231 very convenient.

Basic Usage

  • RTC.get();
    Reads the current date & time as a 32 bit "time_t" number. Zero is returned if the DS1307 is not running or does not respond.
  • RTC.set(t);
    Sets the date & time, using a 32 bit "time_t" number. Returns true for success, or false if any error occurs.
    Read the current date & time as TimeElements variable. See the Time library for TimeElements details. Returns true on success, or false if the time could not be read.
  • RTC.write(tm);
    Sets the date & time, using a TimeElements variable. Returns true for
  • RTC.chipPresent();
    Returns true if a DS1307 compatible chip was present after using the 4 functions. If an error occurs, this can be used to distinguish between a DS1307 that is not running vs no chip connected at all.


LCD Keypad Shield

LCD KeyPad Shield

The LCD Keypad shield includes 6 momentary push buttons and  a blue backlight LCD which can display two row of 16 characters. The buttons are connected to only one analog input (A0) in order to saving on input/output pins.

Pin Allocation

Pin Function
Analog 0 Button (Select, Up, Right, Down and Left)
Digital 4 DB4
Digital 5 DB5
Digital 6 DB6
Digital 7 DB7
Digital 8 RS (Data or Signal Display Selection)
Digital 9 Enable
Digital 10 Backlit Control


DS1307RTC Module

DS1307RTC Module

The DS1307 serial real-time clock (RTC) is a low- power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. DS1307 works on I2C protocol which can interfaces to most microcontrollers.

A 3 volt lithium coin cell battery (CR2032) is connected to the DS1307 which keeps time running after electricity failure. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply.

The module also include a 24C32 32K I2C EEPROM which is not using in this project.


Source Code

This project includes three examples that shows how to display real time on a LCD display with or without using a RTC module.

  1. LCD Digital clock without RTC module.
  2. DS1307RTC LCD Digital clock.
  3. DS1307RTC LCD Digital clock with Alarm.

 The Arduino code is breaking into multiple files which make it more readable and hence easy to maintain.

Arduino IDE Environment



Key Functions

Keys Function 1 Function 2
Left Set Time  
Right Set Date  
Up Set Alarm Time Increase value by one
Down Disable/Enable Alarm Decrease value by one
Select toggle between 12 or 24 hour format Accept input


How to adjust the date and time

Set Date/Time via Serial Interface
  • Run the Serial terminal.
  • Make sure you have selected the the correct COM port.
  • Make sure the baud rate is set correctly at 9600 bps. You should see the following:
    Serial Terminal
  • To set the system date, send a string to the serial port with the following syntax:
    E.g. D160415 will set the system date to 2016 April 15th
  • To set the system time, send a string to the serial port with the following syntax:
    E.g. T012345 will set the system time to 1AM 23minute 45seconds
  • To set the Alarm time, send a string to the serial port with the following syntax:
Set the Date via Push Buttons
  • Push the Right button in order to change the system date.
  • Use the Up/Down buttons to change the date.
  • Push the Select button to accept changes.
  • Timeout will occur after idle for 10 seconds, changes made will not save.
Set the Time via Push Buttons
  • Push the Left button in order to change the system time.
  • Use the Up/Down buttons to change the time.
  • Push the Select button to accept changes.
  • Timeout will occur after idle for 10 seconds, changes made will not save.
Set the Alarm Time via Push Buttons
  • Push the Up button in order to change the alarm time.
  • Use the Up/Down buttons to change the time.
  • Push the Select button to accept changes.
  • Timeout will occur after idle for 10 seconds, changes made will not save.
Disable/Enable Alarm
  • Push the Down button to disable or enable Alarm.
  • If Alarm is enabled, its time is shown on the right corner of the LCD.
  • If  Alarm is disabled, its time is hidden.
Read 20811 times Last modified on Friday, 08 June 2018 12:20


  • Comment Link Stevambums Monday, 13 January 2020 10:31 posted by Stevambums

    Purchase Accutane Online Discount Flagyl Tablets Viagra Kaufen Express cialis cheapest online prices Foro Cialis Sin Receta Propecia Cost Canada

  • Comment Link Calade Friday, 12 July 2019 17:40 posted by Calade

    Hello, Great post working perfectly !
    I'm trying to display also the day of the week, but unsuccessfuly , I know some function do exist to return as 0-6 number...
    Can you please help ?

  • Comment Link admin Thursday, 07 June 2018 09:28 posted by admin

    Mr Moe,
    It seems that the Time.h library is not loaded.

  • Comment Link Mr Moe Wednesday, 30 May 2018 19:58 posted by Mr Moe

    Hi Sir,
    I got error message "' tmElements_t tm;" does not name a type '" . kindly please advice how to fix it . thanks

  • Comment Link Andy Monday, 09 April 2018 03:57 posted by Andy

    Is there a way of making 2 alarms rather than just one, still new to this, but getting by.


  • Comment Link admin Wednesday, 27 September 2017 10:23 posted by admin

    Thank You, Jeff

  • Comment Link Jeff in Utah Monday, 25 September 2017 10:12 posted by Jeff in Utah

    Thank you so much for providing this wonderful project. This has been my first really big project. I have enclosed it in a 3D printed case. I built this alarm project and added a IR communication to turn on a LED flood light, to mimic a day glow alarm clock for my teen-aged daughter who has a hard time waking up in the morning. The alarm would trip just fine if I set it to trip anytime before midnight. However, I found that if I set the alarm, to be after midnight the alarm would not trip. After adding some DEBUG_PRINT lines (See Below) I realized that the alarm would only work one day of the week not every day that I needed. So I would have to set the alarm to turn on at 6:00 am after midnight each day. That wasn't going to work for me. I changed the IF statement for the alarm from:

    if (t_time == t_alarm) {


    if ((hour(t_time) == hour(t_alarm)) && (minute(t_time) == minute(t_alarm)) && (second(t_time) == second(t_alarm))) {

    Now the alarm will trip if the hour, minute, and seconds are equal between the t_alarm (alarm set time) and t_time (current time). This ignores the day like Monday and Tuesday and will work for every day of the week.

    DEBUG_PRINT(" weekday of alarm");
    DEBUG_PRINTLN(" weekday of current time - they should be same");

  • Comment Link admin Monday, 04 September 2017 18:05 posted by admin

    Thank you for your suggestion.
    Please follow link below for those who interested on how to display large font in a LCD1602

  • Comment Link Zia Sunday, 03 September 2017 05:45 posted by Zia


    Though I would say thank you for your comprehensive Arduino based alarm clock, I am about to build mine into a case all based on this awesome tutorial :) Still need to figure out how to get a buzzer wired up but I note posts by others reference this so should be straight forward enough (i hope).

    Can I offer a suggestion to revise this Arduino based alarm clock to use a larger font? Would make this project a little more attractive to look at when all built up ;)


  • Comment Link admin Thursday, 25 May 2017 12:28 posted by admin

    I'm not sure if I understand your question. This project using some library from other website , which library you want to use. What is "wday"?

  • Comment Link damien Monday, 22 May 2017 05:33 posted by damien

    I used your code to make a clock for my daughter, many LEDs etc... i needed to add "wday" and i got a problem: on saturday evening a 23:59:59, the week day goes to "7" and make the whole thing bugging. I added a way to set the "wday" myself to revert the bug. Is it possible the problem comes from the library?
    thank you for your help

  • Comment Link admin Monday, 01 May 2017 21:24 posted by admin

    I get it from China, you can get it from GearBest as well.

  • Comment Link achbah Saturday, 29 April 2017 06:44 posted by achbah

    hi where can i purchase all the iteams in this project i need them asap for a school project i need answear quick plzzz

  • Comment Link Rob Wednesday, 15 February 2017 23:28 posted by Rob

    Any progression yet. When you press reset the same problem happens. Set alarm is still in display but alarm does not go off

  • Comment Link Rob Tuesday, 13 December 2016 22:03 posted by Rob

    No problem, thanks for the update. Take your time. Hope you will find a solution for this in the future. I am not able to solve the problem myself. It would be very nice if the alarm clock would keep it's settings and give the preset alarm also after a power off/on.

    Best Regards, Rob

  • Comment Link admin Monday, 12 December 2016 20:52 posted by admin

    Hi, Rob
    I'm too busy and don't have time to test the project yet.

  • Comment Link Rob Saturday, 26 November 2016 03:36 posted by Rob

    Thanks very much for your help.

  • Comment Link admin Friday, 25 November 2016 10:24 posted by admin

    Give me some time, I will test it on this week end.

  • Comment Link Rob Wednesday, 23 November 2016 17:00 posted by Rob

    Pin 3 is working for the buzzer.
    When I set an alarm the buzzer sounds on the preset alarm time.
    However , when I remove power from the Arduino and power it up again the alarm does not sound on the preset alarm time. (the preset alarm time is still on the display)

  • Comment Link admin Wednesday, 23 November 2016 10:40 posted by admin

    How about change the BUZZER_PIN to 11

Leave a comment

Back to Top