SARPi Project - Slacking on a Raspberry Pi
Install a Real Time Clock on a Raspberry Pi running Slackware Linux
This SARPi mini-project will enable you to install and configure a real time clock (RTC) using the I2C interface with Slackware Linux on a Raspberry Pi computer in just a few easy steps. It's prudent and beneficial to add a RTC if your Raspberry Pi computer doesn't come with one built-in (i.e. only the Raspberry Pi 5 has a built-in RTC). In this mini-project we'll be installing a ChronoDot v2.1, which is an extremely accurate real time clock module, based on the DS3231 temperature compensated RTC (TCXO).
Although we'll be installing a DS3231-based RTC you can just about use the same procedure with any [I2C] RTC module; DS1302, DS1307, PCF8563, RV3028, etc. The only real difference are the modules (software drivers) used for each RTC device. For the most common RTCs the correct modules are already available in Slackware Linux. Sometimes different RTCs use the same modules. Sometimes you need to locate and/or build these modules yourself.
RTC on the Raspberry Pi 5
The Raspberry Pi 5 features a built-in I2C real time clock (RTC) that does not require any interface or settings enabling in order to initialise or configure it. It simply works "out of the box" so to speak.
To disable the Raspberry Pi 5 onboard RTC 'dtparam=rtc=off' must be specified in the '/boot/config.txt' file so that your own RTC may be installed and configured accordingly.
Quick Links
• Things you will need for this mini-project.
• Installing a RTC on a Raspberry Pi via the GPIO header.
• Configuring Slackware Linux to detect a RTC.
• Configuring Slackware Linux to use a RTC on a Raspberry Pi.
Older means and methods to install and configure a RTC for Raspberry Pi (1) and those not using Device Tree Blob overlays:
• Installing & configuring a RTC on Slackware Linux without Device Tree overlays.
• Time to get it all working.
• Setting the RTC to the correct time & date.
• The final countdown reboot? (optional).
•
What you will need for this project
• A Raspberry Pi computer with a GPIO header, running Slackware Linux.
• A real time clock module (RTC). Such as a ChronoDot RTC (other RTCs will work too).
• Suitable cables to connect the ChronoDot to your RPi (some RTCs connect directly to the GPIO without cables).
• The i2c-tools package may need to be installed for troubleshooting and diagnostics, etc. [optional]
Slackware Linux includes the i2c-tools package in the source tree. You can use 'slackpkg' or manually download the i2c-tools package to install it.
IMPORTANT! : With Slackware Linux packages, make sure to only install packages for the specific Slackware Linux version that you're running. Packages that are intended for Slackware ARM 15.0 are not designed to work with Slackware AArch64 and vice versa.
•
Installing a RTC on a Raspberry Pi via the GPIO header
If your real time clock module (RTC) plugs directly into the Raspberry Pi GPIO header then you can skip this ection and move on to Configuring the Slackware Linux system for a RTC.
The RTC module we're using in this project is the ChronoDot v2.1 [DS3231SN]. We chose this particular RTC for a number of reasons. It has one of the best reputations for being a very high quality and accurate timekeeper and also features a replaceable battery. Another consideration was because it's already supported by the rtc-ds1307 module under Slackware Linux. The ChronoDot's Maxim DS3231SN temperature compensated controller [TCXO] was also a major factor in selecting this RTC module over the DS1307 and its contemporaries.
This guide also works for most other DS3231-based real time clocks. Such as; the ChronoPi RTC Module, and extremely popular DS3231 MINI RTC Module, which plugs directly in to the Raspberry Pi GPIO without the need for any cables. The RTC device may be of a different design but if it uses the same modules and/or controller then it'll work in the same way throughout.
If your real time clock module doesn't plug directly into the Raspberry Pi GPIO header then you'll need to connect it using cables. This is what's required with the ChronoDot v2.1 RTC.
With everything powered off, follow the diagram below and connect the RTC to your Raspberry Pi GPIO header. One easy way to do this is by using solderless female-female breadboard jumper cables which simply plug-in to the header pins at both ends. The cable length is your prerogative. You can of course make your own connections and cables by using old CD/DVD ROM audio cables, etc. The only important thing is that you ensure the wiring is correct and insulated.
Below is a diagram showing how you need to connect the RTC to a Raspberry Pi GPIO header. This is the standard 40-pin GPIO header which is found on all current Raspberry Pi boards, although it is unpopulated on the Raspberry Pi Zero [W] and Raspberry Pi Zero 2 W. The Raspberry Pi (1) Model B features a 26-pin GPIO header. The first 26-pins of a 40-pin GPIO header on later Raspbery Pi versions are exactly the same configuration as the 26-pin header on a Raspberry Pi (1) Model B.
• ChronoDot VCC --> Raspberry Pi GPIO Pin 1 (3v3 PWR)
• ChronoDot SDA --> Raspberry Pi GPIO Pin 3 (SDA I2C)
• ChronoDot SCL --> Raspberry Pi GPIO Pin 5 (SCL I2C)
• ChronoDot GND --> Raspberry Pi GPIO Pin 9 (Ground GND)
NB: Make sure you connect the ChronoDot to the 3.3v power supply if you're using a standard battery, and NOT the 5v power. Although the ChronoDot is designed to operate on anywhere between 2.3v and 5.5v, we need to be powering it from the (3v3) 3.3 volts supply using GPIO Pin 1. If we were using a rechargeable battery then we'd connect the power to the 5v pin.
For more information on the Raspberry Pi GPIO header see the Raspberry Pi Hardware Documenation.
We're going to insert the 3v Lithium CR1632 coin cell battery to the ChronoDot RTC (which comes supplied). The RTC you have might be the same or come with the battery already soldered to the circuit board. Just make sure the polarity of the battery is correct if you do need to insert one. Connect the cables to the GPIO header on your Raspberry Pi (as shown above) and do a final [double] check of the connections before powering it on, just to be sure.
That's pretty much all of the hard work done.
•
Configuring the Slackware Linux system for a RTC
With your RTC correctly wired and connected, power on your Raspberry Pi and boot into the SLackware Linux operating system.
In this guide we are using Slackware AArch64 Linux on a Raspberry Pi 4 [4GB RAM] with the hostname "torq". It's not important which Slackware Linux version you use, but if you're installing a RTC on a Raspberry Pi (1) with a 26-pin GPIO header then there are some specific requirements that you'll need to be aware of, which are covered later in this guide.
Log in as 'root' user.
At the moment, your Slackware Linux system may not beconfigured to detect or use a real time clock (RTC) that's been installed. This can be verified by using the 'hwclock -v' command:
If that's the case you will see output similar to the following screenshot:
Note the "No usable clock interface found." and "hwclock: Cannot access the Hardware Clock via any known method." output here.
In this event, what you'll need to do is edit the '/boot/config.txt' file and specify a setting to enable your RTC (e.g. the ChronoDot in our case). To do this you'll use a text editor of your choice. We use 'nano'. So we'll type 'nano -w /boot/config.txt' at the command prompt:
Scroll down to the section you see in the screenshot below, titled "# Enable [I2C] DS3231 real time clock [RTC]" and uncomment the line below it. This should be the Device Tree overlay for the I2C RTC DS3231 - 'dtoverlay=i2c-rtc,ds3231' - so it appears like in the screenshot:
NB: If this line does not already exist in your /boot/config.txt file then add it.
If you are installing a DS3231-based RTC then the 'dtoverlay=i2c-rtc,ds3231' setting is correct. If it's a DS1307 then use 'dtoverlay=i2c-rtc,ds1307' instead. Any RTC which appears in the output from using the 'modinfo rtc-ds1307' command can be substituted in this setting, and it's quite a long list.
For other RTCs you are going to need to do some investigating to find out which module supports it. For example, if your RTC uses a PCF8523-based controller then you would add this line instead:
This can be put anywhere in the '/boot/config.txt' file on a line of its own.
Now save and exit the '/boot/config.txt' file.
Next thing to do is to 'reboot' the system to take on this new setting.
Once the system has rebooted, log in again as 'root' user.
•
Configuring Slackware Linux to detect a RTC
Log in, elevate to 'root' user and type 'hwclock -v' at the command prompt:
You should see something similar to the following screenshot:
If you receive any error notifications go back and check your settings in the /boot/config.txt file. If you find any mistakes correct them and reboot your system once more. Your /boot/config.txt file should pretty much match what's displayed in this guide in order to work successfully. Also check the cable connections between the RTC and Raspberry Pi.
However, did you notice the date and time in the output above? It's not correct. In fact, it's years wrong.
You can check the system and hardware clock with the 'date && hwclock -r' commands:
As you can see from our output, the system time and ChronoDot RTC are not in sync. The first output is the system clock and beneath it is the hardware clock (the ChronoDot RTC). Your own results will be completely different but the point is to check if the system and hardware clock are accurate. For us they are not. For you they might be too, especially if you are configuring the RTC for the first time.
So now we need to set the system and hardware clock date/time accurately, and we're going to use a ntp server to achieve this. You can do the same but if you want to use a ntp server you will require an internet connection.
If you don't have an internet connection you can set the system time by using the 'date MMDDhhmmYYYY.ss' command.
MM = 2 digit month (00-12)
DD = 2 digit date (01-31)
hh = 2 digit hours (00-23) [24 hour format]
mm = 2 digit minutes (00-59)
YYYY = 4 digit year
ss = 2 digit seconds (00-59)
So, for example, 07:44:13 PM on 09 March 2024 would be: 'date 030919442014.13'
Incidentally, you can also set the date/time from a ntp server connected to your local network if you have one available.
So, to set the system date/time you can use the 'ntpdate' or 'sntp' commands, followed by a ntp server address. The ntp server we are using as an example is 'pool.ntp.org' but you can select your own here if you prefer. To set the hardware clock date/time you can get it from the system once you know it is relatively accurate. You can achieve this with one simple command.
Now type 'ntpdate pool.ntp.org && hwclock -w' and/or 'sntp -sS pool.ntp.org && hwclock -w' at the command prompt:
What you've effectively done is set the system clock with the 'ntpdate pool.ntp.org' command and subsequently taken that setting and written it to the hardware clock (ChronoDot RTC) with the 'hwclock -w' part of this command.
Now you can use the 'hwclock -r && date' command again to check that the system and hardware clock date/time matches.
If the output of the system date/time and hardware clock date/time are identical then you have successfully installed and configured the RTC on your Slackware Linux system. Well done!
Seeing as you now have a fully working and installed, highly accurate, real time clock there's no need to rely on a ntp server alone at boot time. You can configure the system so that the date/time is set by the hardware clock (i.e. the ChronoDot RTC). If this is something you would like to do then use a text editor to access the '/etc/rc.d/rc.local' file. We'll do this with 'nano':
Your '/etc/rc.d/rc.local' file should look like the screenshot below.
This file grabs the date/time from your RTC when the system has loaded and writes it to the system clock.
You can easily change it so that the system clock is set by a ntp server which updates your RTC as well. It's entirely up to you what to do and how to do it. The following screenshot shows how this might be achieved:
Save and exit the '/etc/rc.d/rc.local' file in 'nano' by pressing the + keys together, then pressing the key when prompted.
You can test these settings by setting a bogus date (i.e. that you know is wrong and reboot the system. So let's do that now:
We have reset our system and hardware clock date to Wed Jan 1 12:00:00 GMT 2020 on purpose.
Now we will reboot the system to test that these settings are working.
After rebooting log in again as 'root' user. At the command prompt type 'date && hwclock -r'. What you should see is that your Slackware Linux system date/time and RTC date/time are identical.
Now you can be sure whenever you boot your Slackware Linux system it will wet the system time accurately, whether that's by ntp server and/or your newly installed and configured RTC.
If, for whatever reason, you need to set the date/time accurately again on your RTC then you can use the 'ntpdate 0.pool.ntp.org && hwclock -w' command to achieve this. Always remember that in order to use this command you must be logged in as the 'root' user.
As far as this guide goes, for installing and configuring the ChronoDot RTC on your Slackware Linux system on a Raspberry Pi, you are done! Contratulations are in order if you have successfully managed to complete this fun little project successfully.
•
Installing & configuring the ChronoDot RTC on Slackware Linux without Device Tree overlays
Installing a realtime clock (RTC) on the Raspberry Pi (1) via the 26-pin GPIO header is a prerequisite. See these instructions on how to achieve this.
You need to follow this section of the guide only if your system does not rely on Device Tree overlays to load and configure hardware at boot time. For the purpose of this section of the guide , we have given the name "myrasbox" to our Slackware Linux system.
Remember, if you want to use a non-Device Tree (pre-DT) system to install and configure your ChronoDot RTC, you'll need to disable Device Tree in the /boot/config.txt file by adding 'device_tree=' on a line by itself.
Log in as (or change to) the 'root' user. If the date is not already set to current time you should now set it manually, or use the 'sntp' command. It's a good idea to make sure your Slackware Linux software and Raspberry Pi kernel and firmware are fully up to date. Running 'slackpkg' at this point will achieve this and, depending on the number of updates available, may take a while to complete.
As 'root' user, you're going to 'cd' to the /tmp directory and download the i2c-tools package. There's two choices; Slackware ARM 15.0, and Slackware AArch64 current.
Note for Slackware Linux packages: Make sure to only install package(s) for the Slackware Linux version you're running. Packages that are built for current hard float port will not work with 14.2 soft float port (and vice versa).
• To install i2c-tools-4.2 for Slackware AArch64 current use 'slackpkg' or download it from a Slackware Linux mirror .
• To download and install i2c-tools-4.2 for Slackware ARM 14.2 type the following:
root@myrasbox:/tmp# wget https://slackware.uk/sarpi/pkg/14.2/i2c-tools-4.2-armv7-1_slack14.2_sp1.txz
root@myrasbox:/tmp# installpkg i2c-tools-4.2-armv7-1_slack14.2_sp1.txz
This i2c-tools Slackware Linux package was created specifically for this project using the i2c-tools.SlackBuild script available from SlackBuilds.org because it didn't seem to be already available for download on the Internet. It goes without saying that i2c-tools for Linux is useful for more than just setting up RTCs on a Raspberry Pi.
Once this has completed you should see something similar to the following output.
After installing the i2c-tools package, you're going to edit the rc.local file and instruct the Slackware Linux system to load the required modules and perform commands at boot time in order to communicate with the RTC. Do this with 'nano' by typing:
You will hopefully see something similar to the screenshot below.
You need to tell the system to load some specific modules. These will be explained later. Enter the following code into the rc.local file below any existing text:
/sbin/modprobe i2c-dev
/sbin/modprobe rtc-ds1307
Pay attention to the next section because it is important (only) for Raspberry Pi (1) Model B owners. Whether you are using a Raspberry Pi (1) Model B rev.1 or rev.2 board depends on the code you need to enter and this is crucial towards success. Raspberry Pi 1 Model A+ and Model B+, Raspberry Pi 2, Raspberry Pi 3, and Raspberry Pi 4 users do not have to bother checking their hardware version(s). If you're unsure which revision you're currently using see this page: Identifying Your Model of Raspberry Pi. Once you have determined which version of Raspberry Pi you are using, you can proceed.
• For Raspberry Pi (1) Model B rev.1 boards you need to enter the following code into the rc.local file below the text you have just entered:
/sbin/hwclock -s
• For Raspberry Pi 3, Raspberry Pi 2, Raspberry Pi (1) Model B rev.2, Model A/A+/B+ boards you need to enter the following code into the rc.local file below the text you have just entered:
/sbin/hwclock -s
The last thing you need to do before saving the rc.local file is to comment out the sntp line by putting a #hash in front of it. This stops the system from attempting to grab the current time from a NTP server over the Internet at boot time. It would be a little pointless to get a time reference from a NTP server when there's a highly accurate real time clock available! :-D
Now you should have a rc.local file looking very similar to the one below.
Once you've finished editing the rc.local file press the + keys together, then press key when asked if you wish to save any changes, and then press key to save the file and exit.
The i2c-bcm2708 module is the low level i2c subsystem driver. The i2c-dev module allows access to devices through the /dev interface. The rtc-ds1307 module contains the drivers for many RTC controllers, such as the DS1337 and MCP79410. Using the 'modinfo rtc-ds1307' command shows you that the DS3231 (i.e. the ChronoDot) is supported within the rtc-ds1307 module, and that's why it's needed at boot time.
The 'echo ds3231 0x68 > /sys/class/i2c-adapter/i2c-1/new_device' command is telling the i2c subsystem that there's a new 'ds3231' device available (the ChronoDot) and where it is on the bus. It's worth noting that this command only works the first time you use it. Any repeated usage will result in an error message.
The '/sbin/hwclock -s' command is asking the real time clock (the ChronoDot) for a time reference and subsequently writing that date and time to the Slackware Linux system.
•
Time to get it all working
So now your rc.local file should be configured to initialise the RTC and use it at boot time. You could 'reboot' at this point to test your setup but there's no real need to do so.
Instead of rebooting your Raspberry Pi, you can simply run the rc.local file to load the modules and perform the commands required to get the RTC working with the system. Type the following code to run the rc.local file:
After running the rc.local file you need to make sure the RTC is recognised by the system and, most importantly, set the correct time and date on it. So, depending on whether your Raspberry Pi is a rev.1 or rev.2 board (check it with this link), type the following:
• For Raspberry Pi (1) Model B rev.1 boards use this command:
• For Raspberry Pi 3, Raspberry Pi 2, Raspberry Pi (1) Model B rev.2, Model A/A+/B+ boards use this command:
The screenshot below is the result you're looking for. It shows that the device has been detected and recognised.
Instead of '68' being displayed you might see 'UU' in its place. This is perfectly fine because it just means that a software driver (the rtc-ds1307 module) has been loaded and is currently using the 0x68 address. In both cases it indicates the RTC has been detected.
So you can go right ahead and find out what time and date is set to on your RTC. If this is the first time you have used it don't expect the current settings to be anywhere near accurate. Type the following command:
The result should be something similar to what you see below. Notice that in our screenshot the current date and time is years out on the RTC. Yours may also be the same. However, this is just a transient error because it's the first time the ChronoDot RTC has been powered on. ;-)
•
Setting the RTC to the correct time & date
This can be achieved in two ways: manually using the 'date' command, or by utilising a NTP server on the Internet with the 'sntp -sS' command. The latter is preferred for accuracy reasons alone.
• Set the time and date on the RTC manually by using the following command (pay attention to the date format):
The month (MM), day (DD), hour (hh), and minute (mm) values are always specified in 2 digits and have a leading zero '0' when applicable. The time is always specified as the 24 hour clock. The year (YYYY) value is always specified by 4 digits.
Example: if the time is 9:17pm on September 11, 2016 then the command would be as follows:
In our example above the 'date 091121172016' command sets the time and date on our Slackware Linux system and the 'hwclock -w' command which follows then writes that date and time to the hardware clock (the ChronoDot RTC).
• Set the time and date on the RTC using a NTP server with the following command:
The 'sntp -sS pool.ntp.org' command grabs the current date and time from a NTP server and writes it to our Slackware Linux system. The 'hwclock -w' command which follows then writes that date and time to the hardware clock (the ChronoDot RTC). The NTP server you use does not have to be the same one as in our example.
NB: The above 'date' and/or 'sntp -sS' command(s) can be used at any time (as 'root' user) to set the time & date on the RTC.
Now type 'hwclock -r' (i.e. READ the hardware clock) to see if you have successfully set the ChronoDot RTC to the correct date and time. You should check the output to make sure it's accurate.
After you have set the date and time the RTC will retain it, even when your Raspberry Pi is turned off, because of the 3v Lithium CR1632 coin cell battery. The ChronoDot real time clock is the most accurate RTC module that we've ever come across and it's really impressed us with ease of use, cost effectiveness, and a totally cool design!
•
The final countdown reboot? (optional)
As final confirmation that you've done everything right you can power off your Raspberry Pi (disconnecting any Ethernet cable or wireless Internet device) and power it back on, booting into your Slackware Linux system. Once again, login as 'root' and type the 'hwclock -r' or 'date' command, or both. If the result is accurate to current time then it's perfect.
When we rebooted our Raspberry Pi, and disconnected it from any Internet connection, a few minutes later we logged in as 'root' and typed the following:
The section of output above from the 'dmesg' command tells us that the i2c modules have been loaded successfully and are working, and that our ChronoDot RTC (DS3231) has been recognised and is configured correctly for use by our Slackware Linux system. We also checked that the date and time was still current and accurate by typing:
The date and time were spot on! If you found the same results, well done! Happy days from now on.
•
Thanks!
Thank you for reading and taking part in this SARPi mini-project. We hope you enjoyed it and found it interesting and educational.
Thanks also to Patrick Volkerding, and the entire Slackware Team, for producing a truly wonderful OS. Without you, and the work you do, the SARPi Project would not exist.
If you have any questions or need help, visit the Slackware Linux Forum on Linux Questions. Or get in touch on the #SARPi IRC channel on irc.libera.chat.
•