Introduction: Raspberry Pi - Real Time Clock (RTC)

Picture of Raspberry Pi - Real Time Clock (RTC)

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

Picture of 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

Picture of 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

Picture of 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!

Picture of 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)!

Comments

rikuzenith (author)2016-10-31

I'm planning my first ever Raspberry Pi project and this tut seems like a very nice resource, thank you! I was just thinking, can you suggest any tips or stuff to read if I'd want to have my Raspberry Pi device to be powered by mains, but allow the real time clock battery take over (to power the clock only), if the device is unplugged from the mains / rebooted for other reason?

KevinL204 (author)2016-05-28

sudo apt-get install i2c-tools

kooth (author)2015-11-24

Very Cool!

JesusG33k (author)2015-11-23

How long can the clock last off the coin cell?

AdrieSentosa (author)JesusG33k2015-11-23

sorry for late reply,

According to sparkfun, a small CR1225 Lithium coin cell battery that will run the RTC for a minimum of 9 years (17 years typical) without an external 5V power supply.

About This Instructable

13,149views

149favorites

License:

Bio: A candle loses nothing of its light when lighting another
Add instructable to: