Raspberry Pi 3

DS1307 connections
Note the EEPROM can be left out.

DS1307 Real Time Clock Raspberry Pi I2C Interface

by Lewis Loflin

Updated latest Raspbian 2019.

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

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

Some of these may already be installed:

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

Stop! If running latest Rasbian with i2c enabled the DS1307 works by default.

Setting the RTC Clock

The RTC module must be loaded by the kernel by running:

~ $ sudo modprobe rtc-ds1307

Now you need to be running as the super user; type in:

~ $ sudo bash

Then, if you have a Rev.2 Raspberry Pi or later:

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

Or if you have the older Rev.1 Raspberry Pi, use:

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

Then:

~ $ sudo geany /boot/config.txt

Add:

# hardware clock
device_tree=bcm2710-rpi-3-b.dtb
dtparam=i2c_arm=on
dtoverlay=12c-rtc,ds1307



Assuming leafpad is your text editor (if not use geany) 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:

~ $ sudo leafpad /etc/modules

Add if not already there:

i2c-dev
i2c-bcm2708
rtc-ds1307

Save and then exit the editor.

Rebbot Raspberry Pi.

Next you will need to add the DS1307 device creation at boot by editing the /etc/rc.local file by running

~ $ sudo leafpad /etc/rc.local

Add the following lines to the file: before exit:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s
date

Then reboot.

In terminal type sudo i2cdetect -y 1 (enter) and you should see:

This if the DS1307 is recognized:

pi@raspberrypi:~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --        

68 is now UU.

Now let's set the time

.

pi@raspberrypi:~ $ date
Sun Feb 10 13:50:51 EST 2019

Use your current time and date!

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

~ $ date -s "28 APR 2019 10:42:00"

To update the RTC:

~ $ sudo hwclock -w

Check the DS1307 RTC:

~ $ pi@raspberrypi:~ $ sudo hwclock -r

To allow a user such as pi to access i2c from userspace if needed:

~ $ sudo adduser $USER i2c

Reboot and ready to go.