This tutorial requires a Raspberry Pi running a kernel with the RTC module and
DS1307 module included. Current Raspbian distros have this, but others may not!
The Raspberry Pi is designed to be an ultra-low cost computer, so a lot of things we
are used to on a computer have been left out. For example, your laptop and computer
have a little coin-battery-powered 'Real Time Clock' (RTC) module, which keeps time
even when the power is off, or the battery removed. To keep costs low and the size
small, an RTC is not included with the Raspberry Pi. Instead, the Pi is intended to be
connected to the Internet via Ethernet or WiFi, updating the time automatically from
the global ntp (nework time protocol) servers
For stand-alone projects with no network connection, you will not be able to keep the
time when the power goes out. So in this project we will show you how to add a low
cost battery-backed RTC to your Pi to keep time!
Wiring the RTC
To keep costs low, the Raspberry Pi does not include a Real Time Clock module.
Instead, users are expected to have it always connected to WiFi or Ethernet and keep
time by checking the network. Since we want to include an external module, we'll
have to wire one up.
We have three different RTC we suggest, PCF8523 is inexpensive, DS1307 is most
common, and DS3231 is most precise. Any of them will do!
While the DS1307 is historically the most common, its not the best RTC chipset, we've
found!
Adafruit PiRTC - PCF8523 Real Time
Clock for Raspberry Pi
This is a great battery-backed real time
clock (RTC) that allows your Raspberry Pi
project to keep track of time if the power
is lost. Perfect for data-logging, clockbuilding,...
https://www.adafruit.com/product/3386
Adafruit PCF8523 Real Time Clock
Assembled Breakout Board
This is a great battery-backed real time
clock (RTC) that allows your
microcontroller project to keep track of
time even if it is reprogrammed, or if the
power is lost. Perfect for...
https://www.adafruit.com/product/3295
Adafruit DS1307 Real Time Clock
Assembled Breakout Board
This is a great battery-backed real time
clock (RTC) that allows your
microcontroller project to keep track of
time even if it is reprogrammed, or if the
power is lost. Perfect for...
The datasheet for the DS3231 explains
that this part is an "Extremely Accurate
I²C-Integrated RTC/TCXO/Crystal". And,
hey, it does exactly what it says...
https://www.adafruit.com/product/3013
Don't forget to also install a CR1220 coin cell. In particular the DS1307 wont work at all
without it and none of the RTCs will keep time when the Pi is off and no coin battery is
in place.
to install the helper software and then sudo i2cdetect -y 1 at the command line,
you should see ID #68 show up - that's the address of the DS1307, PCF8523 or
DS3231!
When you first plug in the RTC module, it's going to have the wrong time because it
has to be set once. You can always read the time directly from the RTC with sudo
hwclock -r
(ignore use of deprecated -D parameter)
You can see, the date at first is invalid! You can set the correct time easily. First run da
te to verify the time is correct. Plug in Ethernet or WiFi to let the Pi sync the right
time from the Internet. Once that's done, run sudo hwclock -w to write the time,
and another sudo hwclock -r to read the time
Once the time is set, make sure the coin cell battery is inserted so that the time is
saved. You only have to set the time once
You can then type in exit to drop out of the root shell.
Then check the time with sudo hwclock -r which will read the time from the DS1307
module. If this is the first time the module has been used, it will report back Jan 1
2000, and you'll need to set the time
First you'll need to get the right time set on the Pi, the easiest way is to connect it up
to Ethernet or Wifi - it will automatically set the time from the network. Once the time
is correct (check with the date command), run sudo hwclock -w to write the system
time to the RTC
You can then verify it with sudo hwclock -r
Next, you'll want to add the RTC kernel module to the /etc/modules list, so its loaded
when the machine boots. Run sudo nano /etc/modules and add rtc-ds1307 at the end
of the file (the image below says rtc-1307 but its a typo)