Question 11.13: You want your Raspberry Pi to remember the time, even when i......

You want your Raspberry Pi to remember the time, even when it has no network connection.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Use an RTC (real-time clock) module.

A very common RTC chip is the DS1307. It has an I2C interface and can be bought as a ready-to-use module that includes the chip itself, a quartz crystal for it to maintain accurate timekeeping, and a battery holder for a 3V lithium cell.

To make this recipe, you will need:

• A DS1307 or compatible RTC module (see “Modules” on page 381). You can also use an aLaMode board (Recipe 14.13).

• Female-to-female jumper wires (see “Prototyping Equipment” on page 380)

The RTC module that you are using must be 3.3V compatible. That means that its I2C interface should either have no pull-up resistors at all, or they should pull up to 3.3V and NOT 5V. When using the Adafruit model here, simply don’t include the two resistors when soldering together the module. If you have a ready-made module, carefully remove any pull-up resistors.

Assemble the RTC module if it is in kit form, remembering to omit pull-up resistors and then connect the module to the Raspberry Pi, as shown in Figure 11-17.

The DS1307 is an I2C module, so your Raspberry Pi must be set up to work with I2C (see Recipe 8.4). You can check that the device is visible using I2C tools (Recipe 8.5).

$ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: — — — — — — — — — — — — —
10: — — — — — — — — — — — — — — — —
20: — — — — — — — — — — — — — — — —
30: — — — — — — — — — — — — — — — —
40: — — — — — — — — — — — — — — — —
50: — — — — — — — — — — — — — — — —
60: — — — — — — — — 68 — — — — — — —
70: — — — — — — — —

The 68 in the table indicates that the RTC module is connected to the I2C bus at hex address 68.

If you are using one of the original Raspbery Pi model B, revision 1, boards, then use 0 after the y option in the preceding line. The revision 1 boards were distinctive for having a black audio socket.

You now need to run the following commands so that the RTC can be used with a program called hwclock.

$ sudo modprobe rtc-ds1307
$ sudo bash
$ echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

Once again, if you are using a revision 1 board, change i2c-1 to i2c-0.

You can now access the RTC using the following command:

$ sudo hwclock -r
Sat 01 Jan 2000 00:08:08 UTC -0.293998 seconds

As you can see, the clock is not currently set.

To set the time on the RTC module, you first need to make sure that your Raspberry Pi has the right time. If your Pi is connected to the Internet, this should happen automatically. You can check this using the date command:

$ date
Tue Aug 20 06:42:47 UTC 2013

If the time is wrong, you can also set it manually using date (Recipe 3.33). To transfer the Raspbery Pi system time onto the RTC module, use the following command:

$ sudo hwclock -w

You can then read the time using the -r option:

$ sudo hwclock -r
Wed 02 Jan 2013 03:11:43 UTC -0.179786 seconds

The RTC having the correct time is pointless unless it is used to set the correct system time in Linux when it reboots. To arrange for this to happen, you need to make a few configuration changes.

First, edit /etc/modules (using sudo nano /etc/modules) and add rtc-ds1307 to the end of the list of modules. If you have already added some modules while setting up I2C, SPI, and other options, your file might look something like this:

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with “#” are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
i2c-bcm2708

i2c-dev
spidev
rtc-ds1307

Next, you need two commands to run automatically during startup to set the system time. So edit the file /etc/rc.local using the command sudo nano /etc/rc.local, and just before the final exit 0 line, insert the following two lines. Remember to change i2c-1 to i2c-0 if you are using a revision 1 Pi.

$ echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
$ sudo hwclock -s

When you finish, the file should look something like this:

#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ “$_IP” ]; then
printf “My IP address is %s\n” “$_IP”
fi
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s
exit 0

Now, when you reboot, your Raspbery Pi should set its system time from the RTC. However, if there is an Internet connection available, this will take precedence for setting the time.

Discussion

An RTC is not essential for a Raspberry Pi by any means, since a Raspberry Pi that is connected to the Internet will automatically connect to a network time server and set its own clock. However, you may not always be using the Pi when connected to a network, in which case a hardware RTC is a good option.

The aLaMode (Recipe 14.13) Arduino-based interface board for Raspberry Pi also includes an RTC module that will work with this arrangement.

See Also

AB Electronics have a neat RTC that plugs directly into the GPIO socket. This is shown in Figure 11-18.

This recipe is based on a tutorial from Adafruit.

Table A-8. Modules
Raspberry Pi camera module Adafruit: 1367, MCM: 28-17733, CPC: SC13023
Arduino Uno SparkFun: DEV-11021, Adafruit: 50, CPC: A000066
Level converter, four-way SparkFun: BOB-11978, Adafruit: 757
Level converter eight-way Adafruit: 395
LiPo boost converter/charger SparkFun: PRT-11231
PowerSwitch tail Adafruit: 268
16-channel servo controller Adafruit: 815
Motor driver 1A dual SparkFun: ROB-09457
RaspiRobot board Sparkfun: KIT-11561, raspirobot.com
PiFace digital interface board MCM: 83-14472, CPC: SC12827
Humble Pi MCM: 83-14637, CPC: SC12871
Pi Plate Adafruit: 801
Gertboard MCM: 83-14460, CPC: SC12828
Breakout board with paddle terminals MCM: 83-14876, CPC: SC12885
PIR motion detector Adafruit: 189
Venus GPS module SparkFun: GPS-11058
Methane sensor SparkFun: SEN-09404
Gas sensor breakout board SparkFun: BOB-08891
ADXL335 triple-axis accelerometer Adafruit: 163
4×7-segment LED with I2C backpack Adafruit: 878
Bicolor LED square-pixel matrix with I2C backpack Adafruit: 902
PiLite interface board Ciseco, CPC: SC13018
aLaMode interface board Makershed: MKWY1, Seeedstudio: ARD10251P
Freetronics Arduino LCD shield www.freetronics.com
RTC module Adafruit: 264
16 x 2 HD44780 compatible LCD module SparkFun: LCD-00255, Adafruit: 181
Table A-3. Prototyping equipment
Description Suppliers
M-M jumper wires SparkFun: PRT-08431, Adafruit: 759
M-F jumper wires SparkFun: PRT-09140, Adafruit: 825
F-F jumper wires SparkFun: PRT-08430, Adafruit: 794
Half-sized breadboard SparkFun: PRT-09567 Adafruit: 64
Pi Cobbler Adafruit: 1105
11.17
11.18

Related Answered Questions

Question: 11.8

Verified Answer:

Keypads are arranged in rows and columns, with a p...
Question: 11.2

Verified Answer:

Record the last state of the button and invert tha...
Question: 11.1

Verified Answer:

Connect a switch to a GPIO pin and use the RPi.GPI...
Question: 11.7

Verified Answer:

Use a rotary (quadrature encoder) connected to two...
Question: 11.9

Verified Answer:

Use a PIR (passive infrared) motion detector modul...
Question: 11.12

Verified Answer:

The solution to this is very similar to that of us...
Question: 11.11

Verified Answer:

There are at least two ways to solve this problem....
Question: 11.10

Verified Answer:

A 3.3V serial GPS module can be connected directly...