⏰ What is a Real-Time Clock (RTC)?

A Real-Time Clock (RTC) is a small, low-power chip that keeps track of the current time and date, even when the computer is powered off. It usually has a small battery (like a CR2032 coin cell) to keep it running when the main system is shut down or disconnected from power.


📌 Do Raspberry Pi & Orange Pi Have an RTC?

No, most Raspberry Pi and Orange Pi boards do not have a built-in RTC.

  • When powered on, they rely on NTP (Network Time Protocol) to sync time from the internet.
  • If there’s no internet, the system clock resets after each reboot.

🔹 Exceptions: Some Orange Pi boards, like Orange Pi 5 and some industrial SBCs, come with an RTC header where you can add an external RTC module.


🛠️ Do You Need an RTC on Your SBC?

It depends on what you’re using it for:

You might need an RTC if:

  • Your SBC runs offline (no internet) and needs to keep the correct time.
  • You’re using it for data logging, security systems, or automation that depends on accurate timestamps.
  • You’re running scheduled tasks (cron jobs) that require precise timing after a reboot.

You probably don’t need an RTC if:

  • Your board is always connected to the internet and can sync time via NTP.
  • Your tasks don’t rely on accurate timestamps after a reboot.

🕰️ Adding an RTC to Raspberry Pi or Orange Pi

If you need an RTC, you can buy a small I²C RTC module like:

  1. DS3231 (high accuracy, recommended)
  2. DS1307 (cheaper, but less accurate)

How to connect it?

  • Wire it to the I²C pins of your SBC.
  • Install the appropriate drivers.
  • Set the system clock to read from the RTC at boot.

🛠️ How to Add an RTC (Real-Time Clock) to Your Raspberry Pi or Orange Pi

We’ll use the DS3231 module, which is more accurate than the DS1307. The setup process is the same for both Raspberry Pi and Orange Pi.


1️⃣ Connect the RTC Module

The RTC module communicates using I²C, so wire it as follows:

RTC Module (DS3231/DS1307)Raspberry Pi GPIOOrange Pi GPIO
VCC (Power)3.3V (Pin 1)3.3V (Pin 1)
GND (Ground)GND (Pin 6)GND (Pin 6)
SDA (Data)SDA1 (Pin 3)SDA0 (Pin 3)
SCL (Clock)SCL1 (Pin 5)SCL0 (Pin 5)

⚠️ Make sure you’re connecting to 3.3V, not 5V! The DS3231 can work with both, but your SBC’s I²C pins are 3.3V logic.


2️⃣ Enable I²C on Your SBC

📌 For Raspberry Pi
Enable I²C using raspi-config:

sudo raspi-config
  • Go to Interfacing Options → I2C → Enable
  • Reboot your Pi:
    sudo reboot
    

📌 For Orange Pi (Armbian)
Edit the hardware configuration file:

sudo nano /boot/armbianEnv.txt
  • Add this line (if not already present):
    overlays=i2c1
    
  • Save and exit (Ctrl+X, then Y, then Enter).
  • Reboot:
    sudo reboot
    

3️⃣ Detect the RTC Module

After reboot, check if the RTC is detected:

sudo i2cdetect -y 1   # For Raspberry Pi
sudo i2cdetect -y 0   # For Orange Pi

You should see an output like this:

     0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- 68 -- -- -- --

If you see 68, your RTC is working! 🎉


4️⃣ Set Up the RTC as the System Clock

1️⃣ Load the RTC kernel module:

sudo modprobe rtc-ds3231

2️⃣ Add it to startup (so it loads automatically):

echo "rtc-ds3231" | sudo tee -a /etc/modules

3️⃣ Disable fake hardware clock (Armbian & Raspbian do this by default):

sudo systemctl disable fake-hwclock
sudo systemctl stop fake-hwclock

4️⃣ Tell the system to use the RTC at boot:

sudo hwclock -w   # Save current system time to RTC
sudo hwclock -r   # Read the RTC time

