Want to build this project?
View Parts

 

UPDATE: Adafruit shows how to interace with the DS1307 RTCDS1307 RTC without a level converter in their Adding a Real Time Clock to Raspberry Pi tutorial

 

Yesterday, I described how to access the Raspberry Pi's serial console via the GPIO header.  Another capability that can accessed from that header is the I2C bus.  I2C stands for Inter-Integrated Circuit and is common serial bus for chips to talk to each other.  Although not written for the Pi, Tronixstuff's Arduino and the I2C bus is a good introduction to how the bus works.  Here's a short video from NXP that the tutorial highlights:

 

 

Unlike most computers, the Pi lacks a real-time clock (or RTC).  This means the Pi doesn't know what time it is when powered on.  If connected to a network, then the Pi could retrieve the current time from a time server (referred to as NTP).  Another option is to connect an external real-time clock to the Pi via the I2C bus.  I already had a DS1307 Real Time Clock breakout board kit from Adafruit:

http://www.adafruit.com/images/medium/ds1307rtc_MED.jpg

However, the DS1307 operates at 5V and requires a logic level converter to safely connect to the Pi's 3.3V I2C pins.  Furthermore, since I2C is bi-directional, a logic level converter should be choosen which explicitly states it will work with I2C.  I choose Adafruit's 4-channel I2C-safe Bi-directional Logic Level Converter:

http://www.adafruit.com/images/medium/ID757_MED.jpg

From the eLinux wiki, the GPIO header pinout show the I2C lines are on GPIO 0 (SDA) and GPIO 1 (SCL):

GPIOs.png

SDA is the Serial Data line, and SCL is the Serial Clock line.  Here is a photo of the RTC board connected via the logic level shifter to the Pi:

20120714_034608.jpg

(From Left to Right: Adafruit 4-channel I2C-safe logic level converter, Adafruit DS1307 RTC board, Adafruit 8-channel logic level converter [unused], SparkFun logic level converter board [connected to FTDI cable for serial console])

 

 

Raspberry Pi

Pin

Jumper

Wire

Logic Level

Converter:

LV side (3.3V)

 

Logic Level

Converter:

HV side (5V)

Jumper

Wire

DS1307

RTC

3V3 Power

Red

LVHVRed5V*
GroundGreenGNDGNDBlackGND*

GPIO 0

(SDA)

YellowA3B3YellowSDA

GPIO 1

(SCL)

BlueA4B4BlueSCL

 

*NOTE: The 5V FTDI cable provides 5V power to the DS1307.  The DS1307 "5V" pin is connected to "VCC" on the 5V FTDI cable, and the DS1307 "GND" pin is connected to "GND" on the 5V FTDI cable.  Alternatively, the DS1307 could instead be powered by the 5V pin on the Pi's GPIO header.

 

Here's a close up of the wiring:

20120715_011650.jpg

(From Left to Right: Adafruit 4-channel I2C-safe logic level converter, Adafruit DS1307 RTC board, TMP102 temperature senor [more info in a future post], SparkFun logic level converter board [connected to FTDI cable for serial console])

 

I'll now turn my attention to the software side now that the wiring is done.  bootc.net a terrific resource for I2C on Pi.  First, download the bootc.net Debian Wheezy image which includes v3.2 of the Linux kernel and I2C drviers.  Boot the Pi from that image and execute these simple commands from I2C and the Raspberry Pi (as root):

 

Register the DS1307 RTC chip with the I2C address of 0x68:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device

View the current date & time stored in the RTC:

hwclock -r

Set the Linux system time to the value in the RTC:

hwclock -s

Here's screenshots of the process:

20120714_034405.jpg

20120715_011535.jpg

 

These commands could be added to a startup script so that the Pi retrieves the current date/time from the RTC while booting up.

 

Finally, the DS3231 is a higher precision I2C RTC than the DS1307.  The ChronoDot by Macetech features this chip.  It also has the advantage of being able to operate at 3.3V, so a logic level converter isn't needed.

 

Cheers,

Drew

http://twitter.com/pdp7

 

 

 

Product Name*DescriptionSupplier
Raspberry Pi Model BRaspberry Pi Model BRaspberry Pi model BRaspberry Pi
Buy NowBuy Now
Optical mouseOptical mouseBasic USB optical mouseIONE / Pro Signal
Buy NowBuy Now
Raspberry Pi power supplyRaspberry Pi power supply120-240v to 5V power supply with micro USB connectorRaspberry Pi
Buy NowBuy Now
KeyboardKeyboardBasic USB keyboardGear Head / A4 Tech
Buy NowBuy Now
Pre-programmed 4GB SD cardPre-programmed 4GB SD card4GB Class 4 SD card preloaded with Debian 6 LinuxSamsung
Buy NowBuy Now
DS1307 Real Time Clock BreakoutDS1307 Real Time Clock BreakoutDS1307 I2C real time clock on breakout boardAdafruit
Buy NowBuy Now
4ch Logic Level Converter4ch Logic Level Converter4 channel 5V to 3.3V bidirectional logic level converterAdafruit
Buy NowBuy Now
Adafruit Pi CobblerAdafruit Pi CobblerGPIO breakout kit for Raspberry PiAdafruit
Buy NowBuy Now
BreadboardBreadboardBasic breadboard with 830 connection pointsTwin Industries
Buy NowBuy Now
Jumper wire bundleJumper wire bundleJumper wires for use on breadboardBud Industries
Buy NowBuy Now

 

*Products and resources listed are listed to help members build their own Pi Projects. They are suggestions and listed for educational purposes. For substitutions of any parts, please post a question asking the original author.