[How to] install RTC hardware clock pcf8563 (I2c)

this device operates at 3.3V (from 3 to 5V) and does not need a voltage level shifter , so you can connect SDA and SCL pins of this device directly to the correspondent Gpio pins of the router.
The pcf8563 is more stable and precise than DS1302 or DS1307, also it is fully supported by OpenWRT 17.xx and 18.xx.

- Prerequisites: install I2c interface

see my previous posts about I2c here:
[How To] Add 20x4 LCD (HD44780) to a OpenWrt router via I2C)
[How To] Add barometric pressure and temperature sensor to Openwrt
[How to] connect a DHT12 I2c humidity and temperature sensor to OpenWrt and display values on LCD

I2c Notes: you can connect many I2c devices to the same bus (SDA and SCL), just wire them in parallel, but pay attention to the total current consumption and operating voltage of each device, you cannot connnect in parallel devices operating at different voltages!
Important: for I2c devices operating at 5V, you need a GPIO voltage level converter from 3.3 to 5V like this one for Raspberry:

Level converter connections:

LV = 3.3V from the router
HV = 5V from the router
GND = GND from the router
2xTX1 = SCL and SDA from the router
2xTX0 = SCL and SDA to the I2c device(s)

- Instructions:

opkg update
opkg install kmod-rtc-pcf8563
opkg install hwclock
opkg install ntpclient #(optional)

reboot the router

check the I2c hw address of the RTC:

i2cdetect -y 0

you should get "0x51"

now edit "/etc/rc.local" file and add the following line after the I2c configuration lines (insmod):

/bin/echo pcf8563 0x51 > /sys/bus/i2c/devices/i2c-0/new_device

reboot again

now you can set the RTC with the following commands:

Usage: hwclock [-r|--show] [-s|--hctosys] [-w|--systohc] [--systz] [--localtime] [-u|--utc] [-f|--rtc FILE]

Query and set hardware clock (RTC)

-r	Show hardware clock time
-s	Set system time from hardware clock
-w	Set hardware clock from system time
--systz	Set in-kernel timezone, correct system time
	if hardware clock is in local time
-u	Assume hardware clock is kept in UTC
--localtime	Assume hardware clock is kept in local time
-f FILE	Use specified device (e.g. /dev/rtc2)

- Configuration:

get current time from internet:

/usr/sbin/ntpclient -l -h ntp1.ien.it -c 1 -s &

write the current time obtained from internet into the RTC:

hwclock -w

(you may change "ntp1.ien.it" with your preferred ntp server)


to set system time from hardware clock at boot, add the following line at the end of "/etc/rc.local" file (just before the "exit 0" line:

/sbin/hwclock -s

to read the current time from RTC:

hwclock -r


root@OpenWrt:~# hwclock -r
2019-08-29 08:45:53.221025+02:00

To set the RTC with a custom time (without internet connection):

hwclock --set --date="2019-08-29 06:00:12"  --ut

if you don't want to open your router and solder any pin inside, simply buy an USB to I2C dongle and install kmod-i2c-tiny-usb :grinning:

That is not really necessary as this supposed to be done by /etc/init.d/sysfixtime script on startup.
However, since the RTC device is not yet available at the beginning of the boot process, the script above will fail. To fix this I had to add a line to /etc/rc.local:

/etc/init.d/sysfixtime start
1 Like