Some people want to know how to use RTC on BBG/BBB. For stand-alone projects with no network connection, you will not be able to keep the time when the power goes out. So in this project we will show you how to add a low cost battery-backed RTC to your BBG to keep time!
This tutorial will show you how to use Grove RTC on BBG, and this tutorial requires a BBG running the Debian or Angstorm distribution. Other distributions may work, but are not tested as part of this tutorial.
Connect the Grove RTC module to BBG as the following picture shows.
The Grove RTC working voltage is 5V, so I connect the the red line to the P9_5 pin, which can provide 5V power.
I assume you have already connect your BBG yo your PC via Micro USB cable. After about 10 seconds open your web browser and input http://192.168.7.2/ in the address bar. And open the Cloud9 IDE.
1. Detect the Grove RTC i2c address
# i2cdetect -y -r 1
Now you can see 0x68 is the i2c address of Grove RTC.
2. Set RTC Time
Now, we can set up the module, execute the following:
# echo ds1307 0x68 > /sys/calss/i2c-adapter/i2c-1/new_device
Now you can check the time which will beread from the DS1307 module:
# hwclock -r -f /dev/rtc0
If this is the first time the module hasbeen used it will report back Mar 1 2015, and you'll need to set the time.
The quickest way to set the time on theBeagleBone Green is to execute the following:
# ntpdate -b -s -u pool.ntp.org
To validate the date and time were setcorrectly, execute:
root@beaglebone:~# ntpdate -b -s -u pool.ntp.org root@beaglebone:~# date Sat Jan 30 07:57:00 UTC 2016
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/rtc0
You can verify it was set correctly byexecuting the following command to read the date and time from the DS1307 RTC:
# hwclock -r -f /dev/rtc0
3. Create a service
Next, let's create a service that will runeach time you boot up your BBG. To start, create a directory and script thatwill be executed:
# 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:
#!/bin/bashsleep 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:
# vim /lib/systemd/system/rtc-ds1307.service
Copy the following contents into that file,and save it:
[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 toactually 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 makethis even better. One would be for better error checking, and failure modes inthe bash script. You could check to ensure that the ds1307 was actuallyconnected prior to enabling it. This is just a barebones example of how to getit working!