Setup a RTC Module with Ubuntu 24.04 on a Raspberry Pi 4

YouTube Video
Introduction
Equipment Used
Installing The RTC Module
Connecting to the Pi Over SSH
Install Required Software
Check the Current Date and Time
Detect the RTC Module
Set the RTC Modules Date and Time
Optional: Disable the Timesyncd Service…


This content originally appeared on DEV Community and was authored by dev_neil_a

  • YouTube Video
  • Introduction
  • Equipment Used
  • Installing The RTC Module
  • Connecting to the Pi Over SSH
  • Install Required Software
  • Check the Current Date and Time
  • Detect the RTC Module
  • Set the RTC Modules Date and Time
  • Optional: Disable the Timesyncd Service
  • Conclusion

YouTube Video

If you would prefer to watch a video of this article, there is a video version available on YouTube below:

Introduction

Recently, I upgraded my Raspberry Pi 4 B 8GB to Ubuntu 24.04 Server and as part of the build, I needed to setup an RTC (real time clock) module that I previously had installed and configured with Ubuntu 20.04 Server to keep the date and time if it couldn’t get it from a NTP (network time protocol) server.

To no one’s surprise, some changes have been made over the years which meant the previous article I wrote that covered the setup of an RTC module in Ubuntu 20.04 would no longer work.

As such, I figured I would put together a new article that covered the steps needed.

The steps in this guide should also work on a Raspberry Pi 3 B+ but only on the server edition of Ubuntu 24.04. That is mostly due to memory requirements for Ubuntu 24.04.

Equipment Used

Here is the hardware and software I used for this project:

  • A Raspberry Pi 4 B 8GB model
  • A FLIRC Case
  • A Raspberry Pi 4 power adapter
  • A SanDisk 128GB MicroSD card with Ubuntu 24.04 installed
  • A MicroSD to SD-Card adapter
  • A SD-Card reader
  • A GPIO-based RTC Battery Module with a DS1307 chipset
  • A CAT 6a / 7 Ethernet cable
  • The Raspberry Pi Imager application
  • Ubuntu Server 24.04 64-bit for Raspberry Pi (downloaded via the Raspberry Pi Imager tool).

Installing The RTC Module

To begin with, you will need to check which chipset is on your RTC module. You should be able to find this by looking at the text on the module or you can find it from the vendors description from where you bought it, which might be easier.

You can then proceed to install the module onto the GPIO pins on the Pi. For my RTC module, I needed to install it onto pins 1, 3, 5, 7 and 9 but yours might be different.

GPIO Pins Used

I placed a bit of electric tape over the RTC module as it was going into an aluminium case which it shouldn't come into to contact with, but I figured it's better to be safe than sorry.

Installed RTC Module

You can then install the MicroSD card and connect the Ethernet cable and power adapter to the Pi.

Connecting to the Pi Over SSH

SSH is used to remotely connect to Ubuntu (or Linux in general) to allow you to run terminal commands. You will need to use a terminal application to use SSH. Which application you use depends on your computers Operating System. Some examples that are available include:

  • Linux: Terminal, X-Term or the console screen if you have no GUI installed
  • macOS: Terminal (built-in) or iTerm2 (download)
  • Windows: Putty or PowerShell SSH (Needs to be enabled in Windows 10).

As part of the Raspberry Pi Imager setup, you specified a username and password to use. You will need to use those credentials with the following command:

ssh username@ip_address
  • Replace username with the username you specified in the Raspberry Pi Imager
  • Replace ip_address with the IP address the Raspberry Pi obtained from your DHCP server (typically, this would be your Router). It should look something like 192.168.0.11.

Once connected, you should then be prompted for a password. This is the password you previously set in the Raspberry Pi Imager. Once you have successfully logged in, you should see a prompt like the below:

pi@pi4-server:~$ _
  • pi: This is your username
  • pi4-server: This is the hostname of the Raspberry Pi you are connected to.

Install Required Software

There are two dependencies that need to be installed to complete the setup. These are:

  • i2c-tools – This package contains tools for working with the i2c bus on the Raspberry Pi
  • util-linux-extra – This package contains a bunch of tools but the one that is needed in here is hwclock. This is used to interact with a hardware clock, such as an RTC module. Previously, hwclock was installed by default in Ubuntu 20.04 but at some point, they removed it as part of the base installation.

To install the two packages, run:

sudo apt install -y i2c-tools util-linux-extra

Check the Current Date and Time

Before we begin setting up the RTC module, let’s check the current date and time of the system. This will not come from the RTC module at this point. Instead, it will be set using the systemd timesyncd service that is run when the Raspberry Pi is booting up.

Run the following to get the current system date and time:

date

The date and time should be the current date, time and time zone for where you are. For example:

Mon Jul 22 16:20:14 BST 2024

If it isn’t, you may need to check your Raspberry Pi’s network and internet connection settings.

Detect the RTC Module

To detect the RTC module, use the i2cdetect command to locate the module on the i2c bus. At the terminal, enter the following command:

sudo i2cdetect -y 1

The output should look like the below:

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

In the above, all the fields are the same, except for the field at position 60x8. This is where the RTC module has been detected. This is typical for most RTC modules that are installed on a Raspberry Pi but yours may come back in a different field.

