element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog What time is it?  How to add a RTC to the Raspberry Pi via I2C
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: fustini
  • Date Created: 18 Jul 2012 9:32 PM Date Created
  • Views 5269 views
  • Likes 3 likes
  • Comments 12 comments
Related
Recommended
  • research
  • i2c
  • rpiintermediate
  • rtc
  • raspberry_pi
  • raspberrypi
  • ds1307

What time is it?  How to add a RTC to the Raspberry Pi via I2C

fustini
fustini
18 Jul 2012
imageWant to build this project?
View Parts

 

UPDATE: Adafruit shows how to interace with the DS1307 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:

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

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):

image

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:

image

(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:

image

(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:

image

image

 

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 B Raspberry Pi model BRaspberry Pi
Buy Now
Optical mouse Basic USB optical mouseIONE / Pro Signal
Buy Now
Raspberry Pi power supply 120-240v to 5V power supply with micro USB connectorRaspberry Pi
Buy Now
Keyboard Basic USB keyboardGear Head / A4 Tech
Buy Now
Pre-programmed 4GB SD card 4GB Class 4 SD card preloaded with Debian 6 LinuxSamsung
Buy Now
DS1307 Real Time Clock Breakout DS1307 I2C real time clock on breakout boardAdafruit
Buy Now
4ch Logic Level Converter 4 channel 5V to 3.3V bidirectional logic level converterAdafruit
Buy Now
Adafruit Pi Cobbler GPIO breakout kit for Raspberry PiAdafruit
Buy Now
Breadboard Basic breadboard with 830 connection pointsTwin Industries
Buy Now
Jumper wire bundle Jumper wires for use on breadboardBud Industries
Buy 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.

  • Sign in to reply

Top Comments

  • Former Member
    Former Member over 13 years ago in reply to Former Member +1
    Looks like it hwclock --systohc (assuming the system time is correct).
  • fustini
    fustini over 12 years ago

    A member of my hackerspace, Joe Walnes, designed a nifty low-profile breakout board for the Pi called the Pi Crust: http://picru.st/   We discoverted that the Adafruit RTC correctly fits on the Pi Crust i2c header, so it's the most compact way I've seen to add a RTC to the Pi:

     


    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • bad_boy_y
    bad_boy_y over 12 years ago

    a other think to put in my «to do» list

     

    realy thank's for all this spec

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • fustini
    fustini over 13 years ago

    Recently, I had been wondering whether the logic level converter was really needed:Re: Is level shifting really needed for I2C?

     

    Well, it just so happens that Adafruit has posted their own Raspberry Pi DS1307 RTC tutorial, and they don't use any logic level converter for the 5V DS1307 RTC.  This is because they instruct:

    When building the kit, leave out the 2.2K ohm resistors - by leaving them out, we force the RTC to communicate at 3.3V instead of 5V, which is better for the Pi!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • fustini
    fustini over 13 years ago in reply to Former Member

    Yup, thanks for pointing that out, Gareth.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 13 years ago in reply to Former Member

    Looks like it hwclock --systohc (assuming the system time is correct).

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube