Introduction: Raspberry Pi - Real Time Clock (RTC)

About: A candle loses nothing of its light when lighting another

In this project, we will be making a Real Time Clock (RTC) which keep the time for your Raspberry Pi even if it is turned off.

Real Time Clock is essential for every Micro-controllers to keep the time even if it is turned off. Without a Network Time Protocol (NTP), the system time and date will almost certainly be wrong. For some projects this is a vital problem especially if you are logging time stamps or performing other time sensitive operations.

It’s a fairly easy project and can be used either on its own or part of something bigger (Check it Out >> Raspberry Pi - Data Logging).

Step 1: List of Materials

For this project, we will be using:

- Raspberry Pi

- DS3231 module cost: ~2.00 USD

- Jumper Wire or Custom Proto Shield (Check it out how to make it >> )

Step 2: Hardware Connections

In general, the connections are very simple. Follow the instructions and images above, and you should have no problems.

Connecting RTC Module

  • the VCC pin on Raspberry's 3V pin (P1-01)
  • the GND pin to Raspberry’s GND (P1-06)
  • the SDA (Serial Data Line) to Raspberry's pin 3, and
  • the SCL (Serial Clock Line) to Raspberry's pin 5

P.S. Raspberry Pi first pin had a square soldering hole.

Step 3: Raspberry Pi Programming

First, setup the module

Update and upgrade your Pi

sudo apt-get update<br>sudo apt-get -y upgrade

Modify your system file

sudo nano /etc/modules

change or add the /etc/modules

snd-bcm2835<br>i2c-bcm2835 <br>i2c-dev<br>rtc-ds1307

Next, setup Raspberry's i2c communication

detect your rtc device

sudo i2cdetect -y 1<br>or<br>sudo i2cdetect -y 0

Modify your system line

sudo nano /etc/rc.local

Add the following two lines before the exit 0 line :

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device <br>hwclock -s

Reboot your pi

sudo reboot

Finally, setup date on RTC

change your Raspberry Pi system time

sudo date -s "20 NOV 2015 18:49:00"

write the rtc module

sudo hwclock -w

match your system and rtc module

sudo date; sudo hwclock -r

Step 4: Enjoy!

The final test is to determine if the RTC module is keeping time and that the Pi will use that time when it boots. The best way to do that is to :

  • Power down Raspberry Pi (sudo halt),
  • Remove the power cable,
  • Remove the network connection ,
    (Pi automatically synchronize time when connect to an internet)
  • Attach the Pi to a monitor and keyboard,
  • Leave it overnight
  • Power it up and use “sudo date” to see what time the Pi thinks it is

After making sure that everything works smoothly, you can take this project into bigger project (Check it Out >> Raspberry Pi - Data Logging)!