Internet Clock

01/20/14 Updates

Background

We recently replaced our Tivo with a TivoHD.  A consequence of that switch is that our cable box was replaced with a cable card which is inserted into the TivoHD.  With the loss of the cable box we also lost the digital time readout which was on the face of the box.   We liked the readout and decided it should be replaced.  I suppose I could have gone out and purchased a small digital clock but what's the fun in that.   I decided it would be a fun project to play around with.

Hardware Design

At the same time I had been experimenting with a new Wiznet module - the WIZ810MJ.  The Wiznet module contains a complete TCP/IP stack all on a single chip and makes interfacing with the Internet fairly simple compared to previous solutions.  So the plan was to design and build a digital clock that would be capable of interfacing directly with an Internet time server so that the time would be very accurate.  It would also solve the problem of power failures as well as make Daylight Savings Time almost a non event.

The final design proved to be pretty simple with only 3 main components.  The components were an Atmel ATMEGA168 AVR processor, a DB1-CLOCK module from Lucid Technologies, and a WIZ810MJ module from Wiznet.  The ATMEGA168 was actually more processor than necessary with its 16K of flash programming memory.  I used less than half of the flash but I had several on hand and I have tried to use this size on several projects rather than keep lots of different sizes on hand.  The 168 had all of the features I needed - an SPI interface to talk to the Wiznet module, several 16 bit timers with interrupts to implement the real time clock, and enough extra I/O to drive the clock display.  The DB1-CLOCK module is a 4 digit, .56" high LED mounted on a circuit board with an MC14489 common cathode driver chip.  The LED module only requires +5 volts, ground, and three I/O lines - enable, clock, and data.  As I mentioned before, the WIZ810MJ uses the WIZ5100 chip which is a complete ethernet interface on a single chip.  The module also has the required RJ45 connector, a 25 mhz crystal, and a few passives to round out the interface.  The only negatives for me were the 14 X 2 X 2mm headers that were required to plug in the interface.  While these headers are not that uncommon, .100" headers are far more common and, in fact, the next versions of the module have .100" headers.  These are the WIZ811MJ and the WIZ812MJ.

The only other thing I needed was a small power supply section.  I powered the clock from a 5 volt regulated wall wart since the display requires 5 volts but the Wiznet module needs 3.3 volts so I included an LM3940IT-3.3 3.3 volt regulator.  I also powered the processor from the 5 volt supply which worked out well since the WIZ810MJ has 5 volt tolerant I/O.

Here are the schematic and the PCB layout.  Clicking on the thumbnails will display full size images:
schematic PCB


Software


I used the Bascom compiler from MCS Electronics for the programming and I continue to be pleased with this software.  Mark is constantly improving it and, although every now and then I'm tempted to switch to C, there's really nothing that I haven't been able to do with Bascom.  The concept is to set up an interrupt routine using a 16 bit timer.  The timer start count on the timer would be set to overflow in 1 second and the interrupt routine would simply reset the count, increment seconds, minutes, and hours as required and then set a flag once a minute to display the time.  The routine would also set a flag once a day at midnight when a routine would be called to get the exact time from an Internet time server and correct the clock if necessary.  This routine would also adjust the timer start value for the timer to correct for any changes due to temperature drift which might cause the ceramic resonator frequency to drift slightly.

The interrupt routine, real time clock, and display stuff were pretty straight forward and things that I had done before.  The new part for me was the ethernet module and interfacing directly to the Internet.  Fortunately I found an excellent tutorial on using the Wiznet module and was able to use that to get a running head start.  I used UDP instead of TCP/IP since it is a much simpler protocol without error detection and was more than adequate for this project.  I also learned quite a bit about Internet time servers and the different protocols used.  There are basically three different protocols used by time servers - Network Time Protocol (RFC-1305), DayTime Protocol (RFC-867), Time Protocol (RFC-868).  You can read about the three protocols here.  Although it's not the most common, I used the Time Protocol since it was the easiest to use for my first attempt and there were several built-in Bascom functions that made the implementation very straight forward.

The DB1-CLOCK module also has 4 LEDs that I made use of.  One is the AM/PM indicator, one is a daylight savings time indicator, one is a link active LED, and the last one is an error condition indicator.  The Bascom source code is included in the download package if you're interested in how it's all implemented.  If you would like to build a clock similar to this, you might want to contact me for the latest code since I'm still tweaking the firmware to add features.

Enclosure