If the RTC shows the correct time, you’re good to go!


5️⃣ Sync Time from the RTC at Boot

To ensure your SBC always uses the RTC time at startup:

1️⃣ Edit the rc.local file:

sudo nano /etc/rc.local

2️⃣ Before exit 0, add this line:

hwclock -s

3️⃣ Save & exit, then reboot:

sudo reboot

After reboot, check the time:

date
hwclock -r

If both times match, your RTC is working perfectly! ✅


💡 Conclusion

  • If your SBC has internet, it can sync time via NTP and may not need an RTC.
  • If your SBC runs offline (no internet), an RTC ensures it keeps accurate time.
  • The DS3231 RTC is recommended because it’s more accurate than the DS1307.

Here’s a bash script that automates the setup of an RTC module (DS3231) on your Raspberry Pi or Orange Pi running Armbian. This script:

✅ Enables I²C
✅ Detects the RTC module
✅ Loads the necessary kernel module
✅ Disables the fake hardware clock
✅ Sets the RTC as the system clock at boot


📜 RTC Setup Script

Save this as setup_rtc.sh and run it with sudo.

#!/bin/bash

echo "🔧 Setting up RTC (DS3231) on your SBC..."

# Detect whether we are on a Raspberry Pi or an Orange Pi
if [ -f /boot/config.txt ]; then
    SBC="Raspberry Pi"
    I2C_BUS="1"
    CONFIG_FILE="/boot/config.txt"
elif [ -f /boot/armbianEnv.txt ]; then
    SBC="Orange Pi (Armbian)"
    I2C_BUS="0"
    CONFIG_FILE="/boot/armbianEnv.txt"
else
    echo "❌ Unsupported system. Exiting."
    exit 1
fi

echo "🛠 Detected SBC: $SBC"

# Enable I2C
echo "🔧 Enabling I2C..."
if [ "$SBC" = "Raspberry Pi" ]; then
    sudo raspi-config nonint do_i2c 0
elif [ "$SBC" = "Orange Pi (Armbian)" ]; then
    sudo sed -i '/^overlays=/ s/$/ i2c1/' $CONFIG_FILE
fi

# Reboot for changes to take effect
echo "🔄 Rebooting to apply changes..."
sudo reboot

# Detect RTC
echo "🔍 Checking if RTC is detected..."
sudo apt-get install -y i2c-tools
sudo i2cdetect -y $I2C_BUS

# Load RTC kernel module
echo "📦 Loading RTC kernel module..."
sudo modprobe rtc-ds3231
echo "rtc-ds3231" | sudo tee -a /etc/modules

# Disable fake hardware clock
echo "❌ Disabling fake-hwclock..."
sudo systemctl disable fake-hwclock
sudo systemctl stop fake-hwclock

# Sync system time to RTC
echo "⏰ Syncing system time to RTC..."
sudo hwclock -w
sudo hwclock -r

# Ensure RTC time is used at boot
echo "🔧 Configuring RTC to set system time at boot..."
sudo sed -i '/exit 0/i hwclock -s' /etc/rc.local

echo "✅ RTC setup complete! Rebooting..."
sudo reboot

🛠️ How to Use the Script

  1. Download & Save the Script

    nano setup_rtc.sh
    
    • Paste the script into the editor.
    • Save with CTRL+X, then Y, then ENTER.
  2. Make it Executable

    chmod +x setup_rtc.sh
    
  3. Run the Script

    sudo ./setup_rtc.sh
    
  4. After the Reboot, Check if RTC is Working

    date
    hwclock -r
    

🎯 What This Script Does

  • Automatically detects if you’re on a Raspberry Pi or Orange Pi (Armbian)
  • Enables I²C and reboots the system
  • Loads the RTC module (rtc-ds3231)
  • Disables fake-hwclock (to prevent conflicts)
  • Writes system time to RTC
  • Ensures RTC sets the system time at boot

This should set up your DS3231 RTC with minimal effort! 🚀