Raspberry Pi Single Board Computer
Real Time Clock for the Raspberry PI
Designed by Mark Little
The Raspberry Pi Does come with a battery-backed Real Time Clock (RTC). For systems connected to the Internet, or a Global Positioning System (GPS) receiver, this is not a problem as the Pi can pick up the time from either a Network Time Protocol (NTP) server, or the GPS satellites. For stand alone systems, however, the system time is lost each time the Raspberry Pi is rebooted.

This limitation can be overcome by adding a battery-backed RTC which will retain the current Date and Time while the Raspberry Pi is powered down. There are a number of RTC modules available for the Raspberry Pi. Some use the I2C interface, while others use a non-standard interface. The RTC described here is one of the non-standard RTC modules that use the DS1302 RTC chip. This RTC was selected because its non-standard interface made them less costly than those with a standard interface.

One of the unfortunate things about the non-standard interface is that the output pins used to control the RTC vary between the various versions of the Raspberry Pi. The program used to control the RTC attempts to take this into account by identifying the model of the Raspberry Pi and adjusting the pins accordingly, although this has not been fully tested as I only have access to a couple of models of the Raspberry Pi.

This program is loosely based on the program "rtc-pi", but has been expanded to allow the RTC to directly read or set the system date/time. The program will also display the current RTC date/time as well as a comparison between the RTC date/time and the system date/time. Complimenting the RTC program is a script that checks whether the system is locked onto an external time source using NTP (network Time Protocol). If it detects that the local NTP service is locked, it will load the system RTC date/time into the RTC, ensuring that the RTC only gets updated when the system time is accurate. The program can also run automatically at system boot so that the system clock is set to an accurate date/time.
Source Files, Libraries & Documentation