What You Need for an Arduino Clock Project
If you want to keep accurate time on an Arduino project, you need a real-time clock of some kind. The Arduino can keep track of time very accurately by using the millis() function, but only since the last time it was powered up. If you lose power to the Arduino, the millis() function gets reset to zero — not very handy if you are trying to log real time precisely.
What you need is an additional way to keep time, while consuming only a little power. It’s more or less a wristwatch. It has a tiny button battery for power, so it works even if there’s a power outage and your Arduino shuts down. When the power comes back, your clock will still be ticking away. Because it’s very efficient, the RTC battery lasts for several years.
Although the RTC is your timekeeper, you use the Arduino to handle the display of the time and responding to inputs and outputs as you change its settings. The main features for your clock are
Keeping track of the time of day
Displaying the time
Setting an alarm and sounding it
First, you need to get everything together.
In this project, you build it on a breadboard, but you may want to move it to a suitable housing after you’ve got it working. In that case, make sure your housing can accommodate your breadboard or consider moving the circuit to a stripboard when you’ve finished building it. Here are the parts you need:
A full-size breadboard or two half-size ones that are connected
Adafruit Industries Real Time Clock DS1307 Breakout Board
An HD44780 standard parallel 16 x 2 LCD Display (such as Adafruit ID: 181, Jameco 2118598, or the 16 x 2 display from oomlout.co.uk)
A 10k ohm potentiometer to control contrast (included with the displays from Adafruit and Oomlout)
Two momentary pushbutton switches (widely available, such as Jameco 2076148 or from oomlout.co.uk)
A single-pole dual throw (SPDT) or double-pole double-throw (DPDT) slide switch with ON-NONE-ON positions (such as Jameco 1915801 or Farnell 1813681 or 1437711).
A piezoelectric sounder (such as Jameco 1956776 or Maplin N16CL)
Four 220-ohm resistors
A suitable housing, if you want to package it up nicely.
Many RTC modules are on the market. In terms of its ease of use, one of the best is the RTC DS1307 Breakout Board module from Adafruit Industries. It is based on the Dallas Semiconductor DS1307 Integrated Circuit (IC), which is an inexpensive and rugged real-time clock. It’s on the low end cost-wise, so it may lose or gain a small amount of time over extended periods.
More precise products are available, but they are much more expensive. You could skip getting the kit, purchase the various parts necessary to use the DS1307 and build it yourself, but the breakout board kit contains all the parts and the printed circuit board makes it very simple to connect the components correctly.
A breakout board is any printed circuit board that makes it easy to physically access all the pins on an integrated circuit or other device for prototyping, building, and testing. In a sense, the Arduino is a breakout board from the ATmega328 Microcontroller IC.
The Adafruit Industries RTC Breakout Board module comes as a kit with a small number of parts, so you need to do a little soldering to reap its benefits. But a little soldering is always rewarding! This kit is inexpensive and readily available, and has excellent online assembly instructions and documentation. As a bonus, the battery is included!
With the RTC breakout board at the heart of your clock, you need to be able to display the time, as well. Although there are dozens of ways to do this, one of the simplest is using a 16 x 2 LCD display. These are inexpensive and easy to control. There are Arduino code libraries for LCD displays, which make it easy to update text on the screen.
Another benefit of using a 16 x 2 LCD display is that you can add some text on the screen to make your clock a little more verbose and interesting than the standard, 7-segment display you find on most clocks out there.
The term 16 x 2 refers to the number of characters on a 16 x 2 display. These LCDs displays can display two lines of text, composed of 16 characters each.
You need the following two switches:
The momentary pushbutton switches are for incrementing the hours and minutes of the alarm time. The parts listed are suitable for easily inserting into a breadboard, but you may want to use different switches if you are going to place this clock into a separate housing.
The slide switch is used to control three states: time display mode, alarm set mode, and alarm armed mode. This means the switch must have three positions, so make sure to get one that is: ON-NONE-ON and not simply a two-position, ON-ON switch.
For the alarm, you need a piezoelectric sounder. These come with or without a plastic housing. You should get one that is enclosed already, because the enclosures are designed to amplify the sound of the piezo element. If yours is not enclosed you can mount it on a hard surface, but it’s better to get one that is designed to be clearly audible in the first place.