# Arduino GPS Clock

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/027/725/medium800/leds_20150916_0011.jpg?1442445580)

This is a project to build a clock with an Arduino that sets itself using time broadcast from GPS satellites.

There are two ways this clock can be built. &nbsp;One way is with a GPS receiver which allows the clock to set its time automatically. &nbsp;There are&nbsp;a few dozen GPS satellites in orbit above the entire globe and all of them have extremely accurate clocks that make determining your position on earth possible. &nbsp;By tapping in to the time broadcast from these satellites you can easily build a very precise clock.

The second way to build this project is with a battery-backed real time clock module. &nbsp;This won't set itself like a GPS clock, but it will keep good time for many years. &nbsp;Both options work great for making a clock!

The best part of this project is that you can build it yourself exactly how you desire. &nbsp;You can put it in a fun case or just use a cardboard box—get creative and have fun building the clock. &nbsp;Learning and making things yourself is something to celebrate and encourage in people of all ages.

# Arduino GPS Clock

## Hardware

![](https://cdn-learn.adafruit.com/assets/assets/000/027/724/medium800/leds_20150916_0002.jpg?1442445550)

To build this&nbsp;clock you'll need the following hardware and tools:

- An [Arduino Uno](https://www.adafruit.com/products/50)&nbsp;or compatible like the [Metro 328](https://www.adafruit.com/products/2488). &nbsp;You can actually use other Arduinos like the Leonardo, Mega, etc. but you might need to make small changes to the code when using GPS (see the [GPS guide](../../../../adafruit-ultimate-gps/overview)&nbsp;and examples).
- A[7-segment LED backpack display](https://www.adafruit.com/categories/103). &nbsp;Either the 1.2" (big one, at the top of the photo above) or 0.56" (middle display in image) will work. &nbsp;You could also use a 14-segment quad alphanumeric display, but it doesn't have a colon so it won't make a great clock (but it is great for displaying text or more complex output). &nbsp;The code for this project is made to use the 7-segment display.
- A time source, one of the following:

  - The [ultimate GPS shield](https://www.adafruit.com/products/1272)&nbsp;or [breakout](https://www.adafruit.com/products/746). &nbsp;If you use GPS then the clock will set itself based on the time from GPS satellites. &nbsp;You'll need to make sure the clock can get a good view of the sky for it to get a GPS fix. &nbsp;You also probably want to [add a coin cell battery to the GPS board](../../../../adafruit-ultimate-gps/battery-backup)&nbsp;so that it remembers the time even when it loses the satellite signal.
  - A battery-backed real time clock like the [DS1307](https://www.adafruit.com/products/264). &nbsp;This is a cheaper option than GPS but it will need to have its time set once ahead of time (then it will remember the time as long as the battery lasts, which is usually many years!). &nbsp;The [ChronoDot](https://www.adafruit.com/products/255)&nbsp;is another nice real time clock. &nbsp;This guide will show how to use the DS1307. &nbsp;Be aware the DS1307 requires a little more soldering and assembly than the GPS shield or breakout.

- [Breadboard](https://www.adafruit.com/products/64)&nbsp;and [jumper wires](https://www.adafruit.com/products/153).
- An enclosure for the clock--the box your parts arrive in or any other small cardboard box works great. &nbsp;You can get as fancy or as simple as you want with an enclosure. &nbsp;You could even leave everything bare to showcase your work!
- [Soldering tools](https://www.adafruit.com/products/136). &nbsp;You'll need to solder the 7-segment display together, and solder&nbsp;the GPS or real time clock module. &nbsp;If you're new to soldering don't worry it's not difficult--with a little patience and practice you'll be a pro in no time. &nbsp;Be sure to read the [Adafruit guide to excellent soldering](../../../../adafruit-guide-excellent-soldering/tools)&nbsp;and practice on some spare parts to get the hang of it.
- [Power supply](https://www.adafruit.com/products/63)&nbsp;for the Arduino. &nbsp;You can plug in any 7-12 volt supply or even a [small battery pack](https://www.adafruit.com/products/67)&nbsp;to power the clock. &nbsp;I recommend a plug in power supply since the clock won't run for very long&nbsp;on batteries.

Once you have all the parts you'll first need to build the 7-segment display and GPS or real time clock module. &nbsp;Carefully follow the guides for each of these components to assemble and test them:

- [Adafruit LED Backpack Guide](../../../../adafruit-led-backpack/overview)
- [Adafruit Ultimate GPS Breakout Guide](../../../../adafruit-ultimate-gps/overview)
- [Adafruit Ultimate GPS Shield Guide](../../../../adafruit-ultimate-gps-logger-shield/overview)
- [Adafruit DS1307 Guide](../../../../ds1307-real-time-clock-breakout-board-kit/overview)

Be sure to check that the basic examples for each component work before you move forward with assembling the clock. &nbsp;It will be much easier to debug issues with each component in isolation instead of when they're assembled into the clock!

Once you're ready to wire up the clock follow the right section below depending on the hardware that you're using, either the GPS or DS1307 real time clock.

# GPS Clock Wiring

To build the clock with GPS support wire the components together as shown in the diagram below.

Note that this diagram only shows the ultimate GPS breakout. &nbsp;For the GPS shield ignore all the wires going to the GPS breakout and instead connect the GPS shield to the Arduino and then connect the 7-segment display to the GPS shield/Arduino as shown in the diagram (you might need to solder the wires to the open row of GPIOs next to the GPS shield's headers, or solder&nbsp;[stacking headers](https://www.adafruit.com/products/85)&nbsp;to the GPS shield).

![](https://cdn-learn.adafruit.com/assets/assets/000/027/728/medium800/leds_clock_gps_breakout_bb.png?1442455535)

- Connect **Arduino 5V** to **7-segment +/VIN power** pin and **ultimate GPS breakout VIN**  **pin** (red wires).
- **If using the larger 1.2" 7-segment display** connect **Arduino 5V** to **7-segment IO pin** (don't worry the 0.56" display doesn't have this pin).
- Connect **Arduino GND** to **7-segment -/GND ground** pin and **ultimate GPS breakout GND pin&nbsp;** (black wires).
- Connect **Arduino A5 or SCL** to **7-segment C/clock pin&nbsp;** (yellow wire).
- Connect **Arduino A4 or SDA** to **7-segment D/data pin&nbsp;** (orange wire).
- Connect **Arduino D8** to **ultimate GPS breakout TX pin&nbsp;** (blue wire).
- Connect **Arduino D7** to **ultimate GPS breakout RX pin** (green wire).

Once you've wired everything together it should look&nbsp;something like the following:

![](https://cdn-learn.adafruit.com/assets/assets/000/027/726/medium800/leds_20150916_0006.jpg?1442445818)

Now jump to the software page to learn how to install the software and run the clock sketch.

# DS1307 Real Time Clock Wiring

To build the clock with the DS1307 real time clock wire the components together as shown in the diagram below:

![](https://cdn-learn.adafruit.com/assets/assets/000/027/729/medium800/leds_clock_ds1307_bb.png?1442455578)

- Connect **Arduino 5V** to **7-segment +/VIN power pin** and **DS1307 5V pin**.
- **If using the larger 1.2" 7-segment display** connect **Arduino 5V** to **7-segment IO pin** (don't worry the 0.56" display doesn't have this pin).
- Connect **Arduino GND** to **7-segment -/GND ground pin** and **DS1307 GND pin**.
- Connect **Arduino A5 or SCL** to **7-segment C/clock pin** and **DS1307 SCL pin**.
- Connect **Arduino A4 or SDA** to **7-segment D/data pin** and **DS1307 SDA pin**.

Once you've wired everything together it should look something like the following:

![](https://cdn-learn.adafruit.com/assets/assets/000/027/727/medium800/leds_20150916_0003.jpg?1442445830)

Now continue on to the software page to learn how to install the software and run the clock sketch.

# Arduino GPS Clock

## Software

To use&nbsp;the clock sketch you'll want to make sure you're using the [latest version of the Arduino IDE](https://www.arduino.cc/en/Main/Software)&nbsp;(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](../../../../adafruit-arduino-lesson-1-blink). &nbsp;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. &nbsp;With the latest Arduino IDE you can use its [library manager](https://www.arduino.cc/en/Guide/Libraries#toc3)&nbsp;to easily install libraries, or check out this [guide on how to manually install a library](../../../../adafruit-all-about-arduino-libraries-install-use/arduino-libraries). &nbsp;You'll want to install the following libraries:

- [Adafruit LED Backpack Library](https://github.com/adafruit/Adafruit-LED-Backpack-Library)
- [Adafruit GFX Library](https://github.com/adafruit/Adafruit-GFX-Library)
- [Adafruit BusIO Library](https://github.com/adafruit/Adafruit_BusIO)
- [Adafruit GPS Library](https://github.com/adafruit/Adafruit-GPS-Library)&nbsp;(if using GPS)
- [Adafruit RTClib Library](https://github.com/adafruit/RTClib)&nbsp;(if using the DS1307)

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

![](https://cdn-learn.adafruit.com/assets/assets/000/027/730/medium800/leds_Screenshot_from_2015-09-16_20_53_46.png?1442462063)

If you already have one or more of these libraries installed then make sure to update it to the latest version. &nbsp; **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. &nbsp;Then select the **File -\> Examples -\> Adafruit LED Backpack Library -\> clock\_sevenseg\_gps** example. &nbsp;You should see something like the following loaded in the IDE:

![](https://cdn-learn.adafruit.com/assets/assets/000/027/731/medium800/leds_Screenshot_from_2015-09-16_20_59_52.png?1442462423)

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

```auto
// 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:

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

Info: 

The second thing you might want to change is the local time offset. &nbsp;The time from GPS satellites is in GMT or UTC universal time, but your local time is probably different. &nbsp;You can use a site like [worldtimeserver.com](http://www.worldtimeserver.com/current_time_in_UTC.aspx)&nbsp;or [this Wikipedia page](https://en.wikipedia.org/wiki/List_of_UTC_time_offsets)&nbsp;to find what the offset, or difference, is between GMT/UTC time and your local time. &nbsp;

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

```auto
// 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&nbsp;in daylight savings time:

```auto
// 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. &nbsp;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** ). &nbsp;Then press the upload button or click the **Sketch -\> Upload** item to send the code to the Arduino. &nbsp;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. &nbsp;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. &nbsp;Once the GPS has a satellite fix the LED will slow down and blink once every 15 seconds. &nbsp;Make sure the GPS breakout or shield has a clear view of the sky in order to acquire a fix. &nbsp;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](../../../../adafruit-ultimate-gps/battery-backup)&nbsp;so that it can save the time. &nbsp;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&nbsp;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. &nbsp;Then select the **File -\> Examples -\> Adafruit LED Backpack Library -\> clock\_sevenseg\_ds1307** &nbsp;example. &nbsp;You should see something like the following loaded in the IDE:

![](https://cdn-learn.adafruit.com/assets/assets/000/027/732/medium800/leds_Screenshot_from_2015-09-16_21_26_06.png?1442463988)

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. &nbsp;By default the clock uses 12-hour format and it's set by this line:

```auto
// 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:

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

That's it! &nbsp;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. &nbsp;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** ). &nbsp;Then press the upload button or click the **Sketch -\> Upload** item to send the code to the Arduino. &nbsp;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. &nbsp;However you can manually set the time by finding this part of the sketch in the setup function:

```auto
// 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. &nbsp;For example to set the time to January 21, 2014 make the code look like:

```auto
// 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. &nbsp;That's all there is to using the DS1307 real time clock!

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


## Featured Products

### Adafruit METRO 328 Fully Assembled - Arduino IDE compatible

[Adafruit METRO 328 Fully Assembled - Arduino IDE compatible](https://www.adafruit.com/product/50)
We sure love the ATmega328 here at Adafruit, and we use them&nbsp;_a lot_&nbsp;for our own projects. The processor has plenty of GPIO, Analog inputs, hardware UART SPI and I2C, timers and PWM galore - just enough for most simple projects. When we need to go small, we use a <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/50)
[Related Guides to the Product](https://learn.adafruit.com/products/50/guides)
### Adafruit METRO 328 - Arduino Compatible - with Headers

[Adafruit METRO 328 - Arduino Compatible - with Headers](https://www.adafruit.com/product/2488)
This is the&nbsp; **Adafruit METRO Arduino-Compatible - with&nbsp;headers.&nbsp;** It's a fully assembled and tested microcontroller and physical computing board with through-hole headers attached.&nbsp; If you don't want a&nbsp;Metro with the headers attached for...

In Stock
[Buy Now](https://www.adafruit.com/product/2488)
[Related Guides to the Product](https://learn.adafruit.com/products/2488/guides)
### DS1307 Real Time Clock breakout board kit

[DS1307 Real Time Clock breakout board kit](https://www.adafruit.com/product/264)
**[We've upgraded this RTC breakout and made it even easier to use! Now available as a fully assembled board, it has the same components, chip, size, etc but you don't have to put it together. It's also less expensive! Check out...](https://www.adafruit.com/product/3296)**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/264)
[Related Guides to the Product](https://learn.adafruit.com/products/264/guides)
### ChronoDot - Ultra-precise Real Time Clock

[ChronoDot - Ultra-precise Real Time Clock](https://www.adafruit.com/product/255)
The **ChronoDot V3** is the latest version of macetech’s popular ChronoDot line of products. Designed during the Great Chip Shortage, it uses the newly-released MAX31328 temperature-compensated real-time clock chip. However, it remains pin- and code-compatible with the older...

In Stock
[Buy Now](https://www.adafruit.com/product/255)
[Related Guides to the Product](https://learn.adafruit.com/products/255/guides)
### Adafruit Ultimate GPS Breakout - 66 channel w/10 Hz updates

[Adafruit Ultimate GPS Breakout - 66 channel w/10 Hz updates](https://www.adafruit.com/product/746)
We carry a few different GPS modules here in the Adafruit shop, but none that satisfied our every desire - that's why we designed this little GPS breakout board. We believe this is the **Ultimate** GPS module, so we named it that. It's got everything you want and...

In Stock
[Buy Now](https://www.adafruit.com/product/746)
[Related Guides to the Product](https://learn.adafruit.com/products/746/guides)
### Adafruit Ultimate GPS Logger Shield - Includes GPS Module

[Adafruit Ultimate GPS Logger Shield - Includes GPS Module](https://www.adafruit.com/product/1272)
Brand new and better than ever, we've replaced our Adafruit GPS shield kit with this assembled shield that comes with an Ultimate GPS module. This GPS shield works great with any "Arduino shaped" board that has SPI and UART pins available,&nbsp;and is designed to log data to an...

In Stock
[Buy Now](https://www.adafruit.com/product/1272)
[Related Guides to the Product](https://learn.adafruit.com/products/1272/guides)
### 9 VDC 1000mA regulated switching power adapter - UL listed

[9 VDC 1000mA regulated switching power adapter - UL listed](https://www.adafruit.com/product/63)
This is a really nice power supply. It's a switching DC supply so it's small and light and efficient. It is thin so it fits in power strips without blocking other outlets. The output is regulated so you'll get a steady 9V up to 1000mA (1 Amp) of current draw. 5.5mm/2.1mm barrel...

In Stock
[Buy Now](https://www.adafruit.com/product/63)
[Related Guides to the Product](https://learn.adafruit.com/products/63/guides)
### Adafruit 0.56" 4-Digit 7-Segment Display w/ I2C Backpack - Blue

[Adafruit 0.56" 4-Digit 7-Segment Display w/ I2C Backpack - Blue](https://www.adafruit.com/product/881)
What's better than a single LED? Lots of LEDs! A fun way to make a small display is to use an [8x8 matrix](https://www.adafruit.com/category/37_88) or a [4-digit 7-segment display](https://www.adafruit.com/category/37_103). Matrices like these are...

In Stock
[Buy Now](https://www.adafruit.com/product/881)
[Related Guides to the Product](https://learn.adafruit.com/products/881/guides)

## Related Guides

- [Adafruit LED Backpacks](https://learn.adafruit.com/adafruit-led-backpack.md)
- [Personalized NextBus ESP8266 Transit Clock](https://learn.adafruit.com/personalized-esp8266-transit-clock.md)
- [Mindfulness Clock OF DOOM](https://learn.adafruit.com/mindfulness-clock-of-doom.md)
- [Arduino Lesson 13. DC Motors](https://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors.md)
- [1.8" TFT Display Breakout and Shield](https://learn.adafruit.com/1-8-tft-display.md)
- [Adafruit Ultimate GPS Logger Shield](https://learn.adafruit.com/adafruit-ultimate-gps-logger-shield.md)
- [LED Backpack Displays on Raspberry Pi and BeagleBone Black](https://learn.adafruit.com/led-backpack-displays-on-raspberry-pi-and-beaglebone-black.md)
- [Large Pi-based Thermometer and Clock](https://learn.adafruit.com/large-pi-based-thermometer-and-clock.md)
- [Overwatch Prop Gun: Lucio's Blaster Pt. 2](https://learn.adafruit.com/overwatch-lucio-gun-pt-2.md)
- [Trainable Robotic Arm](https://learn.adafruit.com/trainable-robotic-arm.md)
- [Tap Tempo Trinket](https://learn.adafruit.com/tap-tempo-trinket.md)
- [2.8" TFT Touch Shield](https://learn.adafruit.com/2-8-tft-touch-shield.md)
- [Experimenter's Guide for Metro](https://learn.adafruit.com/experimenters-guide-for-metro.md)
- [CircuitPython Hardware: LED Backpacks & FeatherWings](https://learn.adafruit.com/micropython-hardware-led-backpacks-and-featherwings.md)
- [Scrolling Countdown Timer](https://learn.adafruit.com/scrolling-countdown-timer.md)
- [Using the CircuitPython Extension for Visual Studio Code](https://learn.adafruit.com/using-the-circuitpython-extension-for-visual-studio-code.md)
