DEV Community

Hedy
Hedy

Posted on

How to add an RTC (Real-Time Clock) module to your Raspberry Pi?

Here’s a step-by-step guide to adding and configuring an RTC (Real-Time Clock) module (like the DS3231 or PCF8523) to your Raspberry Pi:

What You’ll Need

  1. RTC Module (e.g., DS3231 or PCF8523 – both are highly accurate).
  2. Raspberry Pi (any model with I²C pins).
  3. Battery Holder & Coin Cell Battery (CR2032, typically).
  4. Jumper Wires (female-to-female for most HATs/modules).
  5. Optional: Soldering iron (if your module isn’t pre-soldered).

Step 1: Connect the RTC Module
Wiring (I²C Connection)

Note: Some modules use 5V, but most RTCs (like DS3231) work fine at 3.3V. Double-check your module’s datasheet!

Step 2: Enable I²C on the Pi

  1. Open the Raspberry Pi terminal and run:
bash
sudo raspi-config
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to:
    Interfacing Options → I2C → Enable

  2. Reboot the Pi:

bash
sudo reboot
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the RTC is Detected
After rebooting, check if the RTC is visible on the I²C bus:

bash
sudo i2cdetect -y 1
Enter fullscreen mode Exit fullscreen mode

You should see the RTC’s address (e.g., 0x68 for DS3231 or 0x51 for PCF8523).

Step 4: Configure the Kernel to Use the RTC

  1. Edit the boot config file:
bash
sudo nano /boot/config.txt
Enter fullscreen mode Exit fullscreen mode
  1. Add one of these lines (depending on your RTC module):

For DS3231:

text
dtoverlay=i2c-rtc,ds3231
Enter fullscreen mode Exit fullscreen mode

For PCF8523:

text
dtoverlay=i2c-rtc,pcf8523
Enter fullscreen mode Exit fullscreen mode
  1. Save (Ctrl+X, then Y, then Enter) and reboot:
bash
sudo reboot
Enter fullscreen mode Exit fullscreen mode

Step 5: Sync System Time to the RTC

  1. Check if the RTC is recognized:
bash
sudo hwclock -r
Enter fullscreen mode Exit fullscreen mode

If the time is wrong, set it manually (skip if using NTP first).

  1. Sync System Time → RTC (if Pi has internet/NTP):
bash
sudo hwclock -w
Enter fullscreen mode Exit fullscreen mode

This writes the Pi’s current time (from NTP) to the RTC.

  1. Sync RTC → System Time on Boot (for offline use): Edit /etc/rc.local (before exit 0):
bash
sudo hwclock -s
exit 0
Enter fullscreen mode Exit fullscreen mode

Step 6: Test the RTC

  • Disconnect the Pi from power (and internet).
  • Wait a few minutes, then power it back on.
  • Run:
bash
timedatectl
Enter fullscreen mode Exit fullscreen mode

The time should still be correct (synced from the RTC).

Troubleshooting
No I²C device detected?

  • Check wiring (SDA/SCL swapped? Bad connections?).
  • Verify the RTC module isn’t defective.

hwclock fails?

  • Ensure the correct dtoverlay is set in /boot/config.txt.
  • Try reinstalling the RTC kernel module:
bash
sudo apt install --reinstall i2c-tools
Enter fullscreen mode Exit fullscreen mode

Why This Works

  • The RTC keeps time while the Pi is off using the coin cell battery.
  • On boot, the Pi loads the time from the RTC (if no NTP is available).
  • Linux treats the RTC as a hardware clock (/dev/rtc0).

Optional: Automatic NTP Sync (Hybrid Setup)
If the Pi occasionally connects to the internet, you can sync the RTC to NTP periodically:

  1. Install ntpdate:
bash
sudo apt install ntpdate
Enter fullscreen mode Exit fullscreen mode
  1. Create a cron job to update the RTC daily:
bash
sudo crontab -e
Enter fullscreen mode Exit fullscreen mode

Add:

text
0 3 * * * /usr/sbin/ntpdate -u pool.ntp.org && /sbin/hwclock -w
Enter fullscreen mode Exit fullscreen mode

Now your Raspberry Pi has persistent timekeeping, even offline!

Top comments (0)