Ubuntu Server with RTC DS3231 HELP
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!!
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!!
Re: Ubuntu Server with RTC DS3231 HELP
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
mext
this is done as super user either
or
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
sets the systm time to the hardware time...
next to read the clock code to save or write the rtc unit time and date..
next code
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..
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..
...!!!
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
Code: Select all
set the correct time and date
Code: Select all
time
Code: Select all
date
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
next to read the clock code
Code: Select all
hwclock -r
next code
Code: Select all
hwclock -w
next rebboot the system...
one rebooted from the command line check the time as super user..
Code: Select all
.hwclock -r
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..
Re: Ubuntu Server with RTC DS3231 HELP
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!
Re: Ubuntu Server with RTC DS3231 HELP
you have to install the needed raspberry/rpi parts. ubuntu does not install anything other than the kernel needed to boot.DarkNewt wrote: ↑Tue Feb 22, 2022 2:55 pmHi 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!
Code: Select all
apt search rpi.
Code: Select all
apt search raspberry
Re: Ubuntu Server with RTC DS3231 HELP
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.
Re: Ubuntu Server with RTC DS3231 HELP
i'm thinking you just need to enable the right overlay in /boot/firmware/config.txt.DarkNewt wrote: ↑Tue Feb 22, 2022 5:02 pmHi, 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.
how do you go about it in raspberry?
it should be pretty much the same in ubuntu.
Re: Ubuntu Server with RTC DS3231 HELP
In Raspberry Pi OS I just addkerry_s wrote: ↑Tue Feb 22, 2022 5:10 pmi'm thinking you just need to enable the right overlay in /boot/firmware/config.txt.DarkNewt wrote: ↑Tue Feb 22, 2022 5:02 pmHi, 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.
how do you go about it in raspberry?
it should be pretty much the same in ubuntu.
Code: Select all
dtoverlay=i2c-rtc,ds3231
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
Re: Ubuntu Server with RTC DS3231 HELP
then just add that to /boot/firmware/config.txt :
ubuntu uses uboot to launch/load the rpi kernel & overlays, other than that should be no different.
Code: Select all
sudo nano /boot/firmware/config.txt
Re: Ubuntu Server with RTC DS3231 HELP
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.
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.
Re: Ubuntu Server with RTC DS3231 HELP
what are the commands?
have you checked if there is a disabled service, that just needs to be enabled?
have you checked if there is a disabled service, that just needs to be enabled?
Re: Ubuntu Server with RTC DS3231 HELP
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..!!!
Re: Ubuntu Server with RTC DS3231 HELP
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
https://www.youtube.com/watch?v=MthLLRNAGLs
Re: Ubuntu Server with RTC DS3231 HELP
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
Re: Ubuntu Server with RTC DS3231 HELP
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!
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!
Re: Ubuntu Server with RTC DS3231 HELP
ok try this create a file called rc.local in the /etc directory
enter this code into the file
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
enter this code into the file
Code: Select all
hwclock -s
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
UPDATE NOW SOLVED
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!
I am now going to write up the solution and I will post it here and elsewhere!
Re: Ubuntu Server with RTC DS3231 HELP
quick question do i get any recognition..???
SOLUTION
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 [email protected].
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 [email protected].
Last edited by DarkNewt on Wed Feb 23, 2022 8:48 pm, edited 1 time in total.
Re: Ubuntu Server with RTC DS3231 HELP
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.
Re: Ubuntu Server with RTC DS3231 HELP
Last edited by blade777 on Fri Feb 25, 2022 4:15 pm, edited 1 time in total.
Re: Ubuntu Server with RTC DS3231 HELP
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 [email protected] 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!!
Re: Ubuntu Server with RTC DS3231 HELP
see abouve