Site Information

 Loading... Please wait...

Programming the PCF8523 RTC Using Raspberry Pi and Python

Posted by Wilfried Voss on

Programming the PCF8523 RTC Using Raspberry Pi and Python1. Introduction

The PCF8523 is a low-power real-time clock (RTC) module from NXP. It is often used in embedded systems to maintain accurate timekeeping, even when the host system is powered off. This report details the setup and programming of the PCF8523 RTC using a Raspberry Pi and Python, leveraging the I²C interface.

2. System Requirements

2.1 Hardware

  • Raspberry Pi (any model with GPIO and I²C support; e.g., Raspberry Pi 4 Model B)

  • PCF8523 RTC Module (often available as a breakout board)

  • CR1220 coin cell battery (for backup timekeeping)

  • Jumper wires

2.2 Software

  • Raspberry Pi OS (Bookworm, Bullseye, or earlier)

  • Python 3.x

  • I2C Tools (i2c-tools package)

  • Adafruit CircuitPython PCF8523 library

  • Adafruit Blinka (CircuitPython compatibility layer for Raspberry Pi)

3. Hardware Setup

3.1 Wiring

PCF8523 Pin Raspberry Pi GPIO Pin
VCC 3.3V (Pin 1)
GND GND (Pin 6)
SDA SDA (GPIO 2, Pin 3)
SCL SCL (GPIO 3, Pin 5)

Ensure that the RTC module is powered with 3.3V (not 5V) to avoid damage.

4. Enabling I²C on Raspberry Pi

  1. Open terminal and enter:

    sudo raspi-config
  2. Navigate to Interfacing Options > I2C and enable it.

  3. Reboot the Raspberry Pi:

    sudo reboot
  4. After reboot, install the I²C tools:

    sudo apt update sudo apt install -y i2c-tools
  5. Detect the RTC:

    i2cdetect -y 1

    You should see the address 0x68 or 0xA2 depending on the breakout board.

5. Installing Python Libraries

  1. Install pip if not already installed:

    sudo apt install -y python3-pip
  2. Install Adafruit Blinka and the PCF8523 library:

    pip3 install adafruit-blinka
    pip3 install adafruit-circuitpython-pcf8523

6. Python Example Code

import time
import board
import busio
import adafruit_pcf8523
import datetime
# Initialize I2C
i2c = busio.I2C(board.SCL, board.SDA)
# Initialize RTC
rtc = adafruit_pcf8523.PCF8523(i2c)
# Uncomment to set time (do this once)
# now = datetime.datetime(2025, 4, 9, 12, 0, 0)
# rtc.datetime = now
# Read time from RTC
while True:
t = rtc.datetime
print("Current RTC Time: {}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(
t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec))
time.sleep(1)

6.1 Setting the Time

To set the time, uncomment the rtc.datetime = now line and modify the datetime accordingly. After running the script once, you can comment it out to avoid overwriting the time on every run.

6.2 DateTime Format

The rtc.datetime property returns a time.struct_time object.

7. Advanced Usage

7.1 Battery Backup

The PCF8523 supports battery backup via a CR1220 coin cell. Ensure it's installed correctly to maintain time when the Pi is off.

7.2 Alarm and Timer Features

The PCF8523 supports alarms and countdown timers. However, these are not implemented in the Adafruit library at the time of writing. For advanced features, consider writing raw I²C commands using smbus2 or expanding the Adafruit driver.

8. Conclusion

The PCF8523 RTC is a reliable module for maintaining time on a Raspberry Pi system. With the help of Adafruit’s CircuitPython library and Blinka, integrating and using the RTC is straightforward. This setup is useful in projects requiring timestamping, data logging, or time-based automation.


9. External References


PICAN CAN Bus FD Board With Real-Time Clock For Raspberry PiPICAN CAN Bus FD Board With Real-Time Clock For Raspberry Pi

The PiCAN FD board adds CAN Bus FD (Flexible Data Rate) capability to the Raspberry Pi 3, utilizing the Microchip MCP2517FD CAN FD controller and the MCP2562FD CAN transceiver. Designed by Bosch, CAN FD is an enhancement of the original CAN protocol (ISO 11898-1), offering increased data bandwidth and efficiency for modern automotive and industrial networks.

The board includes onboard connection options via DB9 connector or a 4-way screw terminal, making integration into a variety of systems easy. An optional 5V 1A switched-mode power supply (SMPS) version is available, allowing the Raspberry Pi to be powered directly through either the screw terminal or DB9 connector.

In addition, the board features an onboard PCF8523 real-time clock (RTC) with battery backup support (coin cell battery not included). This RTC ensures accurate timekeeping even when the Raspberry Pi is powered down, which is critical for time-stamping CAN messages and maintaining logs in remote or disconnected applications.

The board is supported by an easy-to-install SocketCAN driver, making it compatible with standard Linux CAN tools. Programming interfaces are available in both C and Python, offering flexibility for developers building automotive, robotics, or industrial automation systems. More information...