Repository navigation
RTC (Real Time Clock) for Raspberry Pi (except Pi5)
Scott Sheppard, KJ4ZZB — Version 1.0 — November 2026
A Raspberry Pi has no clock that keeps running while it's switched off. Each time it boots, it has to get the time from somewhere: the internet, or a GPS receiver. A station at a remote site often has no internet, so GPS is the main time source. This guide adds a small battery-backed real-time clock (RTC) as a backup, so the Pi still knows the correct time if GPS is lost:
- the GPS has no satellite fix yet when the Pi boots
- the antenna or receiver fails, or is unplugged
- the station is running somewhere GPS can't see the sky
Without a backup, a Pi that boots with no time source starts with a date months or years out of date. Logs, APRS timestamps and programs such as WeeWX then go wrong or stop altogether, waiting for a sensible time.
Which Raspberry Pi is this for? The Raspberry Pi 3 family (3B, 3A+ and 3B+), the Raspberry Pi 4 and the Zero 2 W. The Raspberry Pi 5 already has a built-in RTC (it only needs a backup battery on its RTC connector), so it doesn't need this module.
It works on any Raspberry Pi OS install, including DigiPi. Notes specific to DigiPi's read-only filesystem are marked DigiPi:.
| Source | Accuracy | When it's used |
|---|---|---|
GPS (through gpsd and chrony) |
Within milliseconds | Main source, whenever the receiver has a fix |
| Internet time servers (NTP) | Within milliseconds | Whenever the Pi has internet |
| RTC (this guide) | Within a few seconds a month | At every boot, and whenever GPS and internet are both unavailable |
At boot, the Pi sets its clock from the RTC straight away, so everything starts with the right date. When GPS (or internet) time becomes available, chrony takes over and fine-tunes the clock, and it keeps the RTC updated automatically.
| Part | Notes |
|---|---|
| RTC module for Raspberry Pi, 5-pin | Plugs directly onto header pins 1–9. Usually sold as "DS1307" but most use the DS3231 chip. See Step 1. The one used in this guide is available from Amazon. |
| CR2032 coin cell | Keeps the clock running while the Pi is off. Check the module's listing: if it says LIR2032 (rechargeable), use that type instead. |
| GPS receiver | Assumed already connected and working with gpsd (see Before you start) |
You'll need:
- a Raspberry Pi 3B, 3A+, 3B+, 4 or Zero 2 W running Raspberry Pi OS or DigiPi
- a GPS receiver attached to the Pi, with
gpsdrunning. Check withcgps -s: it should show a 3D fix and the time. DigiPi supports USB GPS receivers. - a way to type commands on the Pi: SSH from another computer, a keyboard and monitor, or DigiPi's built-in web terminal (under shell). For setting up SSH from a Mac or Windows PC, see Before you start in the INA226 Battery Monitor guide.
DigiPi: run sudo remount before any step that installs or edits files. A reboot returns the filesystem to read-only, so run it again after each reboot.
The RTC module used in this guide is the I2C RTC DS1307 High Precision RTC Module Real Time Clock Module for Raspberry Pi 3 from Amazon: https://www.amazon.com/dp/B07VVQKR37. Similar 5-pin modules work the same way, but check the chip marking as described below. Use caution if buying an RTC module other than the one listed here: some have the battery soldered in place, so it can't be replaced when it runs down. Choose a module with a coin-cell holder.
Which chip? Look at the largest chip on the board and read its marking:
| Marking | Works at 3.3 V? | Setting used in Step 3 |
|---|---|---|
| DS3231 (most boards) | ✔ Yes | ds3231 |
| DS1307 | ✘ Needs 5 V | Not suitable. Replace it with a DS3231 module. |
A genuine DS1307 needs 5 V, and powering it from the Pi's 5 V pins would put 5 V on the Pi's I2C pins, which can damage the Pi. Never connect this module to pin 2 or 4.
Fit the coin cell with the + side facing up (the side with the writing).
Shut down the Pi and remove the power first.

