In this tutorial, we will be showing you how to add either the PCF8523, DSL1307 or DS3231 real-time clock (RTC) modules to your Raspberry Pi.
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 Raspberry Pi OS, 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.
Recommended
- Raspberry Pi ( Amazon ) 2 or newer
- Micro SD Card ( Amazon )
- Power Supply ( Amazon )
- PCF8523, DSL1307 or DS3231 RTC Modules ( Amazon )
Optional
- Raspberry Pi Case ( Amazon )
- GPIO Breakout Kit ( Amazon )
- Breadboard ( Amazon )
- Breadboard Wire ( Amazon )
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)
- 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
DS1307
- Vin connects to Pin 4
- SDA connects to Pin 3
- SCL connects to Pin 5
- GND connects to Pin 6
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 update
sudo apt 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 “3 Interface 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 “I5 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 install python3-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/firmware/config.txt
” file.
sudo nano /boot/firmware/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.
DS1307
dtoverlay=i2c-rtc,ds1307
PCF8523
dtoverlay=i2c-rtc,pcf8523
DS3231
dtoverlay=i2c-rtc,ds3231
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 -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 a hashtag (#
) in front of it as we have done below.
Find
if [ -e /run/systemd/system ] ; then
exit 0
fi
Replace With
#if [ -e /run/systemd/system ] ; then
# exit 0
#fi
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.
date
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
Conclusion
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 below.
FYI
/boot/config.txt
The file you are looking for has moved to
/boot/firmware/config.txt
Hi Henk,
Thank you for the heads up about that. I have corrected the path within the tutorial.
Kind regards,
Emmet
Excellent guide. Worked perfect with DS3231 module on a Raspy4.
Thank you very much. Greatings from Berlin.
Robby
Excellent and easy to follow guide. Thank you!
Well written instructions for the non programmers like myself.
It worked first time and again with my second HamPi for WSJT-X application.
Thank you for sharing.
Well done.
73 vk2byf
Everything works , include a ferret reboot. but when power supply it’s off, time it’s reset over rtc to 1999.. I try everything..
Hi Diego,
May I ask what RTC you are utilizing with your Raspberry Pi. Most of these chips require some sort of power source to keep the time even when the device is unplugged. Typically this is through a button battery like the CR2032.
Cheers,
Emmet
Gus, I’ll just echo what others have said – Excellent guide! Clear, easy to follow and worked without a hitch.
Thank you, excellent guide. Worked first time.
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!
Great Job.
When i try on my Rapi 3B+
dtoverlay=i2c-rtc,ds3231
i got UU
#dtoverlay=i2c-rtc,ds3231
works
Mr. Gus! excellent guide!
PI 3B+ more DS3231 card works on Raspbian Buster.
Do not forget to use
hwclock -s
to set the time of the RTC to the PII 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.
Alan