Real Time Clock (RTC) interfacing with RaspberryPi

Real Time Clock (RTC) is a computer clock that keeps track of current time. It provides date, month, year and time. Raspberry Pi doesn’t include hardware clock on the board. It uses internet access to set the time with the help of NTP servers. Use of hardware RTC allows the Raspberry Pi to be used without internet access and still provide services that use time stamps. DS-3231 is an I2C based RTC IC. Using I2C protocol supported by the Raspberry Pi RTC support can be easily added.

rtc IMG_20150216_162009008_HDR


  1. With your Raspberry Pi switched OFF connect the RTC board to the GPIO pins.
  2. Insert a coin battery into the battery holder of RTC board.
  3. Using RTC board without a battery installed may damage the RTC and will stop it from appearing on the I2C bus.
  4. Check the connection before powering ON Raspberry Pi.

Configuring Raspberry Pi for RTC:

Step1: Open the Terminal

If you are using Raspian Linux3.18 or later you need to go into Raspberry Pi config utility and enable I2C.

                         sudo raspi-config

Go to advance option and select A7I2C and enable I2C then reboot (sudo reboot)

Step 2: Install I2C-tools.

           sudo apt-get install python-smbus

                        sudo apt-get install i2c-tools

Step 3: By default the I2C and SPI protocols are turned OFF so you need to enable them by editing raspi-blacklist.conf file. (This is only for versions before raspbian linux3.18)

            sudo nano  /etc/modprobe.d/raspi-blacklist.conf

In the file if you see two lines, you need to disable the blacklist of these by adding # character before each line:

#blacklist spi-bcm2708

                        #blacklist i2c-bcm2708

Save your changes and exit the nano editor.

All versions:

Step 4: Set the Raspberry Pi to start I2C automatically at boot by editing /etc/modules.

           sudo nano /etc/modules

Use cursor keys to move to the last line, before exit 0 and add a new line and then add:


Save your changes and exit the nano editor.

Step 5: To avoid having to run the I2C tools at root add the ‘pi’ user to the I2C group:

          sudo adduser pi i2c

Step 6: Next you need to update your Raspberry Pi to ensure all the latest packages are installed:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

Step 7: Next reboot the Raspberry Pi:

           sudo reboot

Step 8: After rebooting you will have now I2C and also you can check by using:

sudo i2cdetect -y 0  (if using v1 Raspberry Pi or)
sudo i2cdetect -y 1 (if using v2 Raspberry Pi)

Then you can see a window like this. The RTC should be appearing on channel 68 as shown bellow. If RTC does not appear then check the battery installed correctly and fully charged. If you installed your RTC before then in channel 68 will be shown as UU.


Step 9: Now you have to enable I2C

           sudo nano /etc/modprobe.d/raspi-blacklist.conf

In that file you will find I2C line where the I2C is black listed

blacklist i2c-bcm2708

Comment it out by adding # character before that line.

                        #blacklist i2c-bcm2708

Step 10: Set the bash shell to root by typing (sudo bash) and then run:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device                              (if you have a version 1 Raspberry Pi or)
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device                             (if you have a version 2 Raspberry Pi)

Type (exit) to drop out of the root bash shell.

Step 11: If you want to read current time on RTC

sudo hwclock –r

If this is the first time you connecting RTC then it will show a date of January 1st 2000.

Step 12: If your Raspberry Pi is connected to the internet then the correct time and date should be set automatically otherwise you need to set current date and time by,

date -s “20 MARCH 2015 18:00:00”

To write current time and date onto RTC using

sudo hwclock –w

Verify the date has been saved onto the RTC Pi with:

            sudo hwclock –r

Now your Raspberry Pi is running with the current time and date.

Step 13: Next step is to make it load when the Raspberry Pi boots. Edit the modules file:

           sudo nano /etc/modules

Add the bellow line at the end of the and save it.


Step 14: Next you want to add the RTC Pi at boot by editing /etc/rc.local

          sudo nano /etc/rc.local

Add the following at the bottom of the page above exit 0.

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device (for version2 RPi)

sudo hwclock –s

And finally reboot the Raspberry Pi using (sudo reboot)



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s