The module plugs straight onto the first five pins of the inner row of the header (the odd-numbered pins), at the corner nearest the SD card. The board lies over the Pi itself, with the coin cell facing up.
| Module pin | Pi pin | Function |
|---|---|---|
| + | 1 | 3.3 V |
| D | 3 | SDA (GPIO 2), I2C bus 1 data |
| C | 5 | SCL (GPIO 3), I2C bus 1 clock |
| NC | 7 | Not connected (spacer) |
| − | 9 | Ground |
Check before powering on: the module's + pin must be on pin 1 (the corner pin with the square solder pad on the underside), not shifted one pin along.
Fitting other boards too? The module uses pins 1–9 and the Pi's standard I2C pins. Other I2C devices can use a second I2C bus on other pins. The INA226 Battery Monitor guide, for example, puts its sensor on pins 16, 17, 18 and 20, so the two work side by side.
Power up the Pi and log in.
1. Turn on I2C:
sudo raspi-configChoose Interface Options → I2C → Yes, then Finish. (DigiPi: I2C is normally already on.)
2. Back up config.txt. A mistake in this file can stop the Pi from booting, and the backup lets you put it back:
sudo cp /boot/firmware/config.txt /boot/firmware/config.txt.bak3. Add the RTC line:
sudo nano /boot/firmware/config.txtAdd this line at the end of the file, then save with Ctrl-O, Enter, Ctrl-X:
dtoverlay=i2c-rtc,ds3231
4. Reboot:
sudo rebootIf the Pi doesn't boot properly afterwards, put the SD card in another computer, open the small bootfs partition, and copy config.txt.bak over config.txt.
5. Check the driver claimed the clock (DigiPi: sudo remount first, since you'll be installing next):
sudo apt install -y i2c-tools
i2cdetect -y 1
ls -l /dev/rtc0What i2cdetect -y 1 shows at 68 |
Meaning |
|---|---|
UU |
✔ The RTC driver is loaded and using the clock |
68 |
The clock is found, but the driver isn't loaded. Check the dtoverlay line and reboot |
-- |
The module isn't answering. Check it's on pins 1–9 and not shifted by a pin |
ls -l /dev/rtc0 should list the clock device.
Raspberry Pi OS includes a tool called fake-hwclock for Pis without a real clock. At every shutdown it saves the current time to a file, and at every boot it puts that time back. With a real RTC it does more harm than good, and on DigiPi it can restore the same old date at every boot, because the read-only filesystem never lets it save a newer one. Remove it:
sudo apt purge -y fake-hwclock
sudo rm -f /etc/fake-hwclock.dataIf it reports "Package 'fake-hwclock' is not installed", it was never there, which is fine.
timedatectl list-timezones | grep America # find yours
sudo timedatectl set-timezone America/New_YorkThe RTC itself always keeps UTC. The time zone only changes how the time is shown.
A new RTC doesn't know the time, so set it once from a good source. Either of these works:
-
GPS: the receiver has a fix (
cgps -sshows a 3D fix and the time). - Internet time (NTP): the Pi is connected to a router with internet access, over Wi-Fi or an Ethernet (RJ45) cable. This is handy for setting up the RTC on the bench, before the station goes to a site with GPS only.
1. Make sure the Pi's clock is correct. Check which source chrony is using:
chronyc sources
dateIn chronyc sources, the line marked with * is the source in use:
Line marked *
|
Source |
|---|---|
| NMEA or GPS | Time from the GPS receiver |
A server name or IP address (for example time.cloudflare.com or 162.159.200.1) |
Internet time (NTP) |
Either one is just as good for this step. date should show the correct date and time. If no line is marked *, wait a minute or two and run chronyc sources again.
If you're relying on GPS and chronyc sources doesn't list a GPS source at all, see the Optional section at the end.
2. Store the time in the RTC, and read it back:
sudo hwclock -w
sudo hwclock -rhwclock -r should show the same time as date, within a second.
From now on, chrony keeps the RTC corrected automatically whenever it has GPS or internet time (the rtcsync setting, on by default).
This is a one-time setup. It makes the Pi copy the time from the RTC into its system clock early in every boot, before other programs start, so they never see a wrong date.
DigiPi: run sudo remount first, or the service file is lost at the next reboot.
1. Create the service file:
sudo nano /etc/systemd/system/rtc-to-system.servicePaste these lines, then save with Ctrl-O, Enter, Ctrl-X:
[Unit]
Description=Set system clock from the RTC
After=systemd-modules-load.service
Before=time-sync.target chrony.service
[Service]
Type=oneshot
ExecStartPre=/bin/sh -c 'for i in $(seq 30); do [ -e /dev/rtc0 ] && exit 0; sleep 1; done; exit 1'
ExecStart=/sbin/hwclock --hctosys --utc
[Install]
WantedBy=multi-user.targetThe ExecStartPre line waits up to 30 seconds for the clock device to appear, then the hwclock line copies the RTC time into the system clock.
2. Turn it on:
sudo systemctl daemon-reload
sudo systemctl enable rtc-to-system.serviceThis is the test that matters: the Pi should come up with the correct time even when GPS isn't available.
-
Unplug the GPS receiver (and, if you can, disconnect the Pi from the internet).
-
Reboot:
sudo reboot
-
Log in and check:
date # correct date and time systemctl status rtc-to-system --no-pager # status=0/SUCCESS
The service shows inactive (dead) with status=0/SUCCESS. That's normal for a one-time boot job: it ran, did its work and finished.
-
Plug the GPS back in. Within a few minutes
chronyc sourcesshows it in use again.
| Task | Command |
|---|---|
| Current time | date |
| Time stored in the RTC | sudo hwclock -r |
| Time sources chrony is using | chronyc sources |
| How accurate the clock is now | chronyc tracking |
| GPS fix and time | cgps -s |
| Copy the system time into the RTC by hand | sudo hwclock -w |
DigiPi sets this up for you. On a plain Raspberry Pi OS install, chrony needs one line to use the GPS through gpsd. Check whether a GPS source is listed:
chronyc sourcesIf there's no NMEA or GPS line, add one (DigiPi: sudo remount first):
echo "refclock SHM 0 refid NMEA offset 0.2 delay 0.2" | sudo tee /etc/chrony/conf.d/gps.conf
sudo systemctl restart chronyAfter a few minutes with a GPS fix, chronyc sources should show NMEA, marked * when no better source is available. The offset 0.2 allows for the usual delay in a GPS receiver's time messages; most receivers fall between 0.1 and 0.5 seconds.
Other devices on the station's network, such as an Ecowitt weather gateway, can take their time from the Pi. This matters at a site without internet: a device that can't set its own clock may stamp its data with a date in 1970.
Add these lines to a settings file (DigiPi: sudo remount first). Replace 10.0.0.0/24 with your own network:
sudo tee /etc/chrony/conf.d/serve-time.conf >/dev/null <<'EOF'
# Serve time to devices on the local network
allow 10.0.0.0/24
# With no GPS and no internet, keep serving the RTC-backed clock
local stratum 10
EOF
sudo systemctl restart chronyThe local stratum 10 line matters. Without it, chrony stops answering other devices whenever it has no GPS or internet source, which is exactly when they need it.
Then set each device's NTP server to the Pi's IP address, ideally a fixed one so it doesn't change. To see which devices are asking:
sudo chronyc clients| Symptom | Likely cause |
|---|---|
i2cdetect -y 1 shows 68, not UU
|
The dtoverlay=i2c-rtc,ds3231 line is missing, misspelled, or not saved (DigiPi: forgot sudo remount) |
| Nothing at 68 | Module not seated, or shifted by one pin; I2C not turned on in raspi-config
|
hwclock: Cannot access the Hardware Clock |
The RTC driver isn't loaded; see the first row |
| Every boot comes up with the same old date |
fake-hwclock is still installed (Step 4) |
| The date is correct while running, but wrong after a power-off | Coin cell missing, flat, or fitted upside down; then set the RTC again (Step 6) |
| RTC shows 2000-01-01 | The RTC was never set, or lost power. Set it again (Step 6) |
| Programs such as WeeWX log "Waiting for sane time" | The clock was wrong at boot. Check Steps 4 and 7 |
chronyc sources never shows a GPS source |
gpsd isn't running or has no fix (cgps -s), or the Optional chrony line is missing |
| A device on the network shows 1970 timestamps | It isn't getting time. See Serve the time to other devices |