Monday, April 4, 2016

Set RTC Time

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.

First, load up the RTC module by running
  • sudo modprobe rtc-ds1307
Then, as root (type in sudo bash) run
  • echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device (if you have a rev 1 Pi)
  • echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device (if you have a rev 2 Pi)
You can then type in exit to drop out of the root shell.

Then check the time with sudo hwclock -r which will read the time from the DS1307 module. 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

raspberry_pi_jan01.gif
First you'll need to get the right time set on the Pi, the easiest way is to connect it up to Ethernet or Wifi - it will automatically set the time from the network. Once the time is correct (check with thedate command), run sudo hwclock -w to write the system time to the RTC

You can then verify it with sudo hwclock -r
raspberry_pi_hwclockset.gif
Next, you'll want to add the RTC kernel module to the /etc/modules list, so its loaded when the machine boots. Run sudo nano /etc/modules and add rtc-ds1307 at the end of the file (the image blow says rtc-1307 but its a typo)
raspberry_pi_addmodule.gif
Then you'll want to create the DS1307 device creation at boot, edit /etc/rc.local by running
  • sudo nano /etc/rc.local
and add:
 echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device (for v1 raspberry pi)
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device (v2 raspberry pi)
sudo hwclock -s (both versions)
before exit 0 (we forgot the hwclock -s part in the screenshot below)


raspberry_pi_etcrclocal.gif
That's it! Next time you boot the time will automatically be synced from the RTC module

No comments:

Post a Comment

Komentar Anda Membantu Membuat Perubahan