Recent Posts
Programming the PCF8523 RTC Using Raspberry Pi and Python
Posted by
on
1. 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
-
Open terminal and enter:
-
Navigate to
Interfacing Options
>I2C
and enable it. -
Reboot the Raspberry Pi:
-
After reboot, install the I²C tools:
-
Detect the RTC:
You should see the address
0x68
or0xA2
depending on the breakout board.
5. Installing Python Libraries
-
Install
pip
if not already installed: -
Install Adafruit Blinka and the PCF8523 library:
6. Python Example Code
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
-
Adafruit PCF8523 Library: https://github.com/adafruit/Adafruit_CircuitPython_PCF8523
-
Adafruit Blinka (CircuitPython on Raspberry Pi): https://learn.adafruit.com/circuitpython-on-raspberrypi-linux
-
Raspberry Pi I²C Documentation: https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#i2c
-
PCF8523 Datasheet: https://www.nxp.com/docs/en/data-sheet/PCF8523.pdf
-
I2C Tools for Linux: https://wiki.debian.org/I2CTools
PICAN 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...