
Configure RTC (Real Time Clock) on Raspberry PI
Raspberry PI hasn’t got a real time clock, because that we need an internet connection in order to keep right date and time. This may be a problem if we are working on an off-line application and we need to have date and time when the system starts.
There are a lot of integrated circuits which implement this function. By means of a cell or super-cap, they help us to keep the date and time. Most frequently used I.C. are DS1307 and DS3231, both from Dallas.
In this tutorial we are going to put the focus in other I.C, similar to DS1307 but cheaper. It is MCP7490N, from Microchip. This device is integrated in IRP-102 Hat Board.
Though is very similar to DS1307, there are some small differences to make it works with Raspberry Pi. This guide has been done with Raspbian as operating system.
Follow these steps:
1. System update:
The first step is ensuring that our operating system is up to date. For that type these commands in terminal.
1 2 |
sudo apt-get update sudo apt-get -y upgrade |
2. Enable I2C
We have to check that I2C bus is enabled. If not, we must to switch it on. Run the de configuration assistant this way.
1 |
sudo raspi-config |

RTC Config 1
Select option 9: Advanced options. Then choose option A7: I2C.

RTC Config 2
Then you be asked if we want to enable I2C. Choose YES.

RTC Config 3
Get out of raspi-config. In the next restart of the system i2c modules will be loaded by default.
3. Install i2c-tools
This is an utility package for I2C managing.
1 |
sudo apt-get install i2c-tools |
When the installation process have finished, type:
1 |
i2cdetect -y 1 |
This command scans all devices connected to I2C bus. The second parameter means the BUS ID, we must use 1 for Raspberry PI B+, B(rev 2) and A+, For the old model B (rev 1) we use 0 as bus ID. We’ll get something like that:
1 2 3 4 5 6 7 8 9 10 |
0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- 6f 70: -- -- -- -- -- -- -- -- |
You have to check device in position 6F is showed. This means RTC have been detected.
4. Setting up hardware RTC
First, you can replace the file /etc/init.d/hwclock.sh with this. This file is already modified.
Or just
1 2 3 |
wget http://ingeniapp.com/sources/rtc/hwclock.sh sudo cp /etc/init.d/hwclock.sh /etc/init.d/hwclock.sh.orig sudo mv hwcloch.sh /etc/init.d/ |
from shell and continue with step “5. Module loading at boot”.
If you prefer, you can do changes yourself modifing /etc/init.d/hwclock.sh to initialize clock’s i2c device.
Create RTC initialization function, in first lines of the file, after the line containing “unset TZ”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
init_rtc_device() { [ -e /dev/rtc0 ] && return 0; # load i2c and RTC kernel modules modprobe i2c-dev modprobe i2c:mcp7941x # iterate over every i2c bus as we're supporting Raspberry Pi rev. 1 and 2 # (different I2C busses on GPIO header!) for bus in $(ls -d /sys/bus/i2c/devices/i2c-*); do echo mcp7941x 0x6f >> $bus/new_device; if [ -e /dev/rtc0 ]; then echo "RTC found on bus `cat $bus/name`" break; # RTC found, bail out of the loop else echo 0x6f >> $bus/delete_device fi done } |
Call it from the “start” section. To be specific, insert “init_rtc_device” call right after this fragment into “hwclocksh()” function:
1 2 3 4 5 6 7 |
case "$1" in start) # If the admin deleted the hwclock config, create a blank # template with the defaults. if [ -w /etc ] && [ ! -f /etc/adjtime ] && [ ! -e /etc/adjtime ]; then printf "0.0 0 0.0n0nUTC" > /etc/adjtime fi |
We also have to comment out/remove udev check as our RTC won’t be handled by it
1 2 3 4 5 6 7 8 |
... init_rtc_device # Raspberry Pi doesn't have udev detectable RTC # if [ -d /run/udev ] || [ -d /dev/.udev ]; then # return 0 # fi ... |
Now, save file.
5. Module loading at boot
Edit rc.local
1 |
sudo nano /etc/rc.local |
and then add these lines just before of exit 0
1 2 |
# RTC /etc/init.d/rcS |
Get out the editor saving changes.
6. Enable hwclock and disable fake
Update the real HW Clock and remove the fake
1 2 |
sudo update-rc.d hwclock.sh enable sudo update-rc.d fake-hwclock remove |
Now that real hardware clock is installed, remove the fake package and it’s crons
1 2 3 |
sudo apt-get remove fake-hwclock sudo rm /etc/cron.hourly/fake-hwclock sudo rm /etc/init.d/fake-hwclock |
Next, reboot
1 |
sudo reboot |
and check if /dev/rtc0 is present and/or “sudo hwclock -r” successfully gets time from RTC.
Also, you check if everything is OK typing this command on shell
1 |
i2cdetect -y 1 |
you should get:
1 2 3 4 5 6 7 8 9 10 |
0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- UU 70: -- -- -- -- -- -- -- -- |
UU characters means that device at this position is in use.
7. Set date and time in RTC
You can set date and time in the clock I.C in two ways, by hand or by setting the RTC time with the system time assuming that system time is valid. Check this with date command:
1 2 |
date wed feb 10 13:12:03 CET 2016 |
If system time is OK, you can set the RTC time with system time typing.
1 |
sudo hwclock -w |
If the system hasn’t got right time you can set RTC manually this way
1 |
sudo hwclock --set --date "8/2/2016 12:10:45" |
To show date and time kept in RTC use:
1 |
sudo hwclock -r |
you get:
1 |
wed 10 feb 2016 13:17:51 CET -0.688154 seconds |
To set system date and time from RTC you must use:
1 |
sudo hwclock -s |
Would you like to share your thoughts?
Your email address will not be published. Required fields are marked *