Keep Raspberry Pi Time Correct Offline: DS3231 Setup and Boot-Time Checks
A Raspberry Pi can show the correct time on your desk and start with the wrong date at an offline installation. Network synchronisation may have hidden a missing clock configuration, a flat backup cell or a boot process that never reads the external RTC.
Adding a DS3231 is only one part of the solution. Linux must recognise the hardware, the system and RTC must agree about their time convention, and the application must know whether its timestamps are trustworthy. This guide concentrates on that integration rather than simply checking whether a clock chip answers on I2C.
Decide which clock is responsible for what
The hardware RTC retains calendar time while the main system is off. The Linux system clock supplies time to applications while running. A network time service can correct the system clock when a trusted connection is available. These are related functions, but a successful read from one does not prove the others are configured correctly.
Write a short policy for the project. For example: use the RTC at offline startup, allow the selected network service to improve time when connectivity returns, and update the RTC only from a trusted system time. Do not install an unconditional startup script that overwrites the RTC with an unverified date.
PMD Way's Raspberry Pi real-time clock collection includes a compact DS3231 module with backup battery. Confirm the exact module and mounting orientation before fitting it to the GPIO header.
Check the hardware before changing Linux
The DS3231 includes a temperature-compensated oscillator and battery-backed timekeeping, as described in the Analog Devices DS3231 data sheet. Its oscillator-stop flag indicates that timekeeping may have been interrupted. A calendar value can look reasonable after such an event, so communication alone is not a validity check.
Power down and disconnect the Pi before installing the module. Match its connector and schematic to the exact Pi header; do not infer orientation from the battery holder or a photograph taken from the opposite side. Check supply connections and the I2C pull-up voltage. A module described as usable with 5 V systems must not pull Raspberry Pi GPIO signals up to 5 V.
Use the battery chemistry and polarity specified for the actual module. Do not assume that every DS3231 board has the same charging arrangement. Secure the battery against accidental removal, particularly in classrooms, and avoid shorting it while handling the module.
Also check whether an external RTC is needed. The Raspberry Pi hardware documentation describes the Pi 5's onboard RTC and battery connection. If both onboard and external clocks are present, identifying the device used by Linux becomes especially important.
Register the correct device with the kernel
For a normal supported I2C arrangement, Raspberry Pi's official overlay reference includes the DS3231 option for the I2C RTC overlay:
dtoverlay=i2c-rtc,ds3231
This is a configuration-file entry, not a shell command. Check the documentation supplied with your installed operating system for the appropriate configuration file and bus selection. Current Raspberry Pi OS commonly uses /boot/firmware/config.txt; older instructions can refer to a different path. Back up the file and avoid duplicating an existing RTC declaration.
After restarting, inspect kernel messages for the RTC driver and examine the registered RTC devices. The Linux RTC documentation describes the numbered device interface, such as /dev/rtc0 and /dev/rtc1. The numbering is a device identity to verify, not a promise that the external DS3231 will always be first.
Do not run a separate raw-register program against the clock while the kernel driver is managing it. Use the operating system's RTC interface for normal administration, and keep any low-level diagnostic access deliberate and controlled.
Keep UTC storage separate from local display time
Store the RTC in UTC and configure the operating system's time zone for local presentation. The systemd timedatectl documentation recommends UTC for the RTC because maintaining local civil time there creates problems with time-zone changes and daylight saving.
Start with timedatectl status to inspect the current configuration. Review the system time, time zone, synchronisation status and RTC convention together. The presence of a running network time service is not the same as proof that it has successfully synchronised.
A one-hour or several-hour discrepancy often deserves a time-zone check before an oscillator diagnosis. For example, a project deployed in another Australian state may need a different display zone without any change to the UTC value retained by the clock.
Read first, then set deliberately
The hwclock manual distinguishes reading the RTC, copying system time into it and copying RTC time into the system. Verify which operation you intend before running a command.
If you have established that the DS3231 is /dev/rtc1, a read example is:
sudo hwclock --show --rtc=/dev/rtc1 --utc
Replace that device path with the one actually identified on your system. The --utc option describes how the RTC is stored; hwclock --show normally presents its result in local time. Compare the displayed offset as well as the digits.
Once the system time has been checked against a trustworthy reference, the corresponding deliberate write is:
sudo hwclock --systohc --rtc=/dev/rtc1 --utc
Coordinate this with the installed time service rather than creating competing scripts. Record when the clock was set and why. A successful command should be followed by a readback, but that still does not prove that the next offline boot will use it.
Test the full offline startup path
- Confirm a trustworthy system time and initialise the intended RTC.
- Read back the RTC and record the time and device identity.
- Shut down cleanly, remove main power and leave the system off for a measured interval.
- Prevent network time synchronisation during the test using a controlled offline connection arrangement.
- Restore power and inspect the earliest application and system timestamps.
- Check whether Linux selected the intended RTC and whether time advances by the expected outage interval.
- Reconnect the network and observe any correction without losing the original observations.
Repeat this test after an operating-system upgrade or a change to startup services. If the hardware read is correct but early application timestamps are wrong, investigate boot ordering and clock selection rather than replacing the battery immediately.
Make time confidence visible to the application
An offline instrument should distinguish trusted time, retained but ageing time, and time requiring attention. Record a boot identifier and sequence number alongside timestamps when records must remain distinguishable during corrections. Flag uncertain dates instead of silently presenting them as precise measurements.
Keep the configuration, RTC identity and offline test results with the project documentation. A dependable clock installation is one whose startup behaviour has been observed without the network rescuing it.