Building a Clock with an Arduino


Building a clock with Arduino is easier than you think. All you need to get started is an Arduino prototyping board and some type of display, preferably one with at least eight characters. I will be using an Arduino Uno board with a Sparkfun 16x2 LCD display. To set the time on the Arduino, we will be sending a Unix Timestamp over serial using the Arduino IDE Serial Monitor.

Arduino Clock

Shopping List

You will need the following items for this project. They are all readily available at any hobby shop or online.

  • 1 Arduino Uno
  • 1 16x2 Character LCD Display
  • 1 Breadboard
  • 1 USB Cable
  • 1 Potentiometer
  • 1 220 Ohms Resistor
  • Several Jumper Wires

Arduino Circuitry

Look at the wiring diagram below to connect the LCD display with the Arduino Uno. It may look a little confusing, but it is very straight forward. Six jumper wires are connected from pins on the display to the Arduino headers. Ground is connected three times on the display; +5v once, and 220Ohms +5v are connected once.

The potentiometer is connected to Ground and +5v with the middle pin connected to the 3rd pin from the left on the display. The potentiometer is used to control the contrast of the display to make the characters readable.

The display should be connected to pins 2, 3, 4, 5, 11, and 12 on the Arduino.

Arduino Circuit Board

Arduino Code

There are two different libraries we will use for this project. One is the LiquidCrystal Library that comes packaged with the Arduino IDE. The second library is the Time Library created by Michael Margolis. As the two names imply, LiquidCrystal will be used to control the display, and Time will be used to keep track of the time.

I will discuss the major parts of the Arduino code; however, check out the download for the entire code file that is full of comments!

Install Time Library

Time Library is not packaged with the Arduino IDE. Download the library from

Once downloaded, you will need to put the entire folder into the Arduino Library folder so it can be included in your project. The default library folder is located at C:\Users\YourName\Documents\Arduino\libraries; however, it may be different on your computer.

Once you have the Time Library installed, it will be time to start writing your code.

Setup Function

The setup function in Arduinos is what the name implies; it is used for setting up the rest of the code. The setup function is only run one time when the Arduino initially starts up. Here, you will start the serial connection and the LCD library as well as print a “Starting” message.

void setup()  {
  Serial.begin(9600); // Start serial connection
  lcd.begin(16, 2); // Set up the LCD library. 16 Characters x 2 Lines
  lcd.print("Clock Starting"); // Print a startup message to the LCD

Loop Function

The loop function also does as its name implies; it loops forever. This is where the meat of our code will go for the Clock. It will check to see if the time is set; otherwise, it will wait for a Unix timestamp over serial to set the time.

When the time is set, it will update the serial monitor and the display every 1 second and display the current Hour, Minute, and Second.

void loop(){    
  if(Serial.available()) {
    processSyncMessage(); // If incoming serial, start syncing clock
  if(timeStatus() == timeNotSet) {
    // Time is not set, say so on the display
    lcd.setCursor(0,0); // Set cursor tells the library where to start writing
    lcd.print("Waiting for Sync"); // print sends text to the display at the above location
    Serial.println("Waiting for Sync"); // Print to the serial monitor
      // Time is set, display the current time!
      lcd.print("Current Time:   "); // There are extra spaces at the end to clear out previous text
  delay(1000); // Update every 1 second

digitalClockDisplay() Function

This function is responsible for actually updating the display with the correct time format. The Time Library provides the Hours, Minutes, and Seconds without any leading 0’s (for 0-9) so we have to make sure that it is displayed correctly. Without this function, the time would look like this: 12:4:3 instead of the correct time format of 12:04:03.

void digitalClockDisplay(){
  // Update the Clock
  lcd.setCursor(0,1); // Set the cursor to the 1st position (0) on the 2nd line (1)
  lcd.print(hour()); // Print the Hour
  lcd.print(":"); // Print the colon
  if (minute() < 10) // Add a leading zero if its less than 10
  lcd.print(minute()); // Print the Minute
  lcd.print(":"); // Print the colon
  if (second() < 10) // Add a leading zero if its less than 10
  lcd.print(second()); // Print the Second

  // Output the current time to the Serial Monitor
  Serial.print("Current Time: ");
  if (minute()<10)
  if (second()<10)

processSyncMessage() Function

This function is responsible for processing the Unix timestamp that will be sent over the serial connection. This function will sync the Arduino clock with whatever Unix timestamp we provide over serial.

void processSyncMessage() {
  // If Unix Timestamp is provided over serial, parse the timestamp
  while(Serial.available() >=  TIME_MSG_LEN ){  // Ensure it contains at least 10 characters
    char c = ; // Capture the Serial Header
    Serial.print(c);  // Print it out
    if( c == TIME_HEADER ) { // Make sure its the right header
      time_t pctime = 0; // Clear the time
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c =;          
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
      setTime(pctime);   // Sync the Arduino clock with the Unix Timestamp

Uploading Code to Arduino

Now it is time to upload the code to the Arduino. In the Tools menu, ensure that the board and the port are both correct. If you have the most recent Arduino IDE, it will say “Arduino Uno” on the port menu.


Once you have selected the correct Board and Port, press the right arrow on the Arduino tool bar to begin uploading the code to the board.

Uploading Code to Arduino Board

You will know the upload was successful when the status bar shows “Done Uploading”.

Arduino Code Upload Confirmation

If the upload fails for any reason, the status bar will display the error message and will point out in the text area how to fix the problem. The most common problem is selecting the wrong Port or Board type.

Setting the Time

Now that you have your Arduino programmed, it is time to set the time! Press the Magnifying glass in the Arduino toolbar to bring up the Serial Monitor.

Arduino Serial Monitor

When you first open the Serial Monitor, you will see a “Waiting for Sync” message displayed every second. This is because the Arduino does not know what time it is yet. We will need to send the current Unix Timestamp to the Arduino.

Go to to get the current Unix Timestamp. The Web site will automatically tell you the current timestamp, highlight the timestamp, and copy it. Now, go back to the Serial Monitor and type T in the command line at the top followed by the timestamp that you just copied and press Enter. You Arduino will now be set!

Setting Arduino Time - 1

Setting Arduino Time - 2

Running Arduino Clock

Now What?

Congrats on creating your first Arduino Clock! But, now what? Now that you know how to tell time with Arduino, you can create smarter Internet of Everything (IoE) devices that are aware of the time. Automate the blinds in your house to open and close at a certain time of day. Create something with a bang that will really get people’s attention! 

References / Resources

# # #



  • Good but...

    Posted by pata_de_jaguar on 09/21/2015 08:13am

    Make your own clock and win a FBI Arrest for free!!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Featuring Art Schoeller, VP and Principal Analyst, at Forrester Research Live Event Date: November 9, 2016 @ 10 AM PT / 1 PM ET Being "proactive" with your customers is not enough to survive in the market today. The truth is, organizations that understand how to engage customers on their preferred channels will create contextual, and relevant experiences for customers. Plus, they will see the financial impact of nurturing long-term customer loyalty. Join our guest speaker Art Schoeller, VP and Principal …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date