DarkNewt
Posts: 10
Joined: Mon Feb 21, 2022 8:56 pm

Ubuntu Server with RTC DS3231 HELP

Mon Feb 21, 2022 9:21 pm

Hi,
I have installed the RTCDS3231 successfully in Raspbian but came to a complete halt getting it working with Ubuntu Server 64bit.
I have installed the I2c tools and I can see it, however none of the guides seem to be complete or fully applicable in order to get it working with Ubuntu Server.

If anyone has done this I would apreciate any help, if you know how to do it I am more than happy to help write/produce a howto for this.

Essentially, i want to use the RTC for the time and then install an NTP server - just need to get this RTC recognised...

Many thanks for any input!!

blade777
Posts: 75
Joined: Thu Oct 29, 2020 2:49 am

Re: Ubuntu Server with RTC DS3231 HELP

Tue Feb 22, 2022 1:54 pm

you are in a ubuntu forum however try this,,below...its still command line code anyway....!!!
...!!!

edit the file "rc.local if there is one otherwise create it.... its in the /etc directory code this into the fi;le and save it

Code: Select all

hwclock -s
mext

Code: Select all

 set the correct time and date 
this is done as super user either

Code: Select all

time
or

Code: Select all

date 
and enter the correct date and time it should give you an example of the format required...

next just hit enter and it should be held
to check hit date and it should show the correct set that you set it to

next

Code: Select all

hewclock -s
sets the systm time to the hardware time...
next to read the clock code

Code: Select all

hwclock -r
to save or write the rtc unit time and date..

next code

Code: Select all

 hwclock -w
and it should write all the data to the clock saving it

next rebboot the system...

one rebooted from the command line check the time as super user..

Code: Select all

.hwclock -r
it should be done .. as long as you have a active battery in the rtc module it will set both system and hardware time..via software ..at boot up generally..

to stop the ntp type kill ntp or open a console and type "top" and find the ntps pid number and kill or stop the ntp .yhere are other methods of killing the ntp service..

DarkNewt
Posts: 10
Joined: Mon Feb 21, 2022 8:56 pm

Re: Ubuntu Server with RTC DS3231 HELP

Tue Feb 22, 2022 2:55 pm

Hi Thankyou for the responce, however my issue is hwclock doesn't even see it, the rtc isn't being registered i have tried --verbose and it isn't able to see it at all, I suspect the right modules aren't being loaded as it can't find them. modprobe etc.. can't find them either, and I can't even see rtc0 so that isn't getting created either, I think my problems are as above the modules/drivers just arent' being loaded. If all else fails I am going to try an earlier version to see if that brings any success. If I get any further I will do my best to write the steps up, thanks again for the kind support!

User avatar
kerry_s
Posts: 4587
Joined: Thu Jan 30, 2020 7:14 pm

Re: Ubuntu Server with RTC DS3231 HELP

Tue Feb 22, 2022 3:28 pm

DarkNewt wrote:
Tue Feb 22, 2022 2:55 pm
Hi Thankyou for the responce, however my issue is hwclock doesn't even see it, the rtc isn't being registered i have tried --verbose and it isn't able to see it at all, I suspect the right modules aren't being loaded as it can't find them. modprobe etc.. can't find them either, and I can't even see rtc0 so that isn't getting created either, I think my problems are as above the modules/drivers just arent' being loaded. If all else fails I am going to try an earlier version to see if that brings any success. If I get any further I will do my best to write the steps up, thanks again for the kind support!
you have to install the needed raspberry/rpi parts. ubuntu does not install anything other than the kernel needed to boot.

Code: Select all

apt search rpi.

Code: Select all

apt search raspberry

DarkNewt
Posts: 10
Joined: Mon Feb 21, 2022 8:56 pm

Re: Ubuntu Server with RTC DS3231 HELP

Tue Feb 22, 2022 5:02 pm

Hi, I will give it a go, however afaik this has been done with previous versions of ubuntu on the RP and it didn't need them, and there is no mention of them in any other guide I have looked at. I will report back with any success, I am hoping once resolved that I can provide a guide to this.

User avatar
kerry_s
Posts: 4587
Joined: Thu Jan 30, 2020 7:14 pm

Re: Ubuntu Server with RTC DS3231 HELP

Tue Feb 22, 2022 5:10 pm

