Software

To use the clock sketch you'll want to make sure you're using the latest version of the Arduino IDE (1.6.5 at the time of this writing).

If you're totally new to Arduino take a little time to go through some introductory tutorials like how to make a LED blink.  This will help you understand how to use the IDE, load a sketch, and upload code.

Next you'll need to make sure the libraries used by the sketch are installed.  With the latest Arduino IDE you can use its library manager to easily install libraries, or check out this guide on how to manually install a library.  You'll want to install the following libraries:

Search for the libraries in the library manager and they should be easy to find and install:

If you already have one or more of these libraries installed then make sure to update it to the latest version.  In particular you must update the LED backpack library to its latest version in order to get the clock sketches!

Now follow the appropriate section below depending on if you're using GPS or the DS1307.

GPS Clock Sketch

To load the GPS clock sketch make sure the hardware is wired together, the libraries above are installed, and the Arduino is connected to the computer through a USB cable.  Then select the File -> Examples -> Adafruit LED Backpack Library -> clock_sevenseg_gps example.  You should see something like the following loaded in the IDE:

There are two things you might want to change in the sketch before you upload it to the Arduino.  The first is if the clock uses 24-hour or 12-hour time format.  By default the clock uses 12-hour format and it's set by this line:

Download: file
// Set to false to display time in 12 hour format, or true to use 24 hour:
#define TIME_24_HOUR      false

If you'd like to use 24-hour time change the line to set the define as true, like:

Download: file
// Set to false to display time in 12 hour format, or true to use 24 hour:
#define TIME_24_HOUR      true

The second thing you might want to change is the local time offset.  The time from GPS satellites is in GMT or UTC universal time, but your local time is probably different.  You can use a site like worldtimeserver.com or this Wikipedia page to find what the offset, or difference, is between GMT/UTC time and your local time.  

By default the sketch uses UTC-7 time, or the US Pacific timezone in daylight savings time.  If you are in the US Eastern timezone you'd want to change this value to UTC-4.  To change the local time offset find this part of the code:

Download: file
// Offset the hours from UTC (universal time) to your local time by changing
// this value.  The GPS time will be in UTC so lookup the offset for your
// local time from a site like:
//   https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
// This value, -7, will set the time to UTC-7 or Pacific Standard Time during
// daylight savings time.
#define HOUR_OFFSET       -7

And change it to a new offset, like -4 for US Eastern in daylight savings time:

Download: file
// Offset the hours from UTC (universal time) to your local time by changing
// this value.  The GPS time will be in UTC so lookup the offset for your
// local time from a site like:
//   https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
// This value, -7, will set the time to UTC-7 or Pacific Standard Time during
// daylight savings time.
#define HOUR_OFFSET       -4

Now save your modified example and you're ready to upload it to the Arduino.  Make sure under the Tools -> Board menu the Arduino Uno is selected, and under the Tools -> Port menu the serial port for the Arduino is selected (it should say Arduino Uno).  Then press the upload button or click the Sketch -> Upload item to send the code to the Arduino.  Woo-hoo the clock sketch should be running!

Once the clock sketch is loaded you'll need to wait for the GPS to get a satellite lock before the time will accurately be displayed.  If you look at the GPS breakout or shield you should see a fast blinking red LED (blinking once a second) when the GPS is acquiring a fix.  Once the GPS has a satellite fix the LED will slow down and blink once every 15 seconds.  Make sure the GPS breakout or shield has a clear view of the sky in order to acquire a fix.  It can take anywhere from 45 seconds to 30 minutes to get a fix, depending on the location and view of the sky.

If you haven't done it already consider adding a coin cell battery to the GPS so that it can save the time.  This will let you read a good time value from the GPS even when it hasn't acquired a satellite fix.

Congratulations you've successfully built a GPS clock!

DS1307 Real Time Clock Sketch

To load the DS1307 clock sketch make sure the hardware is wired together, the libraries above are installed, and the Arduino is connected to the computer through a USB cable.  Then select the File -> Examples -> Adafruit LED Backpack Library -> clock_sevenseg_ds1307 example.  You should see something like the following loaded in the IDE:

If you'd like you can change whether the clock displays time in 24-hour or 12-hour time format by slighting changing the sketch code.  By default the clock uses 12-hour format and it's set by this line:

Download: file
// Set to false to display time in 12 hour format, or true to use 24 hour:
#define TIME_24_HOUR      false

If you'd like to use 24-hour time change the line to set the define as true, like:

Download: file
// Set to false to display time in 12 hour format, or true to use 24 hour:
#define TIME_24_HOUR      true

That's it!  There isn't anything else you normally need to change in the sketch.

Now save your modified example and you're ready to upload it to the Arduino.  Make sure under the Tools -> Board menu the Arduino Uno is selected, and under the Tools -> Port menu the serial port for the Arduino is selected (it should say Arduino Uno).  Then press the upload button or click the Sketch -> Upload item to send the code to the Arduino.  Woo-hoo the clock sketch should be running!

If this is the first time you've used the DS1307 it will automatically set its time based on the time the sketch was compiled and uploaded.  However you can manually set the time by finding this part of the sketch in the setup function:

Download: file
// Set the DS1307 clock if it hasn't been set before.
bool setClockTime = !rtc.isrunning();
// Alternatively you can force the clock to be set again by
// uncommenting this line:
//setClockTime = true;
if (setClockTime) {
  Serial.println("Setting DS1307 time!");
  // This line sets the DS1307 time to the exact date and time the
  // sketch was compiled:
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // Alternatively you can set the RTC with an explicit date & time, 
  // for example to set January 21, 2014 at 3am you would uncomment:
  //rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

Notice the comments mention how to manually set the time.  For example to set the time to January 21, 2014 make the code look like:

Download: file
// Set the DS1307 clock if it hasn't been set before.
bool setClockTime = !rtc.isrunning();
// Alternatively you can force the clock to be set again by
// uncommenting this line:
setClockTime = true;
if (setClockTime) {
  Serial.println("Setting DS1307 time!");
  // This line sets the DS1307 time to the exact date and time the
  // sketch was compiled:
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // Alternatively you can set the RTC with an explicit date & time, 
  // for example to set January 21, 2014 at 3am you would uncomment:
  rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

Upload the sketch again and the time should be changed.  That's all there is to using the DS1307 real time clock!

Congratulations you've successfully built an Arduino clock using the DS1307!

This guide was first published on Sep 17, 2015. It was last updated on Sep 17, 2015. This page (Software) was last updated on Feb 23, 2020.