Jul 16, 2015

Adding a RTC to Raspberry Pi


Setting up I2C

sudo apt-get install python-smbus
sudo apt-get install i2c-tools

Run sudo raspi-config and follow the prompts to install i2c support for the ARM core and linux kernel.
Reboot.

Open terminal
sudo nano /etc/modules

and add these two lines to the end of the file:

i2c-bcm2708
i2c-dev

Testing I2C

sudo i2cdetect -y 1


The RTC module must be loaded by the kernel by running:

sudo modprobe rtc-ds1307

Now you need to be running as the super user; type in:

sudo bash

and then, if you have a Rev.2 Pi or later:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

Type exit to stop being the 'root' user.

Check the time on the RTC device using:

sudo hwclock -r

The easiest way is to connect it up to Ethernet or Wifi. It will automatically set the time from the network. You can check the time and write the system time to the RTC board using the date command:

Date & sudo hwclock -w

You will need to add the RTC kernel module to the file /etc/modules so it is loaded when the Pi boots. In a terminal window run

sudo nano /etc/modules

and add rtc-ds1307 at the end of the file.

Next you will need to add the DS1307 device creation at boot by editing the /etc/rc.local file by running

sudo nano /etc/rc.local

and add the following lines to the file:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s
date

just before the exit 0.