HOW TO INSTALL AND CONFIGURE AN RTC TO WORK WITH A RASBERRY PI AND UBUNTU.

Forum for posting topics and questions about anything.
Post Reply
DarkNewt
Posts: 2
Joined: Wed Feb 23, 2022 8:32 pm

HOW TO INSTALL AND CONFIGURE AN RTC TO WORK WITH A RASBERRY PI AND UBUNTU.

Post by DarkNewt » Wed Feb 23, 2022 8:37 pm

PURPOSE:

The purpose of this guide is to configure an RTC clock to work with Ubuntu on a Raspberry Pi. Without an RTC when your PI has been off a long time the system time will not update until it connects to the internet. The system can then encounter errors until the ntp service synchronises with an NTP server on the internet.
The synchronization process can take a long time if the difference between your system time and the actual time is significant, as NTP doesn’t sync instantly it does it incrementally. Adding an RTC allows the system time to sync from it as well and stay very close to the actual time avoiding any errors.
This is useful if your project needs accurate time between reboots etc.. I started on this as I am building an NTP server as part of my cluster build.

STAGE 1 – PRE-REQUISITES:

A – A Raspberry Pi 4 (mine was an 8gb) This should work on other variants.
C – An RTC clock DS1307 or DS3321 (I used a high accuracy DS3321 from Hobby Components) with a working battery.
B – A working wired or wireless network interface
See instructions and where to put it for a working yaml file to get wlan0 connected
C – An up to date Ubuntu Server Installation
sudo apt-get update
sudo apt-get upgrade
D – Your keyboard correctly set, you will be editing files
sudo dpkg-reconfigure keyboard-configuration
E – The I2C interface should be enabled
Boot your Pi with Raspbian run Raspbian config and enable it from there.
F – SSH server installed (it was by default for me but can be installed with the following command)
Sudo apt-get install openssh-server

STAGE 2 – INSTALL I2C TOOLS:

Execute the following commands at the terminal:
A – installs the tools:
sudo-apt get install i2c-tools
B – Check they are running
sudo i2c-detect -y 1
NOTE: By default most clocks are registered at 0x68 if yours is different note it down and replace 0x68 with yours wherever it is mentioned in the guide.

STAGE 3 – CHECK THE DS1307 DRIVERS ARE INSTALLED

Execute the following commands at the terminal:
A - sudo modprobe rtc-ds1307
This should execute without any errors and checks to see if the module loads. Later in the guide we will get them to load automatically on boot.

STAGE 4 – CHECK THAT YOU CAN CREATE THE NEW RTC INTERFACE:

Note: in Ubuntu this is added as part of new_device, this was a major headache for me as below the command needed to be run under root privileges in bash because the > redirection needs to be interpreted correctly. After much research I came across a method others had used for similar tasks and it enabled me to get this working at boot time later in the guide.

Execute the following commands at the terminal:

A – switch to bash running as root:
sudo bash
B – This command allows hwclock to access the RTC
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
C – Exit Bash
exit
D – Test that hwclock can now access the RTC
Sudo hwclock -r
E – Check the system time (if you are connected to the internet this should be correct)
Date
F – Now write the system time to the RTC
Hwclock -w
Note: later in the guide hwclock -s will be used to set the system time from the RTC

STAGE 5 – GET THE RTC-DS1307 MODULE TO LOAD AT BOOT TIME

Execute the following commands at the terminal:

A – Navigate to the following directory:
cd /etc/modules-load.d
C – Edit the modules.conf file
sudo nano modules.conf
D – Add the following line to your modules.conf
#This loads the rtc-1307 module
rtc-1307

STAGE 6 – CREATE A SYSTEMD SCRIPT TO CREATE THE INTERFACE AT BOOT UP AND SYNC THE
SYSTEM TIME WITH THE RTC


Execute the following commands at the terminal:

A – Navigate to the following:
cd /etc/
B – Create the file rtc which will be used as the startup script
sudo nano rtc
C – In Nano add the following lines:

#!/bin/bash
#this is the script to be executed at boot to create the rtc interface
echo 'ds1307 0x68' | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s

D - Quit Nano
ctrl x to save and quit nano
E - Set the correct file permissions and make the rc script executable.
sudo chmod 755 rtc
sudo chmod +x rtc (this shouldn’t be necessary but I did it anyway)

STAGE 7 – CREATE THE SYSTEMD FILE:

Execute the following commands at the terminal:

A - Navigate to following directory.
cd /etc/systemd/system/

B – Create the rtc.service file
sudo nano rtc.service
C - Add the following lines to rtc.service file:

#this calls the rtc script at boot to create the rtc interface new_device
[Unit]
Description=RTC Clock
Before=cloud-init-local.service
Requires=systemd-modules-load.service
After=systemd-modules-load.service
[Type]
Type=oneshot
[Service]
ExecStart=/etc/rtc
[Install]
WantedBy=multi-user.target

D – Close and exit Nano
Ctrl + x to exit and save

NOTE: I looked in the /etc/system/system/ folder for a service that I wanted this to run
before, my thinking was that I wanted this all up and running before the network started
so that any possible issues with time and services that rely on the network wouldn’t be
a problem. I chose the cloud-init-local.service, it is well worth reading up on systemd.


E - Check to see if the rtc.service loads without error:

sudo systemctl start rtc.service
Note: this should start the service if there are errors check the formatting and
capitalisation of the file to match above.

F – Enable the rtc.service

sudo systemctl enable rtc.service

Note: This should output something like:
Created symlink /etc/systemd/system/multi-user.target.wants/rtc.service →
/etc/systemd/system/rtc.service.

Note: If that is successful, you should now have a working RTC and a service that starts it at reboot and syncs the system time to the rtc clock.

STAGE 8 – FINAL TESTS

Execute the following commands at the terminal:
A – reboot the server
reboot now

B – When the server has rebooted logon and issue the following command:
Sudo hwclock -r

Note: This should return the time as hwclock now has access to the RTC module and
the system time is set from the clock on reboot every time.
configured on reboot.



FINISH

If you have any improvements/comments you can feedback to rkd@systems-engineer.info.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HOW TO INSTALL AND CONFIGURE AN RTC TO WORK WITH A RASBERRY PI AND UBUNTU.

Post by andrew » Thu Feb 24, 2022 5:16 pm

Nice guide. I can add a link to it from the first post of the appropriate product threads if you like?

One comment, I assume you mean the DS3231 and not the DS3321?
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

DarkNewt
Posts: 2
Joined: Wed Feb 23, 2022 8:32 pm

Re: HOW TO INSTALL AND CONFIGURE AN RTC TO WORK WITH A RASBERRY PI AND UBUNTU.

Post by DarkNewt » Fri Feb 25, 2022 3:57 pm

Hi Andrew,

Yes, it was a typo it should be DS3231

I have made some adjustments to the guide to improve it, including how to get crontab to write the system time to the RTC regularly and keep a historical log of the adjustments etc.. Also how to then install NTP and set other linux and windows machines to use your server.

I can send it to you to put up as a guide so you can format it how you think best and you can put in the relevant links to the actual RTC I used, I bought it from you guys. Just let me know how to send it.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HOW TO INSTALL AND CONFIGURE AN RTC TO WORK WITH A RASBERRY PI AND UBUNTU.

Post by andrew » Sun Feb 27, 2022 8:49 am

If you like I can get it added as a guide on our blog site. If you want to do that then just email the guide to me at: support[at]hobbycomponents.com

If not I can just simply link the relevant product threads to the guide you've posted in this thread.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply

Return to “General Discussion”