The Raspberry PI does not have an internal clock so on power up it gets the time from the internet. But if there is no internet the clock reverts to a default date and time (1st January 1970 10:00AM in my time zone). An RTC module usually has a DS1307 chip that can keep time for about 2 years with a small cell battery the module plugs on to the GPIO pins and updates the date and time on power up. Here is a guide on how to install and set-up RTC.
Buy or build RTC moduleReal Time Clock (RTC) modules can be purchased from EBay for a few dollars. Search on EBay for "Raspberry PI RTC".
Connect RTC module
Attach the module to the Raspberry PI. Most have a plug that plugs straight on to the GPIO pins. The I2C uses pins 3 and 5 and the module usually uses pin 1 and 9 for power.
Check and update time and date settings
- In terminal type sudo raspi-config to set country and time zone,
- Restart system if changes are made
Enable i2c the communication to the RTC module
Install I2C interface
- First make sure the system is up to date
sudo apt-get update
sudo apt-get upgrade
- Enable the I2C interface
- sudo raspi-config select Advanced the enable I2C and set to load by default.
- Install I2C softwaresudo apt-get install i2c-tools
- Reboot the Raspberry PI
Configure the I2C interface
- Add the following lines for the DS1307 chip support
- Type sudo nano /etc/modules add the below linesi2c-bcm2708i2c-devrtc-ds1307Press ctrl y then enter to save
- Restart system
- Then check the interface is working
i2cdetect -y 1 (Note:i2cdetect -y 0 on Model A)
- Module detected at address 68
Start the RTC module
- sudo su
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
- Check the hardware clock is working
- set the local time if required
sudo date --set="Jan 14 10:00:00 AEDT 2016"
Note:Use date command to check the time zone
- Set the hardware clock from the system date time
sudo hwclock -w
Update the Raspberry PI time on boot
- Add the configuration to the start up file
- sudo nano /etc/rc.local
- Add lines just before the exit 0
echo ds1307 0x68 > /sys/class/itc-1/new_device Installs the device
sudo hwclock -s Sets the system time from the hardware clock