Introduction: Set Up Real Time Clock (RTC) on Raspberry Pi

Note: There were some issues with the audio on the YouTube video. I've taken it down temporarily and will upload a fixed video and update the link soon. Sorry for the inconvenience.

Raspberry Pi does not include a hardware clock on board. It uses internet access to set the time with the help of NTP servers. Use of a hardware real time clock (RTC) allows the Raspberry Pi to be used without internet access and still provide services that use time stamps. DS1307 is a commonly available I2C based RTC IC. Using the I2C protocol supported by the Raspberry Pi, real time clock support can easily be added with a cost of around $2-3.

Hardware Setup:

1. Obtain a DS1307 board or make one yourself (it's very straightforward).
2. Ensure that the I2C data and clock lines (SDA and SCL) does not have any pull-up resistors. If any pull-ups are present, remove them.
3. Connect a battery (most boards use CR2032 Lithium Cells) to the DS1307.
4. Make the connections to the Raspberry Pi. Refer here for the GPIO header pinouts. 5V, GND, SDA and SCL need to be connected.

Software Setup:

1. Enable i2c on the Raspberry Pi.

Run sudo nano /etc/modprobe.d/raspi-blacklist.conf
Locate the line blacklist i2c-bcm2708
Comment it out by typing a '#' in front. i.e. #blacklist i2c-bcm2708

2. Load the required modules at startup.

Run sudo nano /etc/modules
Add the following line at the end
i2c-bcm2708
i2c-dev
rtc-ds1307


3. Install i2c-tools

Run sudo apt-get install i2c-tools

4. Reboot (Run sudo reboot)

Note: The following commands require root privileges to run. It is easier to run them from a root prompt.
A root prompt can be launched by running sudo bash.


5. Check for I2C connectivity to DS1307

Run i2cdetect -y 0 on Model A
Run i2cdetect -y 1 on Model B

The address 0x68 should be listed if the module is connected properly.

6. Instantiate the DS1307

Run echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device on Model A
Run echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device on Model B

7. Ensure that the system time is set.

8. Initialize the hardware clock

Run hwclock --systohc -D --noadjfile --utc
This will set the time on the DS1307.

9. Verify the hardware clock

Run hwclock -r
If everything is configured correctly, the time and date is displayed.

10. Configure the hardware clock to synchronize on boot up.

Run nano /etc/rc.local
Insert the following lines before the 'exit 0' line

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device on Model A
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device on Model B

sudo hwclock -s

That's it. You're all set.

Comments

duncan_mascarenhas (author)2014-03-16

Hi nprasan,

Just wanted to know the reasoning why you omitted the pull-up resistors?

I'm a newbie to Raspberry or linux or any sort of electronics but have started taking interest in these things. Just as a Hobby. so your reasoning could help educate me. If possible in very simple words.

The this is I wanted to develop a RTC that was depicted here: https://www.instructables.com/id/DIY-RTC-DS1307-Mod... until I came across yours and now 'm totally confused. As also the data sheet of the DS1307 strictly states to use pull-ups on the SDA and SCL lines.

Would appreciate an easy explanation.

Thanks in anticipation...

Hi duncan,

You are right. The datasheet of DS1307 says pull ups are required - and they are indeed required for SDA and SCL.

The raspberry pi board already has pull up resistors connected to the SDA and SCL lines. So, you don't need to add pull ups on the DS1307 board. You may be thinking that an extra set of pull ups wont hurt. But NO. They do hurt.

Let me explain in simple terms. The raspberry pi's SDA and SCL lines operate on 3.3V levels. DS1307 uses a 5V power supply and has SDA and SCL lines that work on either 3.3 or 5 V levels. Now, if you connect a pull up between 5V supply of DS1307 and SDA,SCL, they now operate on 5V logic. But, it still works because the pull ups present on the Pi force it down to 3.3 V. The problem here is that if something happens to the pull ups on the Pi, then 5V pull up will totally fry the Pi. This is why I insisted on not having a 5V pull up present.

I hope you understood. Feel free to clarify any questions you might have.
Good luck!

Hi nprasan,

Really well explained. just had one last query though; for Vcc do I select the 3.3v or 5v?

DS1307 requires 5V as Vcc.

joey56 (author)2014-01-09

I have got raspberry pi for Christmas it's so cool

joey56 (author)2014-01-09

Hi

About This Instructable

35,572views

58favorites

License:

Bio: Engineer. Educator. Entrepreneur.
More by nprasan:Generating Lissajous Figures using a PC/Laptop Sound CardUse your Windows Phone device as a Wireless MicrophoneUse your smartphone mic with your PC
Add instructable to: