Now that we have the module wired up and verified that you can see the module with i2cdetect, we can set up the module.

Now, execute the following:
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
Now you can check the time which will be read from the DS1307 module:
hwclock -r -f /dev/rtc1
If this is the first time the module has been used it will report back Jan 1 2000, and you'll need to set the time.

The quickest way to set the time on the BeagleBone Black is to execute the following:
/usr/bin/ntpdate -b -s -u

(note on recent Debian OS insatlls you might need to omit the /usr/bin/ part and just run 'ntpdate -b -s -u') 

To validate the date and time were set correctly, execute:

#or more comprehensively (the RTC value will be inaccurate, we haven't set it yet):
Now that the system time is set correctly, you can execute the following to write the system time to the DS1307:
hwclock -w -f /dev/rtc1
You can verify it was set correctly by executing the following command to read the date and time from the DS1307 RTC:
hwclock -r -f /dev/rtc1
Next, let's create service that will run each time you boot up your BBB. To start, create a directory and script that will be executed:
mkdir /usr/share/rtc_ds1307
nano /usr/share/rtc_ds1307/
Now, with the nano text editor open, copy the following into the script:
sleep 15
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s -f /dev/rtc1
hwclock -w
Next, we'll create a service that will get started on boot, and execute the script we just created:
nano /lib/systemd/system/rtc-ds1307.service
Copy the following contents into that file, and save it:
Description=DS1307 RTC Service


After saving the file, we'll need to actually enable the service so it starts each time as the system boots:
systemctl enable rtc-ds1307.service
You can always manually start and stop the service as well:
systemctl start rtc-ds1307.service
systemctl stop rtc-ds1307.service
That's it! Reboot your system and check that it all works:
shutdown -r now
There are a few things you could do to make this even better. One would be for better error checking, and failure modes in the bash script. You could check to ensure that the ds1307 was actually connected prior to enabling it. This is just a barebones example of how to get it working!

Ubuntu Upstart

If you are using Ubuntu and you'd like to have the RTC taken care of by 'upstart' here's some code to do that, place in /etc/init/rtc-ds1307.conf

# Ubuntu upstart file at /etc/init/rtc-ds1307.conf
# Enables DS1307 RTC Breakout

pre-start script
mkdir -p /var/log/rtc-ds1307/
end script

start on runlevel [2345]
stop on runlevel [06]

exec /usr/share/rtc_ds1307/ >> /var/log/rtc-ds1307/rtc-ds1307.log
end script

This guide was first published on Jul 17, 2013. It was last updated on Jul 17, 2013.

This page (Set RTC Time) was last updated on Jul 15, 2013.

Text editor powered by tinymce.