Set RTC Time

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:
Download: file
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:
Download: file
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:
Download: file
/usr/bin/ntpdate -b -s -u pool.ntp.org

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

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

Download: file
date
#or more comprehensively (the RTC value will be inaccurate, we haven't set it yet):
timedatectl
Now that the system time is set correctly, you can execute the following to write the system time to the DS1307:
Download: file
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:
Download: file
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:
Download: file
mkdir /usr/share/rtc_ds1307
nano /usr/share/rtc_ds1307/clock_init.sh
Now, with the nano text editor open, copy the following into the clock_init.sh script:
Download: file
#!/bin/bash
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:
Download: file
nano /lib/systemd/system/rtc-ds1307.service
Copy the following contents into that file, and save it:
Download: file
[Unit]
Description=DS1307 RTC Service

[Service]
Type=simple
WorkingDirectory=/usr/share/rtc_ds1307
ExecStart=/bin/bash clock_init.sh
SyslogIdentifier=rtc_ds1307

[Install]
WantedBy=multi-user.target
After saving the file, we'll need to actually enable the service so it starts each time as the system boots:
Download: file
systemctl enable rtc-ds1307.service
You can always manually start and stop the service as well:
Download: file
systemctl start rtc-ds1307.service
systemctl stop rtc-ds1307.service
That's it! Reboot your system and check that it all works:
Download: file
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

Download: file
# 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]

script
exec /usr/share/rtc_ds1307/clock_init.sh >> /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 May 21, 2019.