DarkNewt wrote:
Tue Feb 22, 2022 5:02 pm
Hi, I will give it a go, however afaik this has been done with previous versions of ubuntu on the RP and it didn't need them, and there is no mention of them in any other guide I have looked at. I will report back with any success, I am hoping once resolved that I can provide a guide to this.
i'm thinking you just need to enable the right overlay in /boot/firmware/config.txt.
how do you go about it in raspberry?
it should be pretty much the same in ubuntu.

User avatar
rpdom
Posts: 20889
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Ubuntu Server with RTC DS3231 HELP

Tue Feb 22, 2022 5:27 pm

kerry_s wrote:
Tue Feb 22, 2022 5:10 pm
DarkNewt wrote:
Tue Feb 22, 2022 5:02 pm
Hi, I will give it a go, however afaik this has been done with previous versions of ubuntu on the RP and it didn't need them, and there is no mention of them in any other guide I have looked at. I will report back with any success, I am hoping once resolved that I can provide a guide to this.
i'm thinking you just need to enable the right overlay in /boot/firmware/config.txt.
how do you go about it in raspberry?
it should be pretty much the same in ubuntu.
In Raspberry Pi OS I just add

Code: Select all

dtoverlay=i2c-rtc,ds3231
to /boot/config.txt

I didn't need to do anything else for it to work.

It looks like that causes the rtc_ds1307 kernel module to be loaded. I don't know about anything else. There should be nothing Pi specific here. It's a standard i2c rtc chip on an i2c bus which is recognised by the kernel so the standard drivers and stuff should "just work".
Unreadable squiggle

User avatar
kerry_s
Posts: 4587
Joined: Thu Jan 30, 2020 7:14 pm

Re: Ubuntu Server with RTC DS3231 HELP

Tue Feb 22, 2022 5:40 pm

then just add that to /boot/firmware/config.txt :

Code: Select all

sudo nano /boot/firmware/config.txt 
ubuntu uses uboot to launch/load the rpi kernel & overlays, other than that should be no different.

DarkNewt
Posts: 10
Joined: Mon Feb 21, 2022 8:56 pm

Re: Ubuntu Server with RTC DS3231 HELP

Wed Feb 23, 2022 2:02 am

Hi,
that doesn't work with Ubuntu, I have managed to get it working, however it is not persitent across reboots, playing with systemd at the moment to create a startup for the service, there are additonal commands that are needed over and above what raspbian requires. When I have resolved I will post here as well.

User avatar
kerry_s
Posts: 4587
Joined: Thu Jan 30, 2020 7:14 pm

Re: Ubuntu Server with RTC DS3231 HELP

Wed Feb 23, 2022 3:27 am

what are the commands?
have you checked if there is a disabled service, that just needs to be enabled?

blade777
Posts: 75
Joined: Thu Oct 29, 2020 2:49 am

Re: Ubuntu Server with RTC DS3231 HELP

Wed Feb 23, 2022 4:36 am

have you got the high rise module if so pin 1 should be on the end set of pins of the 40 pin header .. check the pin-outs i think you don't have it connected properly my code should work with it make shire that you are always root user when coding the unit...!! check the physical connections.. mine works check the actual chip set on the module......use ic2 detect to see if the module is there listed in the code or reported to be there..!!!

blade777
Posts: 75
Joined: Thu Oct 29, 2020 2:49 am

Re: Ubuntu Server with RTC DS3231 HELP

Wed Feb 23, 2022 5:01 am

have a look here dont worry about it being for the rpi-3 and dont worry about it being in debian if it is..just look at the pinnouts of the module on the board the 40 pinn header is the same ..for both rpi-3 and 4..


https://www.youtube.com/watch?v=MthLLRNAGLs

User avatar
rpdom
Posts: 20889
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Ubuntu Server with RTC DS3231 HELP

Wed Feb 23, 2022 8:21 am

If the commands were something like sudo modprobe rtc_ds1307, then you should just be able to add rtc_ds1307 to /etc/modules. Possibly in order to load the clock module earlier in the boot sequence you might have to have something in cmdline.txt instead.
Unreadable squiggle

DarkNewt
Posts: 10
Joined: Mon Feb 21, 2022 8:56 pm

Re: Ubuntu Server with RTC DS3231 HELP

Wed Feb 23, 2022 4:44 pm

Hi,
just to re-iterate this is on Ubuntu not Raspbian.

I have I believed solved all the issues except one!

