> ## Documentation Index
> Fetch the complete documentation index at: https://learn.pcbcupid.com/llms.txt
> Use this file to discover all available pages before exploring further.

> MCP79412 RTC module with I²C, battery backup, alarms, and memory. Ideal for accurate timekeeping in Arduino, Raspberry Pi, and IoT projects.

# MCP79412 RTC

export const ShopHeader = ({title, url}) => <div style={{
  display: 'flex',
  justifyContent: 'flex-end',
  alignItems: 'center',
  width: '100%',
  marginBottom: '1rem',
  gap: '20px'
}}>
    <a href={url} target="_blank" rel="noopener noreferrer" style={{
  display: 'inline-flex',
  alignItems: 'center',
  backgroundColor: '#00b894',
  color: '#fff',
  padding: '8px 18px',
  borderRadius: '8px',
  textDecoration: 'none',
  fontSize: '0.95rem',
  fontWeight: 600,
  whiteSpace: 'nowrap',
  flexShrink: 0
}}>
      Shop now
    </a>
  </div>;

export const ImgGallery = ({images, modelUrl}) => {
  var [active, setActive] = useState(0);
  var [show3D, setShow3D] = useState(false);
  useEffect(function () {
    if (show3D || images.length <= 1) return;
    var t = setTimeout(function () {
      setActive(function (p) {
        return (p + 1) % images.length;
      });
    }, 3500);
    return function () {
      clearTimeout(t);
    };
  });
  var img = images[active];
  return <div style={{
    position: 'relative',
    background: 'linear-gradient(to bottom, #fff 0%, #fff 80%, #f8f8f8 100%)',
    borderRadius: '32px',
    border: '1px solid rgba(0,0,0,0.08)',
    overflow: 'hidden',
    marginTop: '0.75rem',
    marginBottom: '1.5rem'
  }}>
      <div style={{
    position: 'relative',
    height: '450px',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    background: '#fff',
    padding: '1rem'
  }}>
        {show3D && modelUrl ? <div ref={function (el) {
    if (!el || el._mv) return;
    el._mv = true;
    var s = document.createElement('script');
    s.src = "https://ajax.googleapis.com/ajax/libs/model-viewer/4.2.0/model-viewer.min.js";
    s.type = "module";
    s.onload = function () {
      var mv = document.createElement('model-viewer');
      mv.setAttribute('src', modelUrl);
      mv.setAttribute('camera-controls', '');
      mv.setAttribute('auto-rotate', '');
      mv.style.width = "100%";
      mv.style.height = "100%";
      mv.style.backgroundColor = "#f8f8f8";
      el.innerHTML = "";
      el.appendChild(mv);
    };
    document.head.appendChild(s);
  }} style={{
    width: "100%",
    height: "100%"
  }} /> : <img src={img && img.url} alt={img && img.label} loading="lazy" style={{
    maxWidth: '100%',
    maxHeight: '100%',
    objectFit: 'contain',
    borderRadius: '8px'
  }} />}
      </div>
      <div style={{
    display: 'flex',
    justifyContent: 'center',
    gap: '12px',
    padding: '1rem',
    background: '#eee',
    borderTop: '1px solid rgba(0,0,0,0.08)'
  }}>
        {images.map(function (img, idx) {
    return <button key={img.id || idx} onClick={function () {
      setActive(idx);
      setShow3D(false);
    }} style={{
      padding: '0.6rem 1.5rem',
      borderRadius: '12px',
      border: !show3D && idx === active ? '1px solid #00b894' : '1px solid rgba(0,184,148,0.4)',
      background: !show3D && idx === active ? '#00b894' : '#fff',
      color: !show3D && idx === active ? '#fff' : '#000',
      cursor: 'pointer',
      fontSize: '0.75rem',
      fontWeight: 700,
      textTransform: 'uppercase',
      fontFamily: 'inherit'
    }}>
              {img.label}
            </button>;
  })}
        {modelUrl ? <button onClick={function () {
    setShow3D(true);
  }} style={{
    padding: '0.6rem 1.5rem',
    borderRadius: '12px',
    border: show3D ? '1px solid #00b894' : '1px solid rgba(0,184,148,0.4)',
    background: show3D ? '#00b894' : '#fff',
    color: show3D ? '#fff' : '#000',
    cursor: 'pointer',
    fontSize: '0.75rem',
    fontWeight: 700,
    textTransform: 'uppercase',
    fontFamily: 'inherit'
  }}>
            3D VIEW
          </button> : null}
      </div>
    </div>;
};

