This is a short guide on how I installed my Raspberry Pi with a RTC (Real Time Clock) module so it can act as an NTP server as well.
Before doing anything software related we need to obtain all the hardware components needed.
- Raspberry Pi 4
- USB-C Power Supply (5.1V/3.0A)
- Mini RTC Module
- Micro SD Card (> 8GB)
The official documentation recommends you use the Raspberry Pi Imager tool (which is really simple to use), but if you do not want to install that to your system you can do it the manual way instead.
First we need to download the image we want to install. This can be found at the following location: https://www.raspberrypi.com/software/operating-systems
For this install we will use the 64-bit Raspberry Pi OS Lite image.
The Raspberry Pi documentation recommends at least an 8GB SD card, so insert it
into the computer and see what name it is given with the help of the lsblk
command:
$ lsblk
sda 8:0 1 59,5G 0 disk
└─sda1 8:1 1 59,5G 0 part /media/username/random-nameMake sure the card is not mounted by issuing the umount command on the path
specified above (your path will of course be unique to your current setup).
$ sudo umount /media/username/random-nameFrom the folder where the image file was downloaded it is now as easy as this to write it to the SD card:
Two Steps:
xz -dk <image>.xz
sudo dd if=./<image> of=/dev/sdX bs=4M conv=fsync status=progressOneliner:
xzcat -dk <image>.xz | sudo dd of=/dev/sdX bs=4M conv=fsync status=progressIn the write command you need to be very careful when you specify the output
filepath (of=), and in our example it will be /dev/sda since that is the
path of the SD card.
This should yield something like this when the operation completes:
$ lsblk
sda 8:0 1 59,5G 0 disk
├─sda1 8:1 1 256M 0 part
└─sda2 8:2 1 1,6G 0 partThe smaller partition (sda1) is the boot partition, and this can be confirmed
by inspecting its label:
$ sudo blkid
/dev/sda1: LABEL_FATBOOT="boot" LABEL="boot" TYPE="vfat" PARTUUID="0ee3aaaa-01"
/dev/sda2: LABEL="rootfs" TYPE="ext4" PARTUUID="0ee3aaaa-02"When the writing completes we want to mount the SD card again so we can make some additional tweaks to the installation before first startup. Just create a temporary folder and mount the boot partition to it:
sudo mkdir /media/rpi-boot
sudo mount /dev/sdX1 /media/rpi-bootSince 2022-04-04 the pi:raspberry username:password combination no longer
exist, and the boot wizard forces you to create a unique user and password
combination at first boot. However, this is a little bit tricky in the headless
case, so we need to create a userconf.txt file in the boot partition to help
us set up a custom user without the wizard.
Just run the following command
openssl passwd -6and type your desired password and end with enter.
The weird looking string received from the command above should now be used in
the creation of the userconf.txt file. If we want to create the user "admin"
with the password "pass" at first boot of the Raspberry Pi, then we should
enter the following string into the file:
echo 'admin:$6$rKdvqXtSjMThWD3T$ZSrJQb5mYAsTaMlBM8SfVjPFm0g3Le58X.swo98ZGTCk2EgO3tYLktioeoPCdaikHCmiLc8qmHtedcmeI8k011' | sudo tee /media/rpi-boot/userconf.txtIf you want the SSH server to be automatically enabled, in case of headless
install, you just have to make sure there exists a file named ssh in the
boot folder:
sudo touch /media/rpi-boot/sshBefore doing anything else with the Pi I recommend you start it and make sure it boots and you can login to it.
To configure the Real Time Clock on the Raspberry Pi I primarily followed this/this guide, but some info was sourced from the comments in the page where I bought my RTC module.
NOTE: All the steps until Disable the Fake RTC Clock can be done at once without rebooting, however, if this is the first time you do it you should probably verify each step at a time which do require restarts.
When you have verified that the Pi can boot you need to enable the I2C
interface, else nothing will work. This is done by running raspi-config
sudo raspi-configAnd navigating to:
Interface Options --> I2C --> Enable
To help you work with the I2C interface you should also install the following package:
sudo apt install i2c-toolsAfter that you can turn off the Pi, mount the RTC and boot it again.
After the Pi has booted with the RTC module present we need to verify that it is actually detected by the OS.
sudo i2cdetect -y 1This should print something like this where the number 68 is what we want to
see if this was successful.
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
For a modern version of the Raspbian OS it is as simple as adding the following
line to the /boot/config.txt file.
# Activate I2C and enable DS3231 RTC
dtoverlay=i2c-rtc,ds3231The
ds3231string is specific for the RTC module I have chosen, change it if you are using another module.
After rebooting again we should now see UU in the place where 68 was present
before.
sudo i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
The fake RTC clock needs to be removed since it interferes with our real one!
sudo apt remove -y fake-hwclock
sudo update-rc.d -f fake-hwclock remove
sudo systemctl disable fake-hwclockEdit the file /lib/udev/hwclock-set and make sure all the following lines are
commented out:
# if [ -e /run/systemd/system ] ; then
# exit 0
# fi
# /sbin/hwclock --rtc=$dev --systz
# /sbin/hwclock --rtc=$dev --systz --badyearFor me the only thing not commented in that file was the following two lines:
dev=$1
/sbin/hwclock --rtc=$dev --hctosysThe first time you use the RTC clock it might not show the correct time. Use the following command to get its time:
sudo hwclock -rThis might already have been synced in case your Pi has access to the internet, else it is time to do it now so we can get the correct time from some upstream NTP servers.
When the simple date command displays the correct time you can write it to
the RTC clock and then verify it:
sudo hwclock -w
sudo hwclock -rYou should now have a device that is able to keep track of time very accurately, and be able to remember it between power outages and extended periods without internet.