Sponsored links

Using PCF2129, a real-time clock IC, with ESP32

Sponsored links

Sponsored links

PCF2129

Operates at 3.3V

The PCF2129 is a real-time clock IC that can run at 3.3V, and is connected to the microcontroller via I2C or SPI. DS3231 is a very famous real-time clock IC for Arduino, but it only runs at 5V. However, it is designed for 5V. For this reason, I think the PCF2129 is more suitable for the ESP32 that runs on 3.3V than the DS3231.

There are two types of PCF2129, PCF2129T and PCF2129AT, with different packages. Since the functions are the same, I use PCF2129AT which has better availability.

https://s.click.aliexpress.com/e/_ANelcV

Circuit

PCF2129のI2C接続回路

The schematic above shows the circuit for connecting to the ESP32 via I2C. 0.22F supercapacitors are used in VBAT for battery backup. I used a small supercapacitor, which does not need to be replaced, because a battery needs to be replaced due to its life span, and its large size requires a lot of space on the board.

I connected LEDs to the interrupt output and CLKOUT output to check the operation. The LED turns off when the interrupt signal or CLKOUT goes low.

Soldering

I mounted the circuit of PCF2129 on the pitch conversion board of Akizuki Denshi.

Insert the ESP32-DevKitC and the module I just made into the breadboard, and wire it up to complete the circuit.
https://s.click.aliexpress.com/e/_AWWYfR

Program

Modify the existing library

PCF2129 has a very useful library that can be used with Arduino.

You can find it at

I modified this library as follows.

  • Changed the structure "struct tm" to hold the time, so that you can set and get the time.
  • Added the ability to set CLKOUT.
  • Added the ability to display the contents of all registers.

Here is the test program.

#include <Wire.h>
#include "PCF2129AT.h"

PCF2129AT rtc;

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  rtc.init();
  Serial.println("PC2129AT Sample Program");

  rtc.readAllRegister();

  //Time Setting
  struct tm dt;
  dt.tm_year = 2021-1900; // year(from 1900years)
  dt.tm_mon = 5-1;        // month(0-11)
  dt.tm_mday = 12;        // day(1-)
  dt.tm_hour = 18;        // hours
  dt.tm_min = 46;         // minits
  dt.tm_sec = 0;          // seconds
  rtc.setDate(&dt);       // Set time to PCF2129

  //Alarm Setting
  dt.tm_sec = 0;          // seconds
  rtc.enableAlarmSecond(); // match second alarm
  rtc.setAlarm(&dt);       // Set alarm to PCF2129

  rtc.clearAF();          // clear all alarm flags
  rtc.enableInterruptAlarm(); // out alarm interrupt to INT pin 
  rtc.setInterruptsPIN(); // set interrupt

  rtc.setClockOut( ClkoutFreq_1Hz );  //CLKOUT 1Hz

  rtc.readAllRegister();
}

void loop ()
{
  struct tm dt; 
  rtc.getDate(&dt);          // get date from PCF2129
  Serial.print("year :");
  Serial.print(1900 + dt.tm_year, DEC);
  Serial.print("\t month :");
  Serial.print(1 + dt.tm_mon, DEC);
  Serial.print("\t date :");
  Serial.print(dt.tm_mday, DEC);
  Serial.print("\t hour :");
  Serial.print(dt.tm_hour, DEC);
  Serial.print("\t minutes :");
  Serial.print(dt.tm_min, DEC);
  Serial.print("\t second :");
  Serial.println(dt.tm_sec, DEC);

  //clear alarm flag at 10 sec
  if( dt.tm_sec == 10 )
  {
    rtc.clearAF();
  }
  delay(1000);
}

This program sets...

  • Set the time on the PCF2129
  • Set the alarm interrupt to occur at 0 seconds per minute
  • Set the CLKOUT to output 1Hz
  • Displays the time in the PCF2129 on the serial monitor every 1 second
  • Disable the alarm interrupt at 10 seconds per minute (but not the alarm)

This is how the program is executed.

The entire program can be downloaded from here.

Execution

When the program is executed, an alarm interrupt will be generated at 0 seconds per minute, and the /INT pin will go low, causing the /INT LED to turn off. After 10 seconds, the /INT pin goes into high impedance and the LED turns on again.

The LED connected to the CLKOUT pin will also blink at 1Hz.

The serial monitor will display the time in PCF2129 every second.

Finished!

I can now control the PCF2129 from the ESP32 to set and get the time, set alarms, and get a 1Hz clock.

I would like to use this to turn the VFD display tube IV-27M into a clock with a calendar, see here for more information on controlling the IV-27M.

https://s.click.aliexpress.com/e/_AZwE7R

Copied title and URL