<ShopHeader title="MCP79412 RTC Module" url="https://shop.pcbcupid.com/product/gm008/" />

<ImgGallery images={[{url: "https://files.pcbcupid.com/Documentation/Boards/g-mod/MCP/mcp1.avif", label: "ILLUSTRATION VIEW", id: "illustration"}, {url: "https://files.pcbcupid.com/Documentation/Boards/g-mod/MCP/pcbcupid-rtc-mcp-sideview.avif", label: "BOARD VIEW", id: "board"}]} />

The **MCP79412** from Microchip Technology is a low-power, I²C-based real-time clock/calendar (RTCC) with integrated EEPROM, SRAM, and a unique ID. It provides highly accurate timekeeping (seconds, minutes, hours, day, date, month, year) and includes alarm functions, power-fail timestamping, and battery backup support, making it ideal for applications that require reliable time and date tracking.<br />

<Info>
  Check the back of the RTC module: modules marked **“M”** use the ***MCP79412***, while those marked **“P”** use the ***PCF8563***.
</Info>

## Pin Configuration

* VCC → VCC
* GND → GND
* CLK → SCK / Any GPIO
* SDA → SDA
* SCL → SCL
* B → No connection / Internally connected to the 3V battery

<Info>
  Install a 3V Lithium Cell (CR1220) on the RTC module to retain time when the main circuit is powered off. The module can also operate without a battery if the circuit remains powered.
</Info>

## Key Features

* I²C Interface (400 kHz Fast Mode)
* Timekeeping: seconds, minutes, hours, day, date, month, year with leap year compensation
* Two Alarms with programmable match options
* Battery Backup with automatic switchover
* Power-Fail Timestamp (records time of power loss)
* 64 Bytes SRAM + 1 Kbit EEPROM (non-volatile storage)
* Unique 64-bit ID (factory-programmed)
* Digital Trimming for oscillator accuracy adjustment
* Operating Voltage: 1.8V – 5.5V
* Temperature Range: −40 °C to +85 °C (industrial)

## Applications

* Data loggers and metering systems
* Security and access control devices
* Consumer electronics (appliances, clocks, timers)
* Automotive time-stamping and scheduling
* Embedded systems requiring alarms, scheduling, or backup timekeeping

## Step 1: Hardware Required

1. Glyph Boards
2. GMOD MCP79412 RTC

## Step 2: Circuit Diagram

