PCF8523 Real-Time Clock

The PCF8523 [1] is a battery-backed I2C real-time clock that can run from 1.0 V to 5.5 V. It requires a 32.768 kHz quartz crystal to operate.

A breakout board is available from Adafruit [2]. Alternatively you can mount the chip yourself on an SOIC-8 breakout board. I recommend you omit header pins from the crystal connections, and mount the crystal directly to the breakout board:


The I2C address is #x68 or 104.

Setting the crystal

By default the RTC is calibrated for a 7 pF 32.768 kHz crystal. It can alternatively be configured for a 12.5 pF crystal:

(defun pcf8523-cal ()
  (with-i2c (str 104)
    (write-byte 0 str)
    (write-byte #x80 str)))

Give the command:


Setting the time

The following routine sets the time:

(defun pcf8523-set (hr min)
  (with-i2c (str #x68)
    (write-byte 0 str)
    (write-byte 0 str)
    (write-byte min str)
    (write-byte hr str)))

The first call to write-byte sets the starting register to write to in the DS3234; in this case 0, the seconds register. We then set the seconds to 0, the minutes to min, and the hours to hr. The times are defined in binary-coded decimal, so to set the time to 12:34 you need to evaluate:

(pcf8523-set #x12 #x34)

Once you have set the time the backup battery will keep the time correct in the PCF8523.

Reading the time

Here's the routine pcf8523-time to read the time:

(defun pcf8523-time ()
(with-i2c (str 104) (write-byte 3 str) (restart-i2c str 3) (list (read-byte str) (read-byte str) (read-byte str))))

It returns the time as a list of three binary-coded decimal numbers: (sec min hrs). For example 12:34:56 will be:

(86 52 18)

because 86 is #x56, 52 is #x34, and 18 is #x12. It's a simple extension to this to read the date and other registers; see the datasheet.

  1. ^ PCF8523 Datasheet on NXP.com.
  2. ^ Adafruit PCF8523 Real Time Clock Assembled Breakout Board on Adafruit.