http://tuxgraphics.org/electronics
   
 
| Tuxgraphics NTP clock with DHCP client, version 2.X![[Tuxgraphics desk top NTP clock with DHCP client]](../../common/images2/article2013021/ntp-clock_th.jpg)  Abstract:
 
The Network Time Protocol (NTP) has revolutionized the world. Suddenly one could
have anywhere in the world accurate time and date.
NTP is a simple UDP based protocol and can be implemented in a
microcontroller.
_________________ _________________ _________________
 The tuxgraphics NTP clock has been very popular over the years. This version
2.0 has a built-in DHCP client. You just plug this clock into any
network and everything else is automatic. You don't need any knowledge
about IP networking.
 
 
 
 
 | 
     
A NTP client
The NTP protocol is described in RFC958. Essentially it is just a 64 bit time-stamp. 32 bits of this time-stamp are the seconds in UTC (=GMT Greenwich mean time) since since Jan. 1st 1900. The other 32 bits are fractions of a second. In other words NTP can be very very accurate. For our purposes it is however enough if we just evaluate the seconds.
The AVR NTP clock synchronizes at startup with a NTP server and uses then a
timer interrupt to maintain time locally. Every hour it tries then to
synchronize again. If your DSL router is however off during the night then it's
not a problem. The clock just continues. We use the on board crystal to maintain
accurate time over several weeks. Once the network connection is back it will
just synchronize again. A little status indicator appears in the lower right
corner while the clock is waiting for an answer from the NTP server.
Public NTP servers are maintained by various universities, government
organizations and big computer companies
like apple. A suitable server is already pre-configured in the tuxgraphics
ntp server code.
     
How to build a clock
A clock is essentially just a counter. Since NTP is already a 32-bit "counter"
we just take a 32bit variable and increment it. That is: the initial setting of
the counter comes via NTP and then we just count up every second.
For this we just generate a timer interrupt every second. The 16bit
timer/counter of the atmega328p supports this already at a hardware level.
The basic clock is therefore just this:
// interrupt, step seconds counter
ISR(TIMER1_COMPA_vect){
        time++;
}
The hardware interrupt is generated every second and we step the counter "time".
Very easy.
A NTP server sends out the time as seconds (and fractions of seconds)
since 1-Jan-1900 GMT.
The math to convert seconds since day-X into a human readable
format is a quite heavy for a microcontroller. The AVR is a 8-bit
processor therefore 32-bit math is expensive but it can still be done.
     
The tuxgraphics  NTP clock with LCD display
    
    
    ![[The hardware components for the NTP clock]](../../common/images2/article2013021/devel_th.jpg) 
A 16x2 character LCD display + AVR ethernet SMD board + a push button is all you need.
    
    
    
    The large push button on the ethernet board can be used to change the
    offset to GMT. E.g. to change between summer time and winter time.
    
    
    I am always asked for ideas on how to build a good case for the clock.
    For all previous version I have used a thin (2-4mm) polycarbonate (lexan) or
    acrylic-glas (plexiglas)
    sheet and I just bend it into an L-like shape. You can find
    such sheets on ebay or in bigger hardware stores.
    
    
    
    ![[NTP clock with polycarbonate case]](../../common/images2/article2013021/polycarbonate-clock_th.jpg) 
NTP clock with clear polycarbonate case
    
    
    
    
    ![[NTP clock with polycarbonate case]](../../common/images2/article2013021/polycarbonate-clock2_th.jpg) 
NTP clock with clear polycarbonate case
    
    
    
    You need to heat the polycarbonate sheet along a thin line in order bend it without breaking it.
     For this you can build a plastic sheet bending
    machine out of a  constantan wire from
    an old toaster or an electric heater.
    
    
    
    ![[plastic bending machine]](../../common/images2/article2013021/plastic-bending_th.jpg) 
A plastic sheet bending machine. A spring holds the constantan wire
straight inside the groove because the wire expands while heated. The
groove is lined on the sides with ordinary aluminum foil.
    
    
    
    You run about 3 ampere DC through the wire to heat it. The exact amount
    depends on the diameter of the constantan wire. The wire must be hot
    but not glow red. The plastic sheet is then placed over the groove and
    heated just along that thin line for about 2 minutes.
    Note that the plastic does actually not touch the wire.
    
    
    Once heated to the right temperature you can bend the plastic sheet
    like warm wax. Since we have heated the sheet only along that thin
    line it will just bend there.
    
    
    Such a polycarbonate case is very nice and strong but it requires
    this plastic bending machine to bend the clear plastic sheet.
    
    
    A much simpler case can be made out of clear packaging material
    with just household scissors.
    
    
    
    ![[clear plastic packaging]](../../common/images2/article2013021/box1_th.jpg) 
