avr ntp clock.

23,391

Circuit Image

The Network Time Protocol (NTP) has transformed global timekeeping, allowing accurate time and date information from anywhere in the world. NTP is a straightforward UDP-based protocol that can be implemented in a microcontroller. While UDP and TCP servers have been previously implemented, a clock functions as a client, which is a new approach. A server simply responds to incoming IP packets, making it easier to implement. However, NTP packets are short, enabling the addition of an NTP client to the existing network stack. This allows for the creation of a clock equipped with an LCD display and a built-in web server, facilitating configuration and current time display. The NTP protocol is detailed in RFC958 and essentially consists of a 64-bit timestamp: 32 bits represent the seconds in UTC (GMT) since January 1, 1900, and the remaining 32 bits represent fractions of a second. NTP can achieve high accuracy, but for practical purposes, only the seconds are evaluated.

The AVR NTP clock synchronizes with an NTP server at startup and uses a timer interrupt to maintain local time. Synchronization occurs every hour, but if the DSL router is off overnight, the clock continues to function. An onboard crystal is utilized to minimize drift during periods without internet connectivity. The clock operates as a counter, leveraging the 32-bit NTP timestamp by incrementing a 32-bit variable. The initial counter setting is obtained via NTP, and thereafter, it counts up each second. The code documentation specifies that an ATmega168 is required for this implementation, as the ATmega88 lacks sufficient processing capability. The complexity arises from converting seconds since a reference day into a human-readable format, which demands substantial computational resources on an 8-bit processor like the AVR. The addition of the NTP client, web server, and LCD driver exceeds the 8KB limit of the ATmega88, while the ATmega168 provides ample space, utilizing only two-thirds of its capacity.

The clock features a 16x2 LCD display with blue backlighting and an AVR web server SMD board mounted on an acrylic sheet. Power is supplied by an old Ericsson mobile phone charger, which outputs 5V DC and connects to the clock via a 1.5m cable. The charger, acquired for 2 Euros on eBay, is lightweight due to its switched power supply design. The clock displays a message indicating an Ethernet link is active but the NTP server is unreachable. It attempts to synchronize every hour, so an interrupted internet connection will show this message, although the clock continues to run from the internal crystal. The clock has its own IP address, which must be reserved in the DSL router's DHCP settings to prevent conflicts with other devices on the network. The NTP server IP is a public address accessible via the internet, and configuration changes are secured with a default password, "secret," which must be entered to apply any modifications.

The schematic for this NTP-based clock system includes the microcontroller (ATmega168), the LCD display, the Ethernet module for network connectivity, and the power supply circuitry. The microcontroller interfaces with the LCD to display time and status messages, while the Ethernet module handles communication with the NTP server. The power supply circuit ensures stable voltage regulation, allowing the system to operate reliably. The layout should consider proper grounding and trace widths to accommodate the current requirements of the components. The overall design emphasizes modularity, enabling future enhancements such as additional features or connectivity options.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. So far we have only implemented UDP and TCP servers. A clock needs however to be a client. This is something new. A server just answers t o incoming IP packets it is therefore a bit easier to implement. However NTP is a short packet and it is therefore possible to add a NTP client to the existing network stack. We can therefore build a clock which has not only a LCD display but also a build-in webserver. This can then be used to configure the clock or see the current time. 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 the clock locally. This will minimize the drift even if no Internet connection was possible for a couple of days. 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. If you had already a look at the code you might have noticed that the included README file says that one needs an atmega168 for this and a atmega88 not sufficient. Why should such a simple counter not fit into an atmega88 chip. The problem is the math to convert seconds since day-X into a human readable format. This requires quite heavy math for a microcontroller. AVR is a 8-bit processor therefore 32-bit math is expensive. Add the NTP client, the web-server and the LCD driver and you are above the 8Kb available in the atmega88.

An atmega168 has however more than sufficient space. It fills not more than 2/3 of the atmega168. You can therefore easily add additional functions to this wall clock if you want. The 16x2 LCD display with blue backlight and the AVR webserver SMD board are mounted on an acrylic glass sheet. For the power supply I used an old Ericsson Mobile phone charger (not visible on the picture). It produces 5V DC and is very light as it is a switched power supply. It plugs directly into the wall socket and the 5V DC output is then connected via a 1. 5m cable to the clock. I got the charger at ebay for 2 Euro. `|`: ETH link up but NTP sever could not be reached yet. 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. Clock own IP: This is the clock`s own IP address. The address under which you can reach it from your local network via web browser. It has to be a free address from your DSL router address range. You should reserver this address in the DSL router by binding it to a MAC address. This is to avoid that the router`s DHCP server gives the address by accident to other computers in your network.

The mac-address to IP configuration can usually be done under a menu point called "Lan IP settings" or "DHCP configuration" in your DSL router. NTP server IP: this is an IP address of a public NTP server on the Internet. You find some other examples of addresses in the source code (see download, file main. c) or from. Password: password to protect the configuration settings. The default is "secret". You have to enter the correct password to apply any changes. I 🔗 External reference