Raspberry Pi Real-Time Clock (RTC): DS3231 Setup and the Pi 5’s Built-In Clock

Raspberry Pi RTC guide covering DS3231 setup and the Raspberry Pi 5 built-in real-time clock

A Raspberry Pi RTC (real-time clock) keeps the date and time while the Pi is switched off and offline. The Raspberry Pi 5 has one built in and only needs its NZ$10 backup battery; a Pi 4, Pi 3 or Zero 2 W needs an add-on clock such as a DS3231, which takes four wires and one line of config. This guide is for makers running Pis in sheds, baches, boats and vans, or anywhere the internet comes and goes.

Without a clock, the Pi depends on internet time servers and starts every boot believing it is whatever time it last saved. If you are new to the boards themselves, our Raspberry Pi explainer is the place to start, and if you are still choosing hardware, see Arduino vs ESP32 vs Raspberry Pi.

Key takeaways

  • Raspberry Pi 5: plug the official RTC battery (SC1163, NZ$10.32 at PB Tech) into the J5 connector. Charging stays off until you add one config line.
  • Older Pis: a DS3231 module is the best add-on, rated at ±2 ppm (about a minute a year) between 0 and 40 °C.
  • Power the module from 3.3 V, not 5 V, using header pins 1, 3, 5 and 6.
  • On current Raspberry Pi OS (Bookworm and Trixie) the overlay line goes in /boot/firmware/config.txt, not /boot/config.txt.
  • The RTC stores UTC, and Raspberry Pi OS handles NZ daylight saving through the time zone setting.

Why a Raspberry Pi loses the time

Most Raspberry Pi boards have no battery-backed clock. While running, the Pi counts time with its own oscillator and corrects it from internet time servers (NTP) using systemd-timesyncd. When the power goes, the count stops.

To avoid booting with a wildly wrong date, Raspberry Pi OS has long used fake-hwclock, a small service that saves the current time to a file every hour and at shutdown, then restores it at boot. That keeps the date roughly in order, but a Pi that was switched off for a week and has no internet boots up a week behind. That matters more than it sounds:

  • Data loggers, weather stations and a DIY home energy monitor record readings against the wrong time.
  • Scheduled jobs, from cron tasks to Home Assistant automations, fire at the wrong moment.
  • HTTPS and VPN connections can fail, because certificates look as if they are not yet valid.
  • A Pi-based CCTV recorder stamps footage with misleading times, which undermines the point of a home security camera system.
  • Logs on a Pi home server become hard to follow after an outage.

A real-time clock fixes all of this: a tiny chip with its own crystal and backup battery that keeps counting when everything else is off.

Raspberry Pi 5 RTC battery: using the built-in clock

The Pi 5 is the first mainstream Raspberry Pi with a real-time clock on the board. It works without a battery while the Pi has power, but to keep time when unplugged it needs a backup cell on the J5 (BAT) connector, near the corner of the board just to the right of the USB-C power socket.

Which battery to fit

Use the official Raspberry Pi RTC Battery (SC1163): a rechargeable ML2020 lithium manganese coin cell with a pre-fitted two-pin JST plug and an adhesive pad for mounting. PB Tech listed it at NZ$10.32 in September 2026. Raspberry Pi’s documentation warns against lithium-ion cells, and against ordinary non-rechargeable lithium coin cells, because the Pi 5’s RTC draws more backup current than a dedicated clock chip.

Turn on battery charging

Charging is disabled by default, so a new rechargeable cell slowly runs flat unless you enable the trickle charger at 3.0 V:

# Open the firmware config file
sudo nano /boot/firmware/config.txt

# Add this line at the very end (under [all]), save, then reboot
dtparam=rtc_bbat_vchg=3000000

# After the reboot: confirm the charge voltage and read the clock
cat /sys/class/rtc/rtc0/charging_voltage
sudo hwclock -r

The charging_voltage file should read 3000000 (microvolts). With a charged battery, the Pi 5 comes back from a power cut with the right time, no network needed and nothing else to install.

Wake-up alarms

The RTC can also switch a halted Pi 5 back on at a set time, which suits solar-powered cameras and loggers that only need to run a few times a day. Our solar panel and battery calculator shows how much panel and battery a setup like that needs.

# Ask the RTC to power the Pi 5 back on 10 minutes (600 s) after it halts
echo +600 | sudo tee /sys/class/rtc/rtc0/wakealarm
sudo halt

# Optional: lowest-power halt, set in the bootloader EEPROM
sudo rpi-eeprom-config --edit
#   then add or change these two lines:
POWER_OFF_ON_HALT=1
WAKE_ON_GPIO=0

DS3231 Raspberry Pi setup (Pi 4, Pi 3 and Zero 2 W)

