The Adafruit PiRTC PCF8523 is a real-time clock (RTC) module that provides accurate timekeeping capabilities to Raspberry Pi projects. It is an essential component for time-sensitive applications where the Raspberry Pi may not have a network connection to update the time. The module uses the NXP PCF8523 RTC chip and maintains the time with a coin cell battery, ensuring that the clock continues to run even when the Raspberry Pi is powered off.
Pin Number | Name | Description |
---|---|---|
1 | GND | Ground connection |
2 | VCC | Power supply (3.3V) |
3 | SDA | I2C Data Line |
4 | SCL | I2C Clock Line |
5 | SQW | Square Wave/Interrupt Output (not used by default) |
raspi-config
or the Raspberry Pi Configuration tool to enable the I2C interface.i2cdetect
command to verify that the Raspberry Pi can detect the RTC module on the I2C bus.Q: Can the PiRTC module be used with other microcontrollers? A: Yes, as long as the microcontroller supports I2C communication and operates at 3.3V logic levels.
Q: How long will the battery last? A: The battery life depends on the quality of the battery and the environmental conditions but typically lasts for several years.
Q: Does the module automatically handle daylight saving time changes? A: No, daylight saving time changes must be handled by the operating system or user application.
Below is an example Python script to set and read the time from the Adafruit PiRTC PCF8523 module using the smbus
library. This script assumes that the I2C interface has been enabled and the smbus
library is installed on the Raspberry Pi.
import smbus
import time
bus = smbus.SMBus(1)
RTC_ADDRESS = 0x68
RTC_SECONDS_REG = 0x03 RTC_MINUTES_REG = 0x04 RTC_HOURS_REG = 0x05
def set_time(hours, minutes, seconds): """Set the time on the RTC.""" # Write to RTC registers to set the time bus.write_byte_data(RTC_ADDRESS, RTC_SECONDS_REG, seconds) bus.write_byte_data(RTC_ADDRESS, RTC_MINUTES_REG, minutes) bus.write_byte_data(RTC_ADDRESS, RTC_HOURS_REG, hours)
def read_time(): """Read the current time from the RTC.""" # Read from RTC registers to get the current time seconds = bus.read_byte_data(RTC_ADDRESS, RTC_SECONDS_REG) minutes = bus.read_byte_data(RTC_ADDRESS, RTC_MINUTES_REG) hours = bus.read_byte_data(RTC_ADDRESS, RTC_HOURS_REG) return hours, minutes, seconds
set_time(12, 34, 56) # Set time to 12:34:56 time.sleep(1) # Wait for a second current_time = read_time() print(f"Current Time: {current_time[0]:02}:{current_time[1]:02}:{current_time[2]:02}")
Please note that this example does not handle the conversion of binary-coded decimal (BCD) values, which is how the PCF8523 stores time. Additional code is required to convert between BCD and integer values for proper time setting and reading.