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 27247 times Last modified on Friday, 08 June 2018 12:20
Back to Top