Configure RTC (Real Time Clock) on Raspberry PI

RTC IRP102

Configure RTC (Real Time Clock) on Raspberry PI

16 February, 2016 DIY, Raspberry Pi 2

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.

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.

RTC Config 1

RTC Config 1

 Select option 9: Advanced options. Then choose option  A7: I2C.

RTC Config 2

RTC Config 2

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

RTC Config 3

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.

When the installation process have finished, type:

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:

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

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”:

Call it from the “start” section. To be specific, insert “init_rtc_device” call right after this fragment into “hwclocksh()” function:

We also have to comment out/remove udev check as our RTC won’t be handled by it

Now, save file.

5. Module loading at boot

Edit rc.local

and then add these lines just before of exit 0

Get out the editor saving changes.

6. Enable hwclock and disable fake

Update the real HW Clock and remove the fake

Now that real hardware clock is installed, remove the fake package and it’s crons

Next, 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

you should get:

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:

If system time is OK, you can set the RTC time with system time typing.

If the system hasn’t got right time you can set RTC manually this way

To show date and time kept in RTC use:

you get:

To set system date and time from RTC you must use:

 
 

About the author

IngeniApp:

2 Comments

  1. Sergey

    3 July, 2016
    Reply

    IngeniApp team,
    seems that you have a typo in section 4, while moving script to init.d:
    should be
    sudo mv hwclock.sh /etc/init.d/

    • IngeniApp

      4 July, 2016
      Reply

      Hi,
      You are right. Fixed up.

      Thank you

Would you like to share your thoughts?

Your email address will not be published. Required fields are marked *

Leave a Reply

Uso de cookies

Este sitio web utiliza cookies para que usted tenga la mejor experiencia de usuario. Si continúa navegando está dando su consentimiento para la aceptación de las mencionadas cookies y la aceptación de nuestra política de cookies, pinche el enlace para mayor información.plugin cookies

ACEPTAR
Aviso de cookies