Pi 4 clock that keeps time offline
Build it step by step
0 of 7 doneShut down
Power the Pi off, then fit the module on the header pins 1 to 6, as its markings show.
Add the overlay
In /boot/firmware/config.txt add the line dtoverlay=i2c-rtc,ds3231 and reboot.
Check the chip
i2cdetect -y 1 shows UU at address 68 once the system uses it.
Remove fake-hwclock
sudo apt purge fake-hwclock, then reboot.
Set the module
With the network connected, run sudo hwclock -w.
Run the script
The two clocks agree.
Test offline
Shut down, unplug the network, wait a minute, start and run the script again.
How do you build a pi 4 clock that keeps time offline?
A Raspberry Pi 4 with a small DS3231 clock module on its header keeps the right time without a network, and a script compares the Pi clock and the module and prints the difference after a power cut. You need Raspberry Pi 4 Model B (4 GB), DS3231 RTC module (mini, for Raspberry Pi), SanDisk microSD card 32 GB (A1, Class 10) and Raspberry Pi 27 W USB-C power supply. It takes 7 steps and about 130 minutes.
- Level
- Intermediate
- Time
- About 130 min
- Parts
- 4
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| RTC module 3V3 | Pi header pin 1 | Power at 3.3 V |
| RTC module SDA | Pi header pin 3 (GPIO2) | I2C data |
| RTC module SCL | Pi header pin 5 (GPIO3) | I2C clock |
| RTC module GND | Pi header pin 6 | Common ground |
Code
A Raspberry Pi has no battery-backed clock of its own. Without a network it either starts with the time it last saved or with a wrong one. The small DS3231 module has a precise clock chip and a backup cell, so it keeps time while the Pi is off. The Pi talks to it over I2C, and a line in the configuration file, called a device-tree overlay, tells Linux that the chip is the hardware clock, so the system reads it at start-up before any network is there. The fake-hwclock package, which saves the time to a file instead, is removed so that it does not compete. The script reads the module with the hwclock command and compares it with the system clock, then prints the difference in seconds. To test it, let the Pi sync from the network, write that time to the module, switch the Pi off, wait, start it with no network and run the script again; the clocks should still agree to within a second or two. The module takes the cell it is made for. Fit it the right way round and never try to charge a coin cell. Shut the Pi down before placing or removing the module on the header.
#!/usr/bin/env python3
# Compare the Pi's own clock with the DS3231 module and report the difference.
import subprocess
from datetime import datetime
def read_module():
out = subprocess.run(["sudo", "hwclock", "-r"], capture_output=True, text=True)
if out.returncode != 0:
raise SystemExit("Could not read the module: " + out.stderr.strip())
return datetime.fromisoformat(out.stdout.strip())
module_time = read_module()
system_time = datetime.now().astimezone()
difference = (system_time - module_time).total_seconds()
print("Module clock :", module_time.strftime("%Y-%m-%d %H:%M:%S"))
print("System clock :", system_time.strftime("%Y-%m-%d %H:%M:%S"))
print("Difference : %+.1f seconds" % difference)
if abs(difference) < 2:
print("The two clocks agree.")
else:
print("The clocks differ. After a network sync run: sudo hwclock -w")Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| After set-up | Difference under two seconds |
| Read the module | sudo hwclock -r prints the time |
| Reboot with no network | Date is still right |
| Compare after a power cut | Difference still small |
| Wrong time written | Fix the network time, then sudo hwclock -w |
If it does not work
No device at address 68
Check the module seating and that I2C is switched on.
Shows 68 not UU
The overlay line is missing or has a typo, check config.txt.
Cannot read the module
Run with sudo and check the overlay.
Time resets after power off
Fit or replace the module's cell.
Script error on the date
Use Python 3.11 or newer, as on Bookworm.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pi 4 clock that keeps time offline?
Raspberry Pi 4 Model B (4 GB), DS3231 RTC module (mini, for Raspberry Pi), SanDisk microSD card 32 GB (A1, Class 10) and Raspberry Pi 27 W USB-C power supply.
How long does the pi 4 clock that keeps time offline take to build?
About 130 minutes for a intermediate maker, in 7 steps.
How do I know the pi 4 clock that keeps time offline works?
After set-up: Difference under two seconds. Read the module: sudo hwclock -r prints the time. Reboot with no network: Date is still right.
No device at address 68: what should I check?
Check the module seating and that I2C is switched on.
Shows 68 not UU: what should I check?
The overlay line is missing or has a typo, check config.txt.
Cannot read the module: what should I check?
Run with sudo and check the overlay.
Time resets after power off: what should I check?
Fit or replace the module's cell.
