APIs and example programs related to the RTC of CoreP4X.
3.3.10-aM5CoreP4X0.3.10.3.13V3_L0 rail. As long as the battery is not depleted, the RTC keeps counting even after the controller is powered off.#include <M5Unified.h>
M5Canvas canvas(&M5.Display);
static const char* WEEK_DAYS[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
};
static void drawTime()
{
if (!M5.Rtc.isEnabled()) return;
const auto dt = M5.Rtc.getDateTime();
char dateLine[40];
char timeLine[24];
snprintf(dateLine, sizeof(dateLine), "%04d/%02d/%02d %s",
dt.date.year, dt.date.month, dt.date.date,
WEEK_DAYS[(dt.date.weekDay + 7) % 7]);
snprintf(timeLine, sizeof(timeLine), "%02d:%02d:%02d",
dt.time.hours, dt.time.minutes, dt.time.seconds);
canvas.fillSprite(TFT_BLACK);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_WHITE, TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold18pt7b);
canvas.drawString("RX8130CE RTC", 240, 64);
canvas.setFont(&fonts::FreeSans18pt7b);
canvas.drawString(dateLine, 240, 188);
canvas.setFont(&fonts::FreeSansBold24pt7b);
canvas.setTextSize(1.2f);
canvas.drawString(timeLine, 240, 282);
canvas.setTextSize(1.0f);
canvas.setFont(&fonts::FreeSans9pt7b);
canvas.setTextColor(TFT_DARKGREY, TFT_BLACK);
canvas.drawString("Tap screen to set demo time", 240, 420);
canvas.pushSprite(0, 0);
}
static void setDemoTime()
{
M5.Rtc.setDateTime({{2026, 11, 4}, {12, 34, 56}});
Serial.println("RTC time set to 2026/11/04 12:34:56");
}
void setup()
{
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
M5.begin(cfg);
M5.Display.setRotation(0);
for (int i = 0; i < 5 && !M5.Rtc.isEnabled(); ++i) {
delay(200);
M5.Rtc.begin(&M5.In_I2C, M5.getBoard());
}
M5.Display.setBrightness(128);
canvas.createSprite(M5.Display.width(), M5.Display.height());
if (!M5.Rtc.isEnabled()) {
canvas.fillSprite(TFT_BLACK);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_RED, TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold18pt7b);
canvas.drawString("RTC not found", 240, 240);
canvas.pushSprite(0, 0);
Serial.println("RTC not found");
return;
}
if (M5.Rtc.getDateTime().date.year < 2025) setDemoTime();
drawTime();
}
void loop()
{
M5.update();
if (M5.Touch.getDetail().wasClicked()) setDemoTime();
static uint32_t lastRefresh = 0;
const uint32_t now = millis();
if (now - lastRefresh >= 500) {
lastRefresh = now;
drawTime();
}
delay(20);
}After uploading, the screen displays the RTC date, weekday, and time; tap the screen to write the example time 2026/11/04 12:34:56.
After installing the Stamp-AddOn C6 For P4 Wi-Fi module, you can obtain the network time via SNTP and write it to the RX8130CE.
WIFI_SSID and WIFI_PASSWORD with the actual Wi-Fi name and password.#define WIFI_SSID "your-ssid"
#define WIFI_PASSWORD "your-password"
#define NTP_TIMEZONE "CST-8"
#define NTP_SERVER1 "0.pool.ntp.org"
#define NTP_SERVER2 "1.pool.ntp.org"
#define NTP_SERVER3 "2.pool.ntp.org"
#include <WiFi.h>
#include <M5Unified.h>
#if __has_include(<esp_sntp.h>)
#include <esp_sntp.h>
#define SNTP_ENABLED 1
#elif __has_include(<sntp.h>)
#include <sntp.h>
#define SNTP_ENABLED 1
#endif
#ifndef SNTP_ENABLED
#define SNTP_ENABLED 0
#endif
M5Canvas canvas(&M5.Display);
static const char* WEEK_DAYS[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
};
static void drawStatus(const char* title, const char* detail)
{
canvas.fillSprite(TFT_BLACK);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_WHITE, TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold18pt7b);
canvas.drawString(title, 240, 220);
canvas.setFont(&fonts::FreeSans12pt7b);
canvas.drawString(detail, 240, 276);
canvas.pushSprite(0, 0);
}
void setup()
{
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
M5.begin(cfg);
M5.Display.setRotation(0);
for (int i = 0; i < 5 && !M5.Rtc.isEnabled(); ++i) {
delay(200);
M5.Rtc.begin(&M5.In_I2C, M5.getBoard());
}
M5.Display.setBrightness(128);
canvas.createSprite(M5.Display.width(), M5.Display.height());
if (!M5.Rtc.isEnabled()) {
drawStatus("RTC not found", "Check the internal I2C bus");
return;
}
drawStatus("WiFi connecting...", WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print('.');
}
drawStatus("NTP syncing...", NTP_SERVER1);
configTzTime(NTP_TIMEZONE, NTP_SERVER1, NTP_SERVER2, NTP_SERVER3);
#if SNTP_ENABLED
while (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) delay(500);
#else
struct tm timeInfo;
while (!getLocalTime(&timeInfo, 1000)) delay(500);
#endif
time_t t = time(nullptr) + 1;
while (t > time(nullptr)) delay(10);
M5.Rtc.setDateTime(localtime(&t));
Serial.println("RTC synchronized");
}
void loop()
{
if (!M5.Rtc.isEnabled()) {
delay(1000);
return;
}
const auto dt = M5.Rtc.getDateTime();
char dateLine[40];
char timeLine[24];
snprintf(dateLine, sizeof(dateLine), "%04d/%02d/%02d %s",
dt.date.year, dt.date.month, dt.date.date,
WEEK_DAYS[(dt.date.weekDay + 7) % 7]);
snprintf(timeLine, sizeof(timeLine), "%02d:%02d:%02d",
dt.time.hours, dt.time.minutes, dt.time.seconds);
canvas.fillSprite(TFT_BLACK);
canvas.setTextDatum(middle_center);
canvas.setTextColor(TFT_WHITE, TFT_BLACK);
canvas.setFont(&fonts::FreeSansBold18pt7b);
canvas.drawString("Network synchronized", 240, 72);
canvas.setFont(&fonts::FreeSans18pt7b);
canvas.drawString(dateLine, 240, 204);
canvas.setFont(&fonts::FreeSansBold24pt7b);
canvas.setTextSize(1.2f);
canvas.drawString(timeLine, 240, 300);
canvas.setTextSize(1.0f);
canvas.pushSprite(0, 0);
delay(500);
}After uploading, the program connects to Wi-Fi and obtains the network time via SNTP; after synchronization, it writes the time to the RX8130CE. The screen displays the calibrated date and time, and the serial port outputs RTC synchronized.
The RTC section of CoreP4X uses the RTC_Class in the M5Unified library. For more related APIs, refer to the documentation below: