}

How to add a real time clock to a Raspberry Pi 2

Created:

Introduction

DS3231 RTC clock connected to the Raspberrypi 2

When a raspberry pi is connected to the internet it will retrieve the date from the using a network time protocol (NTP). But if the raspberrypi is not connected to the internet the date will reverts to 31 Dec 1969 everytime it restarts. This is due to the Raspbery Pi lacks of a real time clock (RTC). In this guide we will explain how to add an RTC to the Raspberry Pi using I2C. In this guide we are going to use the i2c modules with the DS3231, but it could be used for other models.

Step 1: Connect the pins

The RTC has four pins that need to be connected: VCC, GND, SDA, SCL. Check the VCC that your RTC requires, the raspberry pi on pin 2 gives 5v and 3.3v on pin 1. The DS3231 RTC required 3.3V DC supply. The GPIO of the raspberry pi that we are going to use are:

This pin connection correspont ONLY yo raspberrypi 2 model B+

  • Pin 1 (VCC 3.3v) <-> VCC.
  • Pin 9 (GND) <-> GND.
  • Pin 3 (GPIO SDA1 I2C) <-> SDA.
  • Pin 5 (GPIO SCL1 I2C) <-> SCL.

Step 2: Enable i2c module

To enable the i2c module we need to enable it, follow this steps:

sudo vim /boot/config.txt

Append to the end of the file the following:

dtparam=i2c1=on
dtparam=i2c_arm=on

Now append to the /boot/cmdline.txt the following:

bcm2708.vc_i2c_override=1

The file should have something similar to the line above. Don't copy and paste this, just make sure is similar (don't add a new line):

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes bcm2708.vc_i2c_override=1 rootwait

Finnally append two new lines to the /etc/modules-load.d/modules.conf

i2c-dev
i2c-bcm2708

Reboot your raspberrypi.

sudo reboot

Step 3: Testing the RTC

Now we need to install i2c tools for testing:

sudo apt-get install i2c-tools
sudo i2cdetect -y 1

The output should be:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Step 4: Adding the device

sudo su -c 'echo ds3231 0x68 > /sys/class/i2c-adapter/i2c-1/new_device'

Now update the rtc time, first we will make sure that the current date is ok:

date
# Sat Feb 25 00:30:21 UTC 2017
sudo hwclock --systohc
# check the time from the ds3231
sudo hwclock -r

For enable RTC at boot there are several ways to configure it. The most simple is to add it to the /etc/rc.local the following:

sudo echo ds3231 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s
``` bash

You can also use an init script on /etc/init.d, but we will not cover it in this tutorial.

Finnaly we need to disable fake-hwclock and optionally disable ntp:


sudo update-rc.d ntp disable sudo update-rc.d fake-hwclock disable


Reboot and check that the time is the correct one. If you want you can enable ntp, but the idea is to use the RTC and have less processes running.

# Appendix

## All GPIO are with number or ON

If you see the i2cdetect output like this:

``` bash
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
70: 70 71 72 73 74 75 76 77

We found this issue with the tiny rtc using the ds1307, we fixed the issue using another RTC with the chip DS3231 and throwing the tiny rtc. How ever please verify your connections.