Tick tock, what’s the time? This simple class is a simple library to provide a clock for an Arduino equipped with an Ethernet shield. Saves adding a separate real-time clock chip to maintain time for a program connected to the Internet.

To use, declare a global instance of the clock class, and call maintain in each iteration of the loop. Every 5 hours, the clock will contact a network time protocol (NTP) server to get the latest time. In between it will extrapolate time from the Arduino’s millisecond timer. We’ve tried to keep the RAM footprint for this class as small as possible so, for example, it uses program memory to store the NPT message. This library builds on work by Michael Margolis and the Arduino Ethernet library.


In this test program, the time is written out the serial port every second. An instance variable, initialization and a call to maintain (which contacts the NTP server at the right time) are all that are needed to drive this simple clock.

Public Interface

The clock class includes 7 public functions:


The class infrastructure is handled through the Setup and Maintain functions. Neither take any parameters and are intended to be called from the main Arduino setup and loop functions respectively. The work to update the local time is handled by the Maintain function. Most of the time it does nothing. Every five hours it contacts a network time server to update the internal time reference.

The clock class stores time in UTC format as an offset, in seconds, from 1 Jan 1970; the same format used for Unix time. The function SetTimezoneOffset saves an offset, applied to correct for the local time zone.

Time Functions

Four functions provide access to the time stored:

  • GetTimestamp – Returns the number of seconds since 1 Jan 1970, in the local time-zone.
  • WriteTime – Writes the current local time to a serial port in the form HH:MM:SS
  • WriteDateTime – Writes the current local date and time to a serial port in the form YYYY/MM/DD HH:MM:SS
  • DecodeTo – Decodes the local date and time into a DateTime structure. The DateTime structure includes fields for year, month, day, hours, minutes and seconds.

Download Here

The Arduino clock class can be downloaded and used in your own projects. The zip file includes the Clock class and the example above. Place it in your Arduino libraries folder and you’ll find the example on the File menu.

Sounds awesome? Let’s have it a go!

Start typing and press Enter to search