Every other Raspberry Pi model needs an external clock, and the DS3231 is the one to buy. It has a temperature-compensated crystal built into the chip, rated by Analog Devices at ±2 ppm between 0 and 40 °C, so it stays within about a minute a year. Bare modules are cheap and NZ suppliers such as ePartners list them; alternatively, an RTC HAT plugs straight onto the first ten header pins. Many of these add-ons are open-source hardware designs with published schematics.

Raspberry Pi RTC wiring diagram: DS3231 module VCC to pin 1 (3V3), SDA to pin 3 (GPIO 2), SCL to pin 5 (GPIO 3), GND to pin 6, plus the Raspberry Pi 5 J5 battery connector
DS3231 wiring to the first pins of the Raspberry Pi header, and the Pi 5 alternative. Pin numbers from the Raspberry Pi GPIO documentation.

Step 1: Wire the module

With the Pi switched off, connect four wires:

DS3231 pinRaspberry Pi pinSignal
VCCPin 13.3 V power
SDAPin 3GPIO 2 (I2C data)
SCLPin 5GPIO 3 (I2C clock)
GNDPin 6Ground

Take power from pin 1 (3.3 V). Most modules have pull-up resistors on SDA and SCL tied to VCC, so powering them from 5 V would push 5 V into the Pi’s 3.3 V I2C pins.

Step 2: Enable I2C and find the chip

sudo raspi-config nonint do_i2c 0
sudo apt update
sudo apt install -y i2c-tools util-linux-extra
sudo i2cdetect -y 1

The first command switches on the I2C interface (the same as Interface Options in raspi-config), and the install line adds i2cdetect and hwclock if they are missing. The last command prints a grid of addresses: you should see 68, the DS3231. Many cheap modules also show 57, a small memory chip on the same board, which you can ignore. An empty grid means a wiring problem.

Step 3: Load the RTC driver

Since Raspberry Pi OS Bookworm, the firmware configuration lives in /boot/firmware/config.txt. Add the i2c-rtc overlay with the ds3231 parameter:

sudo nano /boot/firmware/config.txt

# Add this line at the very end (under [all]), save, then reboot
dtoverlay=i2c-rtc,ds3231

sudo reboot

# After the reboot, address 68 should now show as UU
sudo i2cdetect -y 1
ls /dev/rtc*

After the reboot, 68 turns into UU, meaning the kernel driver has claimed the chip, and /dev/rtc0 appears. For other chips, swap the parameter; pcf8523, ds1307 and many more are listed in the Raspberry Pi overlays README.

Step 4: Set the clock with hwclock

A new module does not know the time. Let the Pi fetch the correct time from the internet, then copy it across:

timedatectl        # wait until it says: System clock synchronized: yes
sudo hwclock -w    # write the system time into the RTC
sudo hwclock -r    # read the RTC back

You only need to do this once. If the Pi is usually online, repeating sudo hwclock -w every so often keeps the RTC within a second or two. The RTC holds UTC, which is what Raspberry Pi OS expects.

Step 5: Read the RTC at every boot

This is the step older tutorials get wrong for current releases. Guides written for Raspbian Jessie and Buster tell you to edit /lib/udev/hwclock-set, but that script is missing on some current installs, including Trixie-based ones, and where it does exist it exits early on systemd systems unless edited. A one-line udev rule of your own is simpler, and it relies only on udev and hwclock:

# Remove the stand-in clock (harmless if it is not installed)
sudo apt purge -y fake-hwclock

# Copy the RTC time into the system clock every time the RTC appears at boot
echo 'ACTION=="add", SUBSYSTEM=="rtc", KERNEL=="rtc0", RUN+="/sbin/hwclock --rtc=/dev/rtc0 --hctosys --utc"' | sudo tee /etc/udev/rules.d/85-rtc-hctosys.rules

sudo reboot

To test it, shut the Pi down, disconnect Ethernet and Wi-Fi, wait a few minutes, then boot and run date or timedatectl. The time should be correct before the Pi has any chance to reach an internet time server.

DS1307 vs DS3231 vs PCF8523: which I2C RTC for a Raspberry Pi?

ClockAccuracySupplyVerdict for a Pi
DS3231SN±2 ppm from 0 to 40 °C (about 1 minute a year)2.3–5.5 VBest choice: accurate and happy on 3.3 V
DS3231M±5 ppm (MEMS resonator)2.3–5.5 VGood, slightly less accurate
PCF8523Set by its external crystal, typically ±20 ppmWorks at 3.3 VFine for most projects
DS1307Set by its external crystal4.5–5.5 VAvoid: a 5 V chip
Pi 5 built-inNot published by Raspberry PiBoard power plus ML2020 cellEasiest if you have a Pi 5

Parts per million are easier to picture as time. Use the calculator to compare chips over the period your Pi might spend offline:

Free tool

RTC drift calculator

See how far a Raspberry Pi’s clock can wander while it has no internet time, depending on which real-time clock chip keeps it.

Worst-case drift
Per month
Per year
No RTC, Pi switched off that long

