DS1307 connections
Note the EEPROM can be left out.

DS1307 Real Time Clock Raspberry Pi I2C Interface

by Lewis Loflin

We are setting up our system to use a DS1307 RTC, access I2C, etc. I'm working from official Rasbian version of Raspberry Pi 2 which will work on Raspberry Pi 1. Open a terminal.

First let's do an update of the system if you haven't already:

~ $ sudo su
apt-get update && sudo apt-get dist-upgrade

Then:

raspi-config

This will open the Raspberry Pi setup panel go to advanced options turn in I2C and SPI. Save and "reboot, come back open a terminal and log back in as root. (sudo su)

Then:

$ sudo apt-get install i2c-tools python-smbus

On the official Rasbian for Raspberry Pi 2 RPi.GPIO is already installed.

~ $ wget http://www.bristolwatch.com/rpi/dl/i2c.py
~ $ sudo cp i2c.py /usr/local/lib/python2.7/dist-packages/i2c.py

Assuming leafpad is your text editor (if not use geany or beaver) and you are still logged in as root we need to have the i2c modules loaded by default at boot time so add the following to /etc/modules opened in a text editor:

~ $ leafpad /etc/modules

Add the following:

vchiq
snd_bcm2835
i2c-dev
i2c-bcm2708
spi-bcm2708
rtc-ds1307

Save and then exit the editor.

To make DS1307 clock loadable at startup by opening:

$ sudo leafpad /etc/rc.local
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s

Before exit 0.

Save and exit the editor. Reboot the Pi and log back in as root.

Setup a Hardware Clock

If one has a DS1307 clock module connected to the I2C lines do the following:

~ $ i2cdetect -y 1

Should produce something like this:


     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- 04 -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: 50 51 52 53 54 55 56 57 -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --    

0x68 is your clock and after clock setup will read "UU" that is how you know it's at least there. The 0x04 in this case is an Arduino slave receiver. Check to make sure time-date are correct:

~ $ date

Example one should see Sat Dec 7 23:34:49 EST 2015.

If not correct set the time-date. Change time-date in Linux command line;

~ $ date -s "3 MAY 2015 21:55:00"

Check date again. Then do the following if correct to set DS1307 clock:

~ $ hwclock -w

Then to check if DS1307 clock is set read (-r) DS1307:

~ $ hwclock -r

The correct time-date should be displayed. Disconnect your ethernet connection then reboot and log in. Type "date" and correct time-date should be displayed.




Loading

Linux Videos

Live Linux Distro for Using Printer Port with Electronics
Using the powerful Rox-Filer system in Linux
Use FEH under Linux for a Wallpaper Setter
How to create Symbolic links in Linux

 


donate