Adding a hardware clock to Raspberry Pi (DS3231)

The popular clock module is uses the DS1307 real time clock chip, which is not very precise. For just a few dollars more, you can get a module with a much more precise DS3231 RTC chip. The one I got was SunFounder's module from Amazon for $9 (free shipping for Prime). It plugs right onto the Raspberry Pi's 40 pin header and doesn't even interfere with the plastic case I have. I have a Model B, but this module should also work on Model A and Model B+. The instructions refer to DS1307, but the chips use the same I2C commands, so it also works for DS3231. I changed some references that appear in 'standard' instruction from ds1307 to ds3231, but you could instead leave them as ds1307 just as well.

To install the module, run the following commands (from SunFounder's instructions). Turns out Amazon's product page also has good comments and perhaps more up-to-date than SunFounder's.

# Comment out the blacklist entry so the module can be loaded on boot 
sudo sed -i 's/blacklist i2c-bcm2708/#blacklist i2c-bcm2708/' /etc/modprobe.d/raspi-blacklist.conf
# Load the module now
sudo modprobe i2c-bcm2708
# Notify Linux of the Dallas RTC device (use -0 for Model A or -1 for Model B)
echo ds3231 0x68 | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
# Test whether Linux can see our RTC module.
sudo hwclock

You should see a response with what the chip thinks is the date. I think mine said 1999 or some such. No problem.

If your system date is NOT correct (if you don't have an internet connection or something), set the date using ONE of the following commands, with the appropriate date and time, of course:

# Only do ONE of two the below:
sudo dpkg-reconfigure tzdata
sudo date -s "Sep 27 2014 12:46:00"
# Now that your system date is correct, simply use this command to transfer the system date to the chip:
sudo hwclock -w


Next, to read the time from the RTC chip and set the system time from it at every boot, open /etc/rc.local and add these two lines above the exit 0 line:

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

We can also disable the ntp daemon and fake-hwclock during boot. 

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

That's it. Pretty easy.


You can still sync the system time from the internet using...

sudo ntpd -gq
sudo hwclock -w