If the output of the command is like the below, you may need to check that the RTC battery is installed correctly:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Now, although the RTC module has been detected, it is currently not accessible as Ubuntu has not enabled it. To enable it, run the following commands (NOTE: change ds3231 on the echo command to the chipset of your RTC module. Also, if you use a ds3231 module, it will use the ds1307 module driver as they share similar commands so if you see ds1307 mentioned later, that is why):

sudo su
echo dtoverlay=i2c-rtc,ds3231 >> /boot/firmware/config.txt
exit

Once the above has been done, you will need to reboot your system. Run:

sudo reboot

Once the system is rebooted, SSH back to it.

The module should now be active. To check, run the following command:

sudo i2cdetect -y 1

The output should look like the below:

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

If you look at 60x8 again, instead of showing 68 as it did before, it now should be UU. This indicates that the module is now active and can be used by Ubuntu.

If it is 68 or --, something went wrong. You can check to see what happened during bootup by running the following command:

sudo dmesg | grep rtc-

This indicates the status of the RTC module during bootup. If it loads correctly, it should look like the below:

[    3.616265] rtc-ds1307 1-0068: registered as rtc0
[    3.622081] rtc-ds1307 1-0068: setting system clock to 2024-07-22T11:41:49 UTC (1721648509)

If you don’t see that output (or whatever matches your RTC modules chipset) and you get an error instead, check the error to see what went wrong. It may also not return anything.

One potential cause is that the chipset on the RTC module is different to what was specified in the prior echo command. Double check that first. If that isn’t the case, you will need to troubleshoot using the error message provided.

Set the RTC Modules Date and Time

Up until this point, the RTC modules date and time will (potentially) be different to the current date and time if it is the first time that the module has been used. To access the contents of the RTC module, the hwclock command is used.

To start with, let’s look at the date and time that is stored on the RTC module. To do this, run the following command (-r indicates that we want to read the contents of the RTC module):

sudo hwclock -r

The output will be like the below with the date and time being different:

2000-01-01 16:17:57.927712+01:00

To update the date and time on the RTC module, the current date and time from the system needs to be written to the RTC module. To do this, run the following command (-w indicates that the RTC module will have the current system time written to it):

sudo hwclock -w
sudo hwclock -r

The output will now be the current date and time. For example:

2024-06-25 13:02:26.825937+01:00

That is all the steps needed to get an RTC module up and running on Ubuntu 24.04 running on a Raspberry Pi 4.

Optional: Disable the Timesyncd Service

If you are deploying the Raspberry Pi to a location where it will have no internet connection, or you just want to use the time and date from the RTC module, you can disable the timesyncd service.

To do this, run the following command:

sudo systemctl disable systemd-timesyncd.service

When the system is next booted or rebooted, the only source of the date and time for the system will be the RTC module.

Conclusion

I hope that this article was useful.

Thank you for reading and have a nice day!


This content originally appeared on DEV Community and was authored by dev_neil_a


Print Share Comment Cite Upload Translate Updates
APA

dev_neil_a | Sciencx (2024-08-01T12:00:00+00:00) Setup a RTC Module with Ubuntu 24.04 on a Raspberry Pi 4. Retrieved from https://www.scien.cx/2024/08/01/setup-a-rtc-module-with-ubuntu-24-04-on-a-raspberry-pi-4/

MLA
" » Setup a RTC Module with Ubuntu 24.04 on a Raspberry Pi 4." dev_neil_a | Sciencx - Thursday August 1, 2024, https://www.scien.cx/2024/08/01/setup-a-rtc-module-with-ubuntu-24-04-on-a-raspberry-pi-4/
HARVARD
dev_neil_a | Sciencx Thursday August 1, 2024 » Setup a RTC Module with Ubuntu 24.04 on a Raspberry Pi 4., viewed ,<https://www.scien.cx/2024/08/01/setup-a-rtc-module-with-ubuntu-24-04-on-a-raspberry-pi-4/>
VANCOUVER
dev_neil_a | Sciencx - » Setup a RTC Module with Ubuntu 24.04 on a Raspberry Pi 4. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/01/setup-a-rtc-module-with-ubuntu-24-04-on-a-raspberry-pi-4/
CHICAGO
" » Setup a RTC Module with Ubuntu 24.04 on a Raspberry Pi 4." dev_neil_a | Sciencx - Accessed . https://www.scien.cx/2024/08/01/setup-a-rtc-module-with-ubuntu-24-04-on-a-raspberry-pi-4/
IEEE
" » Setup a RTC Module with Ubuntu 24.04 on a Raspberry Pi 4." dev_neil_a | Sciencx [Online]. Available: https://www.scien.cx/2024/08/01/setup-a-rtc-module-with-ubuntu-24-04-on-a-raspberry-pi-4/. [Accessed: ]
rf:citation
» Setup a RTC Module with Ubuntu 24.04 on a Raspberry Pi 4 | dev_neil_a | Sciencx | https://www.scien.cx/2024/08/01/setup-a-rtc-module-with-ubuntu-24-04-on-a-raspberry-pi-4/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.