Worst case = accuracy in ppm × elapsed time (1 ppm is about 2.6 seconds a month). DS3231 limits are from the Analog Devices datasheet; ±20 ppm is a typical tolerance for the 32.768 kHz crystals used by DS1307 and PCF8523 boards at room temperature, and crystal clocks drift further when cold or hot. Without an RTC, a Pi that has been switched off restarts from the last time it saved, so it is behind by however long it was off.

In short, a DS3231 is still within seconds after a month offline, while a crystal-based DS1307 or PCF8523 can be close to a minute out at room temperature, and further out in a cold shed or a hot roof space.

Time zones and NZ daylight saving

The RTC should always hold UTC. Raspberry Pi OS converts that to local time using the time zone, so make sure it is set to Pacific/Auckland (or Pacific/Chatham on the Chatham Islands) with sudo timedatectl set-timezone Pacific/Auckland, or through Localisation Options in raspi-config. Daylight saving changes then happen automatically, including the switch to NZDT on Sunday 27 September 2026, and the RTC never needs adjusting.

If the clock is out by exactly 12 or 13 hours, the RTC was set in local time. Run sudo timedatectl set-local-rtc 0 and then sudo hwclock -w to put it back on UTC.

Troubleshooting a Raspberry Pi RTC

  • i2cdetect shows nothing: I2C is not enabled, or SDA and SCL are swapped. Check pins 3 and 5.
  • 68 appears but never changes to UU: the overlay line is missing, misspelt, or sitting under a section such as [cm4] that does not apply to your board. Put it under [all] at the end of the file.
  • hwclock reports that it cannot access the hardware clock: the driver has not loaded or hwclock is missing. Install util-linux-extra and check that /dev/rtc0 exists.
  • Correct until a reboot without internet: the boot-time rule from step 5 is missing, or fake-hwclock is still installed.
  • Time wrong after months in storage: the backup cell is flat. Many cheap DS3231 boards include a charging circuit meant for a rechargeable LIR2032 cell, so if yours has one and you fit a standard CR2032, follow the seller’s advice; makers often disable that circuit.
  • Pi 5 forgets the time when unplugged: the battery is missing, or charging was never enabled and the cell has run down.

For time-critical work such as scientific logging, a GPS receiver with a pulse-per-second output and chrony is the next step up. An RTC keeps time between syncs; it is not a precision time source.

Our take

  • On a Raspberry Pi 5, buy the official RTC battery, enable charging, and you are done.
  • On a Pi 4, Pi 3 or Zero 2 W, fit a DS3231 powered from 3.3 V, add the overlay line, and add the udev rule from step 5.
  • Skip the DS1307: it is a 5 V chip, and its accuracy depends on an external crystal.
  • Whatever you fit, set the time zone to Pacific/Auckland and let the RTC hold UTC.

Steps reflect Raspberry Pi’s documentation and the Raspberry Pi OS releases current in September 2026, when prices were also checked; details can change with OS updates. Nicegear is independent, sells nothing and has no paid placements.

FAQs

Does the Raspberry Pi have a real-time clock?

The Raspberry Pi 5 does, and it keeps time when unplugged once you fit the rechargeable RTC battery to its J5 connector. The Pi 4, Pi 3 and Zero boards have no RTC, so they need an add-on module such as a DS3231 to keep time while switched off and offline.

What battery does the Raspberry Pi 5 RTC use?

Raspberry Pi recommends its official RTC battery, part SC1163, a rechargeable ML2020 lithium manganese cell with a two-pin JST plug. Enable charging with dtparam=rtc_bbat_vchg=3000000 in /boot/firmware/config.txt, and avoid lithium-ion or non-rechargeable coin cells.

How can a Raspberry Pi keep time without internet?

Fit a real-time clock: the built-in one on a Pi 5 with its battery, or a DS3231 module on older models. The Pi then reads the correct time from the RTC at boot and only needs the internet to correct small amounts of drift.

What is fake-hwclock on a Raspberry Pi?

It is a small service that saves the time to a file regularly and restores it at boot, so a Pi without an RTC does not start with a wildly wrong date. It cannot count time while the Pi is off, so remove it once a real RTC is fitted.

Is the DS3231 better than the DS1307?

For a Raspberry Pi, yes. The DS3231 runs happily on 3.3 V and has a built-in temperature-compensated crystal rated at ±2 ppm, while the DS1307 is a 5 V chip whose accuracy depends on an external crystal.

How do I check the RTC time on a Raspberry Pi?

Run sudo hwclock -r to read the RTC directly, or timedatectl to see the RTC time alongside the system time and whether network time sync is active.

Does a Raspberry Pi RTC handle daylight saving?

It does not need to: the RTC stores UTC, and Raspberry Pi OS applies NZ daylight saving rules through the Pacific/Auckland time zone. Just make sure the time zone is set correctly.