Projects / Pi with a battery-backed clock

Pi with a battery-backed clock

A Raspberry Pi 4 keeps correct time without a network by reading a DS3231 clock chip through a device-tree overlay.

Beginner1.7 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. Pi 4

    Shut the Pi down

    Then unplug power.

  2. Pi 4

    Wire the chip

    Pin 1, pin 6, pin 3 and pin 5.

  3. Pi 4

    Boot and enable I2C

    i2cdetect shows 68.

  4. Pi 4DS323115:17

    Add the overlay line

    dtoverlay=i2c-rtc,ds3231 in the boot config.

  5. Pi 4

    Reboot and check

    i2cdetect shows UU.

  6. Pi 4

    Remove fake-hwclock

    Run the three commands.

  7. Pi 4

    Write and read the time

    hwclock -w then hwclock -r.

  8. Pi 4

    Test without a network

    Power off for a few minutes and compare.

In short

How do you build a pi with a battery-backed clock?

A Raspberry Pi 4 keeps correct time without a network by reading a DS3231 clock chip through a device-tree overlay. You need Raspberry Pi 4 Model B (4 GB), DS3231 RTC module and Breadboard 830 Tie Points. It takes 8 steps and about 100 minutes.

Level
Beginner
Time
About 100 min
Parts
3

Parts list

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

Wiring

FromToWhy
DS3231 VCCPi pin 1 (3.3 V)Power at 3.3 V
DS3231 GNDPi pin 6 (GND)Ground
DS3231 SDAPi pin 3 (GPIO2)I2C data
DS3231 SCLPi pin 5 (GPIO3)I2C clock

Code

A Raspberry Pi has no battery-backed clock, so with no network it forgets the time at every power-off and starts from the last time it saved. A DS3231 is a clock chip with its own temperature-compensated crystal and a coin cell that keeps it running when the Pi is off. It sits on the I2C bus at address 0x68. A device-tree overlay tells the kernel which chip is there, so the system reads the time from it at boot without any program of your own. After the overlay is loaded the bus scan shows UU instead of 68, meaning the driver has taken the address. The fake hardware clock service saves the time to a file as a stand-in and must be removed so it does not fight the real chip. Write the correct time to the chip once with hwclock while the Pi is online. Fit the coin cell the right way round, and never try to charge a standard coin cell. If your module has a charging circuit, use the cell type it is made for.

# Run these commands on the Pi. Shut down and wire the chip first, then boot.
sudo raspi-config nonint do_i2c 0               # turn on the I2C bus
sudo i2cdetect -y 1                             # the chip shows as 68

# Tell the kernel about the clock chip: add this line at the end of the boot config file
#   /boot/firmware/config.txt  (older systems: /boot/config.txt)
#   dtoverlay=i2c-rtc,ds3231
sudo reboot

# After the reboot the address shows as UU, meaning the kernel driver owns it
sudo i2cdetect -y 1

# Remove the fake software clock so the real one is used
sudo apt-get -y remove fake-hwclock
sudo update-rc.d -f fake-hwclock remove
sudo systemctl disable fake-hwclock

# With the Pi online and the system time correct, write it to the chip, then read it back
sudo hwclock -w
sudo hwclock -r

# Test: disconnect the network, power off for a few minutes, then power on and run
date
sudo hwclock -r

Test checklist

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

CheckExpected
Before the overlayAddress 68 on the bus scan
After the overlayAddress shows UU
hwclock -rShows the current time
Power off for a few minutes offlineTime is correct on the next boot
Time driftsOnly seconds a week

If it does not work

Nothing at 68

Check wiring and that I2C is enabled.

Address stays 68 after the overlay

Check the overlay line and the config file path, then reboot.

hwclock error

Remove fake-hwclock and reboot.

Time resets to an old date

Check the coin cell and its polarity.

Wrong time zone

Set it with raspi-config.

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

Ask Lark AI

Common questions

What parts do I need for the pi with a battery-backed clock?

Raspberry Pi 4 Model B (4 GB), DS3231 RTC module and Breadboard 830 Tie Points.

How long does the pi with a battery-backed clock take to build?

About 100 minutes for a beginner maker, in 8 steps.

How do I know the pi with a battery-backed clock works?

Before the overlay: Address 68 on the bus scan. After the overlay: Address shows UU. hwclock -r: Shows the current time.

Nothing at 68: what should I check?

Check wiring and that I2C is enabled.

Address stays 68 after the overlay: what should I check?

Check the overlay line and the config file path, then reboot.

hwclock error: what should I check?

Remove fake-hwclock and reboot.

Time resets to an old date: what should I check?

Check the coin cell and its polarity.