![MCP79412-CIRCUIT-DIAGRAM](https://files.pcbcupid.com/Documentation/Boards/g-mod/MCP/pcbcupid_mcp.avif)

## Step 3: Code Setup

1. **Open Arduino IDE.**
2. **Make sure to install the  library**
3. **Copy and paste the following code into the Arduino IDE:**

```cpp theme={null}
// You can downlaod PCBCUPID_MCP79412 from here https://github.com/pcbcupid/PCBCUPID-MCP79412 
#include <Wire.h> 
#include "PCBCUPID_MCP79412.h"
#include <TimeLib.h>

PCBCUPID_MCP79412RTC rtc;
tmElements_t tm;

// Helper to read an integer cleanly from Serial, flushing leftover chars
int readIntFromSerial() {
  while (!Serial.available()) {
    delay(10);
  }
  int val = Serial.parseInt();
  while (Serial.available()) Serial.read(); // flush
  return val;
}

void setup()
{
    Serial.begin(115200);
    delay(1000);
    Serial.println(F("\r\n=======PCBCUPID_MCP79412 RTC Test Menu ==========\r\n"));
    Serial.println(F("0: Wire.begin() default pins\r\n1: Wire.begin(sda,scl,clockFreq)\r\n2: Wire.begin(TwoWire*)"));
    Serial.println(F("Type 0-2 to select I2C init mode....."));

    uint8_t mode = 
    readIntFromSerial();

    // These are different way's to initialize the I2C of RTC module

    switch (mode)
    {
    case 0:
        rtc.begin();
        Serial.println(F(">>>Wire.begin() with default pins\r\n"));
        break;
    case 1:
        rtc.begin(4, 5, 400000); // ESP32 based glyph default SDA/SCL
        Serial.println(F(">>>Wire.begin(4,5,400kHz)\r\n"));
        break;
    case 2:
        rtc.begin(&Wire);
        Serial.println(F(">>>Wire.begin() using custom Twowire instance\r\n"));
        break;
    default:
        Serial.println(F("Invalid Input, using default Wire.begin()\r\n"));
        Wire.begin();
    }

    Serial.println(F("**********RTC Function Modes*************"));
    Serial.println(F("3. Set RTC using setTime() + now()"));
    Serial.println(F("4. Set RTC using tmElements_t"));
    Serial.println(F("5: Set RTC using compile time"));
    Serial.println(F("Type 3-5 to select RTC mode..."));

    mode = readIntFromSerial();

    // below are the different mode to setup the time for the RTC module

    switch (mode)
    {
    case 3:
        setTime(3, 43, 45, 20, 5, 2025); // HH, MM, SS, DD, MM, YYYY
        rtc.set(now());
        Serial.println(F(">> RTC set using setTime() + now()\r\n"));
        break;

    case 4:
        tm.Hour = 23;
        tm.Minute = 31;
        tm.Second = 30;
        tm.Year = 2009 - 1970;
        tm.Month = 5;
        tm.Day = 20;
        tm.Wday = dowTuesday;
        rtc.write(tm);
        Serial.println(F(">> RTC set using tmElements_t structure\r\n"));
        break;

    case 5:
        setTime(compileTime());
        rtc.set(now());
        Serial.println(F(">> RTC set using sketch compile time\r\n"));
        break;

    default:
        Serial.println(F("Invalid RTC mode, terminating.\r\n"));
        while (true)
            ;
    }
    delay(500);
}

void loop()
{
    Serial.println(F("\r\n--- Current Time ---"));
    printTime(now());

    if (rtc.read(tm))
    {
        Serial.print(F("RTC tmElements_t: "));
        Serial.print(tm.Hour); Serial.print(':');
        printI00(tm.Minute, ':');
        printI00(tm.Second, ' ');
        Serial.print(tm.Year + 1970);
        Serial.print('/');
        Serial.print(tm.Month);
        Serial.print('/');
        Serial.println(tm.Day);
    }
    else
    {
        Serial.println(F("Failed to read RTC time using .read()"));
    }
    delay(3000);
}

void printTime(time_t t)
{
    printI00(hour(t), ':');
    printI00(minute(t), ':');
    printI00(second(t), ' ');
    Serial.print(dayShortStr(weekday(t)));
    Serial.print(' ');
    printI00(day(t), ' ');
    Serial.print(monthShortStr(month(t)));
    Serial.print(' ');
    Serial.println(year(t));
}

void printI00(int value, char delimiter)
{
    if (value < 10)
        Serial.print('0');
    Serial.print(value);
    Serial.print(delimiter);
}

time_t compileTime()
{
    const char *months = "JanFebMarAprMayJunJulAugSepOctNovDec";
    char monthstr[4];
    tmElements_t tm;
    strncpy(monthstr, __DATE__, 3);
    monthstr[3] = '\0';

    tm.Month = (strstr(months, monthstr) - months) / 3 + 1;
    tm.Day = atoi(__DATE__ + 4);
    tm.Year = atoi(__DATE__ + 7) - 1970;

    char buf[3];

    memcpy(buf, __TIME__, 2);
    buf[2] = '\0';
    tm.Hour = atoi(buf);

    memcpy(buf, __TIME__ + 3, 2);
    buf[2] = '\0';
    tm.Minute = atoi(buf);

    memcpy(buf, __TIME__ + 6, 2);
    buf[2] = '\0';
    tm.Second = atoi(buf);

    return makeTime(tm) + 15; // fudge factor
}
```

## Step 4: Upload the Code

1. **Connect the Board**

* Connect your GLYPH board to your computer

2. **Select the Board and Port**

Do the following settings in your Arduino IDE,

* `Tools > Board > esp32 > Pcbcupid GLYPH C3`

<Warning>
  For the `Pcbcupid Glyph C3 ` to appear under `Tools > Board > esp32`, the esp32 board version installed in the Arduino IDE should be greater or equal to 3.1.0.
</Warning>

* `Tools > Port` and select the port connected to your GLYPH.
* `Tools > USB CDC on Boot >` ***Enabled***

<Warning>
  If `USB CDC on BOOT` not enabled, you won't be seeing any serial data on Arduino IDE.
</Warning>

3. **Upload the Code**

   * Click the upload button (➡️ icon) or use the shortcut `CRTL + U` in Arduino IDE to upload the code to the board.

## Step 5: Observe the Output

The RTC module tracks time independently. With the onboard 3V Lithium Cell (CR1220), it continues timekeeping even when the GLYPH is powered off.

![MCP79412-result](https://files.pcbcupid.com/Documentation/Boards/g-mod/MCP/pcbcupid_rtc79412_result.avif)
