Skip to content

Instantly share code, notes, and snippets.

@JonasAlfredsson
Last active September 8, 2022 20:25
Show Gist options
  • Save JonasAlfredsson/b4b5b1eb20c3f86afbcd3c41a1b74a8a to your computer and use it in GitHub Desktop.
Save JonasAlfredsson/b4b5b1eb20c3f86afbcd3c41a1b74a8a to your computer and use it in GitHub Desktop.
A short guide on how I installed my Raspberry Pi with a RTC (Real Time Clock).

raspberry-install

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.

Preparations

Before doing anything software related we need to obtain all the hardware components needed.

  1. Raspberry Pi 4
  2. USB-C Power Supply (5.1V/3.0A)
  3. Mini RTC Module
  4. Micro SD Card (> 8GB)

Installation

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.

Download Image

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.

Prepare the SD Card

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-name

Make 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-name

Write Image to the SD Card

From 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=progress

Oneliner:

xzcat -dk <image>.xz | sudo dd of=/dev/sdX bs=4M conv=fsync status=progress

In 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 part

The 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"

Headless Setup

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-boot

Create a User

Since 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.

Create the Encrypted Password

Just run the following command

openssl passwd -6

and type your desired password and end with enter.

The userconf.txt File

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.txt

6. Enable SSH Server

If 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/ssh

Configure RTC

Before 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.

Preparations

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-config

And 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-tools

After that you can turn off the Pi, mount the RTC and boot it again.

Verify Detection

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 1

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

Load the RTC Module

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

The ds3231 string 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: -- -- -- -- -- -- -- --

Disable the Fake RTC Clock

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-hwclock

Enable the Real RTC Clock

Edit 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 --badyear

For me the only thing not commented in that file was the following two lines:

dev=$1
/sbin/hwclock --rtc=$dev --hctosys

Verify Correct Time

The first time you use the RTC clock it might not show the correct time. Use the following command to get its time:

sudo hwclock -r

This 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 -r

You 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment