Now that we accept the module wired up and verified that you can come across the module with i2cdetect, we tin fix the module.

Don't forget to set up I2C in the previous step!

Raspberry Pi OS's with systemd

This should be the case for any electric current release. For much older releases without systemd, skip to the next section.

Thanks to kd8twg for the hints!

Yous tin can add support for the RTC past calculation a device tree overlay. Run

sudo nano /boot/config.txt

to edit the pi configuration and add whichever matches your RTC chip:

dtoverlay=i2c-rtc,ds1307

or

dtoverlay=i2c-rtc,pcf8523

or

dtoverlay=i2c-rtc,ds3231

to the end of the file

raspberry_pi_dtoverlay.png

Relieve information technology and run sudo reboot to start over again. Log in and run sudo i2cdetect -y ane to see the UU show up where 0x68 should be

raspberry_pi_UU.png

Disable the "fake hwclock" which interferes with the 'existent' hwclock

  • sudo apt-get -y remove fake-hwclock
  • sudo update-rc.d -f fake-hwclock remove
  • sudo systemctl disable fake-hwclock

raspberry_pi_removehwclock.png

Now with the fake-hw clock off, yous can first the original 'hardware clock' script.

Run sudo nano /lib/udev/hwclock-gear up and comment out these three lines:

#if [ -eastward /run/systemd/system ] ; so
# exit 0
#fi

raspberry_pi_hwclock-set.png

Likewise comment out the two lines

/sbin/hwclock --rtc=$dev --systz --badyear

and

/sbin/hwclock --rtc=$dev --systz

pi_a___b___2__3_image.png

Sync time from Pi to RTC

When you commencement plug in the RTC module, it's going to have the wrong time because it has to be set once. You can e'er read the time direct from the RTC with sudo hwclock -r

(ignore utilize of deprecated -D parameter)

raspberry_pi_invalidclok.png

Yous can see, the date at start is invalid! Yous can set the correct time easily. First run appointment to verify the time is correct. Plug in Ethernet or WiFi to let the Pi sync the right time from the Internet. One time that's done, run sudo hwclock -due west to westrite the time, and another sudo hwclock -r to read the time

raspberry_pi_setrtc.png

Once the time is set, make sure the money cell battery is inserted and then that the time is saved. You only have to set the time in one case

That's it! Next time you lot kick the fourth dimension will automatically be synced from the RTC module

hwclock: ioctl(RTC_RD_TIME) to /dev/rtc0 to read the time failed: Invalid argument

If you are getting an mistake message like this when trying to read/write to the RTC, make sure you lot have a good money cell battery installed.

Raspbian Wheezy or other pre-systemd Linux

Commencement, load upwardly the RTC module past running

sudo modprobe i2c-bcm2708
sudo modprobe i2c-dev
sudo modprobe rtc-ds1307

And then, as root (type in sudo bash) run

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

If yous happen to accept an sometime Rev 1 Pi, type in

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

You can so type in exit to drop out of the root shell.

Then check the time with sudo hwclock -r which volition read the fourth dimension from the DS1307 module. If this is the first fourth dimension the module has been used, information technology will report back Jan 1 2000, and you'll need to set the time

raspberry_pi_jan01.gif

Beginning yous'll demand to become the right time assail the Pi, the easiest style is to connect it up to Ethernet or Wifi - it will automatically set the time from the network. Once the time is right (check with the date command), run sudo hwclock -westward to write the system time to the RTC

You tin then verify information technology with sudo hwclock -r

raspberry_pi_hwclockset.gif

Next, y'all'll want to add together the RTC kernel module to the /etc/modules list, then its loaded when the auto boots. Run sudo nano /etc/modules and add rtc-ds1307 at the end of the file (the prototype below says rtc-1307 merely its a typo)

raspberry_pi_addmodule.gif

Older pre-Jessie raspbian is a trivial dissimilar. First upwardly, 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/course/i2c-adapter/i2c-ane/new_device (for v2 raspberry pi)
sudo hwclock -s (both versions)

earlier exit 0 (nosotros forgot the hwclock -southward part in the screenshot below)

raspberry_pi_etcrclocal.gif

That'south it! Next time yous kick the time will automatically be synced from the RTC module

This guide was first published on Aug 31, 2012. Information technology was last updated on Aug 31, 2012.

This folio (Set RTC Time) was concluding updated on Mar 04, 2022.

Text editor powered by tinymce.