Projects / Pi clock that survives power cuts

Pi clock that survives power cuts

Beginner30 minGuided buildReviewed

Build it step by step

0 of 7 done
  1. Pi 5RASPBERR

    Power the Pi off

    Unplug it first.

  2. Pi 5Pi 518:20

    Fit the RTC battery

    Right way round.

  3. Pi 5

    Enable charging

    Add the dtparam line.

  4. Pi 5

    Read the hardware clock

    sudo hwclock -r.

  5. Pi 5RASPBERR

    Remove power and network

    For a few hours.

  6. Pi 5

    Start it with no network

    Check the date.

  7. Pi 5

    Log the drift

    Add the cron line.

In short

How do you build a pi clock that survives power cuts?

Add an RTC so the Pi keeps time without internet, and test pulling the network. You need Raspberry Pi 5, Raspberry Pi 5 RTC battery, SanDisk microSD card 32 GB (A1, Class 10) and Raspberry Pi 27 W USB-C power supply. It takes 7 steps and about 30 minutes.

Level
Beginner
Time
About 30 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
RTC battery connectorPi 5 battery socket next to the USB-C port (matching plug, right way round)Backup for the on-board RTC
Power supplyPi 5 USB-CPower
SD cardPi microSD slotRaspberry Pi OS

Code

Raspberry Pi OS (Bookworm or newer) with Python 3. Fit the Pi 5's RTC battery to the small socket beside the USB-C port with the Pi powered off. The battery is a rechargeable cell, and by default it is not charged, so add the line dtparam=rtc_bbat_vchg=3000000 to /boot/firmware/config.txt and reboot; this charges it to 3 V from the Pi. Check the hardware clock with sudo hwclock -r. To test: with the Pi connected to the network, make sure the time is right, then shut it down, unplug the power and the network for a few hours, and start it with the network still removed; the date and time should be close to correct, not back at the day it last shut down. The script appends the difference between the system clock and the RTC every hour so you can measure the drift over days, which is usually a few seconds a month for a small crystal. Only use the battery made for the Pi 5, and fit it the right way round.

#!/usr/bin/env python3
# Logs how far the system clock is from the battery-backed hardware clock, once an hour. Run from cron: 0 * * * *
import datetime

def hw_time():
    d = open("/sys/class/rtc/rtc0/date").read().strip()      # the RTC counts in UTC
    t = open("/sys/class/rtc/rtc0/time").read().strip()
    return datetime.datetime.strptime(d + " " + t, "%Y-%m-%d %H:%M:%S")

sys_now = datetime.datetime.utcnow()
hw = hw_time()
with open("rtc-drift.log", "a") as f:
    f.write("%s diff_seconds=%.1f\n" % (sys_now.isoformat(timespec="seconds"), (sys_now - hw).total_seconds()))

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
hwclock -rShows the right time
After a few hours unpluggedTime is close to correct
No network at bootDate is not stuck in the past
Drift logSmall differences, a few seconds a month

If it does not work

Date is wrong after a power cut

Charging was not enabled or the battery is flat; charge it for a few hours.

hwclock not found

Use Raspberry Pi OS Bookworm on a Pi 5.

No such file rtc0

The RTC is not enabled; check you are on a Pi 5 with current Raspberry Pi OS.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the pi clock that survives power cuts?

Raspberry Pi 5, Raspberry Pi 5 RTC battery, SanDisk microSD card 32 GB (A1, Class 10) and Raspberry Pi 27 W USB-C power supply.

How long does the pi clock that survives power cuts take to build?

About 30 minutes for a beginner maker, in 7 steps.

How do I know the pi clock that survives power cuts works?

hwclock -r: Shows the right time. After a few hours unplugged: Time is close to correct. No network at boot: Date is not stuck in the past.

Date is wrong after a power cut: what should I check?

Charging was not enabled or the battery is flat; charge it for a few hours.

hwclock not found: what should I check?

Use Raspberry Pi OS Bookworm on a Pi 5.

No such file rtc0: what should I check?

The RTC is not enabled; check you are on a Pi 5 with current Raspberry Pi OS.