How to Display the Time for Your Arduino Clock Project

By Brock Craft

Once you’ve got something on the display for your Arduino Clock Project, it’s time to read the time from the RTC module and display it. Later, you add buttons to program the alarm, and a switch to change between Time Display, Alarm Set, and Alarm Armed modes.

There are really only two things you can display with this clock: either the current time or the time you want the alarm to go off. Rather than including the code for both in the main loop(), it makes sense to put the display procedure in its own function.

Add the following code to the variable declarations section at the top of your code:

RTC_DS1307 rtc;       // Create a realtime clock called rtc
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Create an LCD called lcd
DateTime now;

This creates a DateTime object called now. The DateTime object is part of the RTC library and is the only way to get the time out of the RTC module, though it has a lot of options and can provide hours, minutes, seconds, and the year, month, and day.

Next, add the code in your main loop that reads the current time from the RTC module, and add a function to update the display with the current time:

void loop(){
now =; // Get the current time
// Refresh the display

The now DateTime object stores the current time taken from the function to display the time and to check whether it’s time to trigger the alarm.

To keep the code modular, updating the display is done in its own function, outside the main loop. After closing the main loop, add the updateDisplay() function by entering the following code:

void updateDisplay(){
 int h = now.hour(); // Get the hours right now and store them in an integer called h
 int m = now.minute(); // Get the minutes right now and store them in an integer called m
 int s = now.second(); // Get the seconds right now and store them in an integer called s
 lcd.setCursor(0, 0); // Set the cursor at the column zero, upper row...
 lcd.print(" The time is: "); // ...with spaces to clear characters from setting alarm.
 lcd.setCursor(4, 1); // Move the cursor to column four, lower row
 if (h<10){   // Add a zero, if necessary, as above
 lcd.print(h);   // Display the current hour
 lcd.setCursor(6, 1); // Move to the next column
 lcd.print(":");  // And print the colon
 lcd.setCursor(7, 1); // Move to the next column
 if (m<10){   // Add a zero, if necessary, as above
 lcd.print(m);   // Display the current minute
 lcd.setCursor(9, 1); // Move to the next column
 lcd.print(":");  // And print the colon
 lcd.setCursor(10, 1); // Move to the next column
 if (s<10){   // Add a zero, if necessary, as above
 lcd.print(s);   // Display the current second

Send this code to your Arduino and see what happens. If all is well and your code compiles correctly, you should be in business! The display should show a time that matches the one on your computer.

In the code, the current time is requested from the now object and placed into three integer variables used only in the updateDisplay function. These variables have a local scope, meaning they only can be used within updateDisplay(), and the rest of the program doesn’t know anything about them — which explains why they are not declared at the beginning of your code.

You request time minutes, hours, and seconds separately with now.hours(), now.minutes(), and now.seconds(). Assigning each of these separately to its own integer variable (h, m, and s) is much easier than requesting the current time all at once.

Having parsed the time into three variables, the main thing going on in updateDisplay() is positioning the cursor on the LCD and printing the relevant text. Positioning the cursor is done with lcd.setCursor(), which takes two parameters: column number and line number. Setting the cursor only puts it in the correct position.

You then have to tell the display what to print. The numbering starts at zero, so the upper-leftmost character is in column 0 and row 0. The cursor is positioned at the top left of the screen and the first line of text is printed with lcd.print(“The time is:”).

You could use the lcd.clear() function, which clears all the text on the whole screen. However, your clock is constantly refreshing the display, so clearing the LCD every time the updateDisplay() function executes can introduce just a bit of noticeable flicker. A simpler approach (which uses one less instruction) is just to write the space characters over what might have been already on the display.

Next, the cursor is moved four spaces to the right and on the second row, so that the time is centered on the screen:


One tricky bit about using integers to get the hours, minutes, and seconds, is that they do not have a leading zero for values below 10. You need to add those in manually, both to make the clock look right (for example, 01:05) and to keep the hours minutes and seconds in the same place on the screen.

Adding the leading zeros is accomplished in the same way for hours, minutes and seconds:

if (h<10){  // Add a zero, if necessary, as above
 lcd.print(h); // Display the current hour

The conditional if statement tests whether the current time is less than 10, and if so, the print() statement prints a zero. After this, the LCD automatically advances to the next column position, so when you print the current hour, stored in the integer variable h, it will have a leading zero. The same goes for minutes and seconds.

You must also advance the cursor to the correct position for the leading zero for minutes and seconds. You insert a colon between them with the lcd.print(“:”); statement.