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.

Features

  • 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.
  • RTC.read(tm);
    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:
    Dyymmnn
    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:
    Thhmmss 
    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:
    Ahhmmss
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 7181 times Last modified on Wednesday, 13 July 2016 21:39

18 comments

  • 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
    http://www.instructables.com/id/Custom-Large-Font-For-16x2-LCDs/

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

    Hi,

    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 ;)

    Thanks!

  • 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

    Hi,
    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
    Damien

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

    I get it from China http://taobao.com, you can get it from GearBest as well.
    http://www.gearbest.com/

  • 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

  • Comment Link Rob Tuesday, 22 November 2016 23:43 posted by Rob

    Thnx for the tip but that did not help. After reset still not a buzzer sound.
    Below is how I turn on the defined buzzer.

    /////////////////////////////////////////////////////////////////////////
    // turn ON/OFF alarm
    //////////////////////////////////////////////////////////////////////////
    void switch_alarm(boolean state) {
    pinMode(ALARM_PIN, OUTPUT);
    digitalWrite(ALARM_PIN,state);
    show_alarm_status(state);


    if (alarm_state() == HIGH) analogWrite(BUZZER_PIN, 220) ; // modified by Rob
    if (alarm_state() == LOW) analogWrite(BUZZER_PIN,0); // modified by Rob

  • Comment Link admin Tuesday, 22 November 2016 18:31 posted by admin

    You need to send a LOW signal to BUZZER_PIN after the Arduino is powered up. Put the following line inside the setup() routine.
    analogWrite(BUZZER_PIN,0);

  • Comment Link Rob Monday, 21 November 2016 16:55 posted by Rob

    Thank you for the response on my question.

    I found a different solution that also works. Does not need a transistor. Only a piezo buzzer.

    #define BUZZER_PIN 3 // BUZZER on PIN 3
    in the turn ON/OFF alarm routine:
    if (alarm_state() == HIGH) analogWrite(BUZZER_PIN, 220) ;
    if (alarm_state() == LOW) analogWrite(BUZZER_PIN,0);

    However, I have now another problem: when I remove power from the Arduino after I set an alarm time and put the power on again the set alarm time is still in the display but the alarm is not going of.
    I need to set it again.
    Doe you know a solution?

  • Comment Link admin Monday, 21 November 2016 09:57 posted by admin

    The alarm is control by A3, you need to connect a transistor or relay to this pin.

  • Comment Link Rob Wednesday, 16 November 2016 20:27 posted by Rob

    I am just starting with the Arduino and I would like to have a buzzer as an alarm . Could you please help me with the changes needed in your program? I have no knowledge enough to program this by myself. I was able to get the alarm clock working with your program.

    Kind Regards, Rob

Leave a comment

Back to Top