If your RaspberryPi project needs to know the date and time without an internet connection you need a real time clock. There are a few available. The cheapest option is AdaFruit’s DS1307 kit at $9.00. This kit works with Arduino and Raspberry pi if you are willing to put it together.
Epoch is a real-time clock designed just for the Raspberry Pi. It attaches right to the GPIO, no assembly required. We’ve priced it without a middleman markup so we could keep it as cheap as possible.
Here is how I got my Epoch Prototype up and running. I started with a fresh raspian installation to be sure I did not miss any steps. I’ve done this all form the command line. Use LX terminal or SSH into your pi.
You can see that although the rtc is present, it is not yet available. First we have to get the tools to talk to I2C.
> sudo apt-get update
> sudo apt-get install git
> sudo apt-get install python-smbus
> sudo apt-get install i2c-tools
Now let’s make sure the I2C is not blacklisted. Edit the /etc/ /etc/modprobe.d/raspi-blacklist.conf file. Comment out the last two lines if this file is present.
Next we have to enable I2C on the pi by editing the /etc/modules file. Add the following lines and reboot.
> sudo nano /etc/modules
> sudo reboot
Now see what is on the I2C bus (bus 1 for newer raspberry pi models 0 for older ones)
And the is the DS3231 chip showing up at address 0x68!
Next let’s install the python interface to this chip (not required: skip to hwclock setup if you don’t need python interaction with the rtc)
I just created a directory called GIT on the pi to download the Epoch library into. You can put it wherever you want.
hwclock setup (skip to here)
Most likely you will want to operating system to control the hardware clock. This takes a few steps to set up. Type the following from the command line. The greater than symbol “>” represents the pi user, whereas the pound sign “#” represents the root user command prompt
> sudo bash
# modprobe rtc-ds1307
# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
# nano /etc/modules
(add the line “rtc-ds1307” and save the file)
# nano /etc/rc.local
(…after logging back in. If you are not connected to the internets set the time manually)
> sudo date –set=”20130511 02:08:00″
> sudo hwclock -w