The clock enclosure was kind of fun as well.  It's a simple oak box fabricated from 3/8" red oak with a 1/8" oak back finished with Watco Danish Oil.  The fun part was making the cutouts in the front and back of the case which I did with my little CNC machine.  I used DeltCad to draw the cutouts and SheetCam to generate the tool paths and G Code.  It's always fun when I can combine several hobbies into one project.

Conclusion

So far I'm quite pleased with the way this project has turned out but I'm actually thinking of building another one to implement all the things I've thought of since completing the first clock.  Here's some of the things I'm thinking about:

Pictures

Here's the blank PCB:

blank pcb

Populated PCB:

pcb

Display board:

display

Clock with board installed:

installed board

Finished clock front:

front

Finished clock back:

back

And finally here's the clock up and running:

finished


Updated 01/20/14


The clock has been working well but we had a major power outage recently which also took out our Internet - my provider eventually had to replace the power supply and the entire ONT.  I realized that I needed to make a few changes since, as soon as we got power back, we had no time since the Internet was still down.  I also had several instances over the last couple of years where the hard coded IP SNTP address either was down or no longer responding to Port 37 requests.  My plan was to add a real time clock,  add several IP addresses, and make Daylight Savings Time correction automatic.  Once I finished that I still had plenty of room in the AVR so I decided to add a simple web server which would provide me with access to internal variables.

From a hardware standpoint, I found a simple, cheap real time clock module on Ebay that was only $1.37.  The module uses a DS1302 real time clock chip, has a socket for a backup battery, a 32,768 Hz crystal, and a 5 pin header.  I have used the DS1302 on previous projects and have a Bascom library for it and it works great.  Here's what the module looks like:

DS1302

Normally I probably would have developed a new PCB but I had added headers on the previous board for unused I/O as well as power and ground so I was able to simply use jumpers from my headers to the headers on the DS1302 module.  If I make any more clocks I will probably make a new PCB but, for now, this works for me.  Here's what it looks like:

clock

It was definitely fun to revisit the software since I have learned a lot more since my first program.  The main area of improvement was in using Timers.  The software uses a 16 bit timer with a prescaler of 256 to create an interrupt once per second.  In the interrupt routine, a soft clock is updated each second and the value of this soft clock is displayed each new minute.  In the previous version, I set the start value of the timer and then, when the clock rolled over, the interrupt routine is called and the timer start value is reset to the saved start value.  While this works, I don't think it's the best way to use timers.  In the new version, I set the top value of the timer that determines one second and then, when the timer reaches that setpoint, the interrupt routine is called and the timer automatically restarts from zero without having to reset the setpoint.  This seems to produce a much more reliable and consistent time base.

I hard coded 10 IP addresses for online SNTP servers that are currently working.  The software uses the first IP address and then, if a connection fails, the software automatically switches to the next IP address and saves the index to this IP in EEPROM.  I also added the capability to caluculate the start and end date for Daylight Savings Time and save these values in EEPROM.  AT 2:00 AM every day,  the software gets the latest date and time from the SNTP server, checks to see if Daylight Savings Time is in effect, updates the interrupt routine, and finally resets the time and date in the DS1302 module.  At power up, the program performs the same calculations and, if there is no Internet connection, uses the time and date from the DS1302 module.  Bascom recently added direct support for the various WIZnet chips so the actual programming is much simpler.  In my case, I'm using a WIZ810MJ which uses the W5100 chip.  This chip has built-in support for 4 sockets so the connection to the SNTP server uses one socket and I used a second socket to provide a simple web server.  The server returns the current date and time, the current timer setpoint, the current NTP IP address in use, the client IP address, and the start and end of Daylight Savings Time.  You can access the server here.  Here's what the output looks like:



Although I'm really pleased with the operation of the clock, I still  have a few ideas for future improvements.  I really like the web server and may add the capability to send data from the web page back to the clock program.  That way I could add new IP addresses or, if necessary, set the real time clock direcly from the web page.  Sounds like fun doesn't it?

I have added the new program to the zip file below.


Downloads


All of the necessary files are included in one download.  The file includes the schematic in TinyCad format as well as a .jpg file, the printed circuit board in FreePCB format as well as a .jpg file, the gerber files for having a board made (I used batchpcb.com), the Bascom source code, and an Excel file with the complete parts list.  

DOWNLOAD FILES


link_blue16 (1K)

Questions or Comments

Home