Raspberry Pi RTC: Adding a Real Time Clock

In this Raspberry Pi RTC tutorial, we will be showing you how to add either the PCF8523, DSL1307 or DS3231 real-time clock (RTC) modules to your Raspberry Pi.

Raspberry Pi RTC

We will be showing you how each of the individual real-time clock chips needs to be wired up to the Raspberry Pi to function correctly by providing the pin numbers and a helpful GPIO guide.

You will also be learning in this tutorial the changes you need to make to the Raspberry Pi’s configuration as well as modify packages on Raspbian, so it will read the time from your real-time clock module and not utilize the fake time it relies on by default.

We will also be showing you how you set the time on your real-time clock module when required to do so.

Equipment List

Below are all the bits and pieces that I used for this Raspberry Pi RTC tutorial.


Raspberry Pi 2 or 3

Micro SD Card

Power Supply

PCF8523, DSL1307 or DS3231 RTC Modules


Raspberry Pi Case

GPIO Breakout Kit


Breadboard Wire

Wiring your RTC module to the Raspberry Pi

On your RTC Module, you should find at least four connections. Some RTC circuits may come with more, but we only need the following four for it to work with the Raspberry Pi: VCC/5V/Vin (IC Power-supply pin), SDA (Serial Data Line), SCL (Serial Clock Line) and GND (Ground power-supply pin)

You can either connect these lines directly to your Raspberry Pi or connect it to a breadboard and then to the Raspberry Pi. For this tutorial, we utilized the Pi RTC PCF8523 from Adafruit which plugs in directly over the first six pins which significantly simplifies the process of setting up an RTC (Real Time Clock) module.

However, wiring up a normal PCF8523, DSL1307 and a DS3231 isn’t a complicated process, following our guide below you should have everything connected to in no time.

DS3231 & PCF8523

  • Vin connects to Pin 1
  • SDA connects to Pin 3
  • SCL connects to Pin 5
  • GND connects to Pin 6

DS3231 & PCF8523 Raspberry Pi Connection Diagram


  • Vin connects to Pin 4
  • SDA connects to Pin 3
  • SCL connects to Pin 5
  • GND connects to Pin 6

DS1307 Raspberry Pi Connecction Diagram for RTC

Configuring the Raspberry Pi for I2C

Before we begin setting up and utilizing our RTC on the Raspberry Pi, we first have to make use of the raspi-config tool to configure our Raspberry Pi for use with I2C.

1. Let’s begin this tutorial by ensuring our Raspberry Pi is entirely up to date; this ensures that we will be utilizing all the latest software available.

sudo apt-get update
sudo apt-get upgrade

2. With the Raspberry Pi now entirely up to date we can now run its configuration tool to begin the process of switching on I2C. Run the following command to launch the configuration tool.

sudo raspi-config

3. This command will bring up the configuration tool; this tool is an easy way to make a variety of changes to your Raspberry Pi’s configuration. Today, however, we will only by exploring how to enable the I2C interface.

Use the arrow keys to go down and select “5 Interfacing Options“. Once this option has been selected, you can press Enter.

4. On the next screen, you will want to use the arrow keys to select “P5 I2C“, press Enter once highlighted to choose this option.

5. You will now be asked if you want to enable the “ARM I2C Interface“, select Yes with your arrow keys and press Enter to proceed.

6. Once the raspi-config tool makes the needed changes, the following text should appear on the screen: “The ARM I2C interface is enabled“.

However, before I2C is genuinely enabled, we must first restart the Raspberry Pi. To do this first get back to the terminal by pressing Enter and then ESC.

Type the following command into the terminal on your Raspberry Pi to restart it.

sudo reboot

7. Once the Raspberry Pi has finished restarting we need to install an additional two packages, these packages will help us tell whether we have set up I2C successfully and that it is working as intended.

Run the following command on your Raspberry Pi to install python-smbus and i2c-tools:

sudo apt-get install python-smbus i2c-tools

8. With those tools now installed run the following command on your Raspberry Pi to detect that you have correctly wired up your RTC device.

sudo i2cdetect -y 1

If you have successfully wired up your RTC circuit, you should see the ID #68 appear. This id is the address of the DS1307, DS3231 and the PCF85231 RTC Chips.

Once we have the Kernel driver up and running the tool will start to display UU instead, this is an indication that it is working as intended.

Setting up the Raspberry Pi RTC Time

With I2C successfully setup and verified that we could see our RTC circuit then we can begin the process of configuring the Raspberry Pi to use our RTC Chip for its time.

