(This post is a re-write of the easy to follow instructions and more instructions available from AdaFruit.com with some original added python stuff. Thanks AdaFruit!)
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.
> sudo nano /etc/blacklist.conf
Next we have to enable I2C on the pi by editing the /etc/modules file. Add the following lines and reboot.
i2c-bcm2708
i2c-dev
> 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)
> sudo i2cdetect -y 1 ## use -y 0 for revA
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.
> mkdir GIT
> cd GIT
> git clone https://github.com/wyolum/Epoch.git
> cd Epoch
It works!
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)
(while still root user add the following two lines to /etc/rc.local and save)
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device…
sudo hwclock -s
# nano /etc/rc.local
# reboot
(…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
> date