A bit of clear plastic packaging can as well be used if folded into
a triangle.
    
    
    
    The ultra thin clear plastic packaging as found in the grocery
    store is too thin and not suitable to make a case. It should have
    some strength. Consumer electronics
    or toys are often packaged in bit stronger clear plastic.
You may as well be able to use a large clear plastic bottle from which
you slice out a ring and then cut it open to get a sheet of plastic.
    
    
    The clear plastic packaging can just be cut with scissors and you
    can bend it along the edge of a ruler without heating it first.
    Cut out a long stripe about 10cm (4 inch) wide and fold it into
    a triangle as shown below.
    
    
    
    ![[clear plastic packaging folded into triangle shape]](../../common/images2/article2013021/case1_th.jpg) 
A stripe of clear plastic packaging folded into a triangle shape.
    
    
    
    Now just punch-in some holes and mount the ethernet board and
    the display with thin cable-ties.
    
    
    
    ![[Tuxgraphics desk top NTP clock with DHCP client]](../../common/images2/article2013021/ntp-clock_th.jpg) 
    
    
    
     
Using the clock
The clock has a LCD display with the following LCD display fields:
| 
Time     offset-to-GMT
DayOfWeek Date  Status | 
The "Status" in the right corner is a one character wide status indicator:
-  nothing: ETH link ok, NTP time sync up-to date
 
 
-  '/': ETH link down
 
 
-  '|': ETH link up we are waiting for an answer from the NTP sever. The clock
   tries to sync every hour. Thus if you cut your Internet connection
   during the night then you will see this character. Note that the clock
   will continue to run form the internal crystal.
The clock has as well a built-in web interface. You just point your
browser to the IP address of the clock (it shows it's IP address
at power-on) and you can read the time over the web or you can configure
the clock. The settings are stored in eeprom and not lost at power down.
    
    
    ![[Tuxgraphics desk top NTP clock, web interface]](../../common/images2/article2013021/screenshot1.png) 
    
    Tuxgraphics desk top NTP clock, web interface, main page
    
    
    
    
    ![[Tuxgraphics desk top NTP clock, web interface]](../../common/images2/article2013021/screenshot2.png) 
    
    Tuxgraphics desk top NTP clock, web interface, configuration page
    
     
The NTP clock hardware
The hardware is the tuxgraphics AVR ethernet board (aka avr webserver board) and
a 16x2 HD44780 compatible LCD display. An optional small push button
is required to change the time-offset to GMT but this can as well
be done via the web-interface that this clock has.
I recommend to use for the ethernet board our SMD board as it fits nicely behind
the display.
The LCD display and the push button needs to be  connected as follows:
    
    
    
      ![[NTP clock circuit, LCD connection]](../../common/images2/article2013021/ntp-clock-LCD-button_th.png) 
NTP clock circuit, LCD connection (click for a circuit diagram in PDF
format)
    
    
You can power the 3.3V regulator of the ethernet board, the LCD display
and the LCD-backlight from a 5V DC power supply.
    
     
FAQ section
Q: My clock works but it is almost 5min off before the next synchronization
at the full hour.
A: You forgot to set the fuse bytes. Change the low fuse byte from 0x62 to
0x60: avrdude -p m328p -c stk500v2 -u -v -U lfuse:w:0x60:m
Q: When I access the clock via the web browser then everything is fine. The
LCD display shows however no time. I see just a row of squares.
A: Make a table on paper and note down which ATmega pin name needs to be
connected to which LCD pin name and number. After that check every wire between ethernet board and LCD. There is a mistake somewhere.
Q: What do I have to configure on my DSL router to use this clock?
A: In most cases you will not need to do anything as most home
DSL router run already a DHCP server. Just plug this clock into an
ethernet-jack at the router and that's it.
     
Conclusion
A very nice clock and very pretty to look at ;-). Now you have one
place in your house where you have always exact date and time.
Have fun!
     
References
    
    
  
© Guido Socher, tuxgraphics.org
2013-02-16, generated by tuxgrparser version 2.57