1. To do this, we will first have to modify the Raspberry Pi’s boot configuration file so that the correct Kernel driver for our RTC circuit will be successfully loaded in.

Run the following command on your Raspberry PI to begin editing the /boot/config.txt file.

sudo nano /boot/config.txt

2. Within this file, you will want to add one of the following lines to the bottom of the file, make sure you use the correct one for the RTC Chip you are using. In our case, we are using a PCF8523.







Once you have added the correct line for your device to the bottom of the file you can save and quit out of it by pressing Ctrl + X, then Y and then Enter.

3. With that change made we need to restart the Raspberry Pi, so it loads in the latest configuration changes.

Run the following command on your Raspberry Pi to restart it.

sudo reboot

4. Once your Raspberry Pi has finished restarting we can now run the following command, this is so we can make sure that the kernel drivers for the RTC Chip are loaded in.

sudo i2cdetect -y 1

You should see a wall of text appear, if UU appears instead of 68 then we have successfully loaded in the Kernel driver for our RTC circuit.

5. Now that we have successfully got the kernel driver activated for the RTC Chip and we know it’s communicating with the Raspberry Pi, we need to remove the fake hwclock package. This package acts as a placeholder for the real hardware clock when you don’t have one.

Type the following two commands into the terminal on your Raspberry Pi to remove the fake-hwclock package. We also remove hwclock from any startup scripts as we will no longer need this.

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

6. Now that we have disabled the fake-hwclock package we can proceed with getting the original hardware clock script that is included in Raspbian up and running again by commenting out a section of code.

Run the following command to begin editing the original RTC script.

sudo nano /lib/udev/hwclock-set

7. Find and comment out the following three lines by placing # in front of it as we have done below.


if [ -e /run/systemd/system ] ; then
    exit 0

Replace With

#if [ -e /run/systemd/system ] ; then
#    exit 0

Once you have made the change, save the file by pressing Ctrl + X then Y then Enter.

Syncing time from the Pi to the RTC module

Now that we have our RTC module all hooked up and Raspbian and the Raspberry Pi configured correctly we need to synchronize the time with our RTC Module. The reason for this is that the time provided by a new RTC module will be incorrect.

1. You can read the time directly from the RTC module by running the following command if you try it now you will notice it is currently way off our current real-time.

sudo hwclock -D -r

2. Now before we go ahead and sync the correct time from our Raspberry Pi to our RTC module, we need to run the following command to make sure the time on the Raspberry Pi is in fact correct. If the time is not right, make sure that you are connected to a Wi-Fi or Ethernet connection.


3. If the time displayed by the date command is correct, we can go ahead and run the following command on your Raspberry Pi. This command will write the time from the Raspberry Pi to the RTC Module.

sudo hwclock -w

4. Now if you read the time directly from the RTC module again, you will notice that it has been changed to the same time as what your Raspberry Pi was set at. You should never have to rerun the previous command if you keep a battery in your RTC module.

sudo hwclock -r

You should hopefully now have a fully operational RTC module that is actively keeping your Raspberry Pi’s time correct even when it loses power or loses an internet connection. I hope you have enjoyed this fun Pi project and will put it to good use.

If you have any questions, queries, thoughts, or anything else on this Raspberry Pi RTC tutorial, then be sure to leave a comment over on our forum.


  1. Avatar for Bill Richman
    Bill Richman on

    Excellent guide. Worked perfectly with the Adafruit DS3231 module on a Raspy B+ running Raspbian. Thank you! You saved me a tremendous amount of time poking around trying to figure it out on my own!

  2. Avatar for Gregor Hekro
    Gregor Hekro on

    Great Job.
    When i try on my Rapi 3B+
    i got UU

  3. Avatar for Jesus Davis
    Jesus Davis on

    Mr. Gus! excellent guide!
    PI 3B+ more DS3231 card works on Raspbian Buster.

  4. Avatar for Bob
    Bob on

    Do not forget to use hwclock -s to set the time of the RTC to the PI

  5. Avatar for Alan
    Alan on

    I believe the best RTC clock is accurate to ± 160 s/year = ~4.4ms / day.

    When NTP time is available it would be nice to be able to correct the RTC time; i.e. add/subtract a correction factor on an hourly/daily elapsed time factor from the previous synchonisation.

    This may allow the NTP Deamon to synchonise faster? It can take >20 minutes with a zero time reference.


Leave a Reply

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