element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • 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
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • 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 5717 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).
  • Former Member
    Former Member over 13 years ago

    Hi there,  a quick question - how do you initially set the date/time of the RTC clock?  Or does it auto update from the ntpd service?  I can see instructions on setting the time of the Raspberry Pi (or whatever system you're using) FROM the RTC, but not the other way.  Also handy for when daylight savings comes around...

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

    Thanks for helping to surface the blog posts.  I see what you mean about the blog slice links now.  I'll open a ticket so we'll fix it for a future release.

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

    Drew Fustini wrote:

     

    Those links do work ok for me looking at it right now.

     

    Oh the links to blog posts in my list on the forum work fine.  It's only the blog slice links that don't work, for example:

     

    • http://www.element14.com/community/groups/raspberry-pi/blog/2012/03

     

    That works perfectly well if you paste it into your browser's address box manually (it shows the blog slice for March), but the forum strips off the slice info "/2012/03" and so the link doesn't work as a slice at all in the forum.

     

    Notice that the slice info has also been stripped from the href in this blog comment post too (the link doesn't take you to the slice here either, it only works properly if you paste the visible URL string manually), so clearly this blog tail stripping is generic element14.com functionality.  No worries, maybe someone will fix it one day.

     

    In regards to the blog functionality, I try to use it for instructional posts like this, but the downside is that I don't think it is as visible as a discussion post.

     

    Yep, it's far more appropriate for instructional posts becaused it places the focus on the actual work and it won't get submerged by discussions.  The lack of visibility we can remedy, I'm trying already. image

     

    Morgaine.

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

    Thanks for putting that list together even though the editing system was being a pain.  Those links do work ok for me looking at it right now.  I do find our system does tend over function in manipulating links.

     

    I typically try to create links to Community content by using the link button, choosing the All Content tab, and then locating the content I want to target.  However, this probably wouldn't work too well when trying to create a large list.

     

    In regards to the blog functionality, I try to use it for instructional posts like this, but the downside is that I don't think it is as visible as a discussion post.

     

    Thanks,

    Drew

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

    Thanks - good catch!  bootc.net did write a SPI driver for the Raspberry Pi, but I'm not sure how easily the DeadOn would integrate with the hwclock utility.  I've removed the link and just left the ChronoDot which is I2C.

    • Cancel
    • Vote Up 0 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 © 2026 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