to get the interface up and running after the rest of my configuration so hwclock can access it I need issue the following:

sudo bash

then I issue the following:

echo ds3231 0x68 >/sys/bus/i2c/devices/i2c-1/new_device

this is great but I need it to start at boot, so I have created a systemd script to do so. However the systemd script won't work because it doesn't have enough privelages and the script errors with a permission denied.

So how do I get it so that the command above runs at startup?
once that's solved I can put up a full guide to getting an rtc running on RP with Ubuntu!

blade777
Posts: 75
Joined: Thu Oct 29, 2020 2:49 am

Re: Ubuntu Server with RTC DS3231 HELP

Wed Feb 23, 2022 5:24 pm

ok try this create a file called rc.local in the /etc directory
enter this code into the file

Code: Select all

hwclock -s
and save it using either nano or your preferred editor..at boot it will look at the file and set the system time automatically on every reboot ..

it reads the rtc time into the system time//..or the software running at boot

about the ntp use this code....

you can always go back and use "start " if you wont to restart it

Code: Select all

sudo /etc/init.d/ntp stop

DarkNewt
Posts: 10
Joined: Mon Feb 21, 2022 8:56 pm

UPDATE NOW SOLVED

Wed Feb 23, 2022 6:11 pm

Hi All, after much research I have solved all of the issues, I can't believe how many there were for something that should have been so straightforward. I have learned loads in researching and solving so thats a great benefit. Thankyou to those who replied to me!

I am now going to write up the solution and I will post it here and elsewhere!

blade777
Posts: 75
Joined: Thu Oct 29, 2020 2:49 am

Re: Ubuntu Server with RTC DS3231 HELP

Wed Feb 23, 2022 7:51 pm

quick question do i get any recognition..???

DarkNewt
Posts: 10
Joined: Mon Feb 21, 2022 8:56 pm

SOLUTION

Wed Feb 23, 2022 8:31 pm

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

Thankyou to all the people who tried to help in solving this and to the literally hundreds of blogs/posts that I had to read to get this done. As the saying goes, ON THE BACKS OF GIANTS!

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 DS3321) 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.
Last edited by DarkNewt on Wed Feb 23, 2022 8:48 pm, edited 1 time in total.

DarkNewt
Posts: 10
Joined: Mon Feb 21, 2022 8:56 pm

Re: Ubuntu Server with RTC DS3231 HELP

Wed Feb 23, 2022 8:46 pm

blade777 wrote:
Wed Feb 23, 2022 7:51 pm
quick question do i get any recognition..???
Hi Blade, I wasn't trying to leave anyone out, I had said I was already able to get this working with Rasbian and had already tried everything that was being suggested. In the end I had to do tons of research, and read so many how to's man pages and half guides and try countless different configurations it would be hard to mention everyone/everything I used so I figured the best way to give back was to share.

I think the solution I have posted below is pretty much the most straightforward/complete way to get this working on an ubuntu/RP/RTC combo. But I do take your point and I am going to edit the solution posted below to thank everyone who I read and took the time to comment.

blade777
Posts: 75
Joined: Thu Oct 29, 2020 2:49 am

Re: Ubuntu Server with RTC DS3231 HELP

Wed Feb 23, 2022 8:48 pm

Last edited by blade777 on Fri Feb 25, 2022 4:15 pm, edited 1 time in total.

DarkNewt
Posts: 10
Joined: Mon Feb 21, 2022 8:56 pm

Re: Ubuntu Server with RTC DS3231 HELP

Wed Feb 23, 2022 10:41 pm

blade777 wrote:
Wed Feb 23, 2022 8:48 pm
is that you "ken"...how goes the maximites...mostly its the chipset that mtters the uuntu code i put foward is pretty much general and works well with most r.t.c.units if wired to the rpi correctly under ubuntu..4563 1307 ..etc..etc..
Hiya, as per my solution it works differently with an RTC hat and Ubuntu, and requires a lot more configuration. If you want to connect to discuss you can message me on rkd@systems-engineer.info or look me up on facebook/linkedin, I would be interested in ways of simplifying what i have done.

your message was a little cryptic, I don't think I know you but as above it's always possible!!

blade777
Posts: 75
Joined: Thu Oct 29, 2020 2:49 am

Re: Ubuntu Server with RTC DS3231 HELP

Fri Feb 25, 2022 4:15 pm

see abouve

Return to “Ubuntu”