RTC Unit

Support the following products:

RTCUnit

UiFlow2 Example

get real time

Open the rtc_core2_example.m5f2 project in UiFlow2.

This example displays the Real time on the screen and serial.

UiFlow2 Code Block:

example.png

Example output:

None

MicroPython Example

get real time

This example displays the Real time on the screen and serial.

MicroPython Code Block:

  1# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
  2#
  3# SPDX-License-Identifier: MIT
  4
  5import os, sys, io
  6import M5
  7from M5 import *
  8import m5ui
  9import lvgl as lv
 10from hardware import Pin
 11from hardware import I2C
 12from unit import RTC8563Unit
 13import time
 14
 15
 16page0 = None
 17label0 = None
 18i2c0 = None
 19rtc_0 = None
 20
 21
 22str2 = None
 23
 24
 25def setup():
 26    global page0, label0, i2c0, rtc_0, str2
 27
 28    M5.begin()
 29    Widgets.setRotation(1)
 30    m5ui.init()
 31    page0 = m5ui.M5Page(bg_c=0xFFFFFF)
 32    label0 = m5ui.M5Label(
 33        "label0",
 34        x=3,
 35        y=99,
 36        text_c=0x000000,
 37        bg_c=0xFFFFFF,
 38        bg_opa=0,
 39        font=lv.font_montserrat_14,
 40        parent=page0,
 41    )
 42
 43    i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
 44    rtc_0 = RTC8563Unit(i2c0)
 45    page0.screen_load()
 46    rtc_0.set_date_time(3, 49, 15, 0, 2, 2, 26)
 47    str2 = ""
 48
 49
 50def loop():
 51    global page0, label0, i2c0, rtc_0, str2
 52    M5.update()
 53    str2 = str("Time: ") + str(
 54        (
 55            str((rtc_0.get_date_time(6)))
 56            + str(
 57                (
 58                    str(".")
 59                    + str(
 60                        (
 61                            str((rtc_0.get_date_time(5)))
 62                            + str(
 63                                (
 64                                    str(".")
 65                                    + str(
 66                                        (
 67                                            str((rtc_0.get_date_time(4)))
 68                                            + str(
 69                                                (
 70                                                    str(" ")
 71                                                    + str(
 72                                                        (
 73                                                            str((rtc_0.get_date_time(2)))
 74                                                            + str(
 75                                                                (
 76                                                                    str(":")
 77                                                                    + str(
 78                                                                        (
 79                                                                            str(
 80                                                                                (
 81                                                                                    rtc_0.get_date_time(
 82                                                                                        1
 83                                                                                    )
 84                                                                                )
 85                                                                            )
 86                                                                            + str(
 87                                                                                (
 88                                                                                    str(":")
 89                                                                                    + str(
 90                                                                                        (
 91                                                                                            str(
 92                                                                                                (
 93                                                                                                    rtc_0.get_date_time(
 94                                                                                                        0
 95                                                                                                    )
 96                                                                                                )
 97                                                                                            )
 98                                                                                            + str(
 99                                                                                                ""
100                                                                                            )
101                                                                                        )
102                                                                                    )
103                                                                                )
104                                                                            )
105                                                                        )
106                                                                    )
107                                                                )
108                                                            )
109                                                        )
110                                                    )
111                                                )
112                                            )
113                                        )
114                                    )
115                                )
116                            )
117                        )
118                    )
119                )
120            )
121        )
122    )
123    print(str2)
124    label0.set_text(str(str2))
125    time.sleep(1)
126
127
128if __name__ == "__main__":
129    try:
130        setup()
131        while True:
132            loop()
133    except (Exception, KeyboardInterrupt) as e:
134        try:
135            m5ui.deinit()
136            from utility import print_error_msg
137
138            print_error_msg(e)
139        except ImportError:
140            print("please update to latest firmware")

Example output:

None

API

Class RTC8563Unit

class unit.rtc8563.RTC8563Unit(i2c, address=micropython.const)

Bases: object

Create an RTC8563Unit object.

Parameters:
  • i2c (I2C) – The I2C port used for communication.

  • address (int) – The I2C address of the RTC8563/PCF8563.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from machine import I2C, Pin
from unit import RTC8563Unit

i2c0 = I2C(0, scl=Pin(26), sda=Pin(32), freq=400000)
rtc_0 = RTC8563Unit(i2c0)
get_date_time(select=0)

Getting specific date or time components.

Parameters:

select (int) – The component to get (SECONDS, MINUTES, HOURS, DAY, DATE, MONTH, YEAR).

Returns:

The value of the selected component.

Return type:

int

UiFlow2 Code Block:

get_date_time.png

MicroPython Code Block:

rtc_0.get_date_time(0) # Get seconds
set_date_time(seconds=None, minutes=None, hours=None, day=None, date=None, month=None, year=None)

Setting the date and time values.

Parameters:
  • seconds (int) – Range [0,59].

  • minutes (int) – Range [0,59].

  • hours (int) – Range [0,23].

  • day (int) – Range [0,6] (0 for Sunday).

  • date (int) – Range [1-31].

  • month (int) – Range [1-12].

  • year (int) – Range [0-99] (Last two digits).

UiFlow2 Code Block:

set_date_time.png

MicroPython Code Block:

rtc_0.set_date_time(hours=12, minutes=30)
datetime(dt)

Setting the complete date and time using a tuple.

Parameters:

dt (tuple) – (year, month, date, hours, minutes, seconds, day).

MicroPython Code Block:

rtc_0.datetime((2024, 5, 20, 10, 0, 0, 1))
write_now()

Writing the current system time (from ESP32) to the RTC.

MicroPython Code Block:

rtc_0.write_now()
set_internet_time(source='ntp', host='cn.pool.ntp.org', tzone=0, win=True)

Synchronizing the RTC with network time.

Parameters:
  • source (str) – Time source (“ntp”).

  • host (str) – NTP server address.

  • tzone (float) – Timezone offset.

  • win (bool) – Whether to consider daylight saving time.

UiFlow2 Code Block:

set_internet_time.png

MicroPython Code Block:

rtc_0.set_internet_time(tzone=8)
set_clk_out_frequency(frequency=micropython.const)

Setting the frequency of the CLKOUT pin.

Parameters:

frequency (int) – Frequency constant (e.g., CLOCK_CLK_OUT_FREQ_1_HZ).

MicroPython Code Block:

rtc_0.set_clk_out_frequency(0x83)
check_if_alarm_on()

Checking if the alarm flag is triggered.

Returns:

True if alarm is triggered, False otherwise.

Return type:

bool

UiFlow2 Code Block:

check_if_alarm_on.png

MicroPython Code Block:

rtc_0.check_if_alarm_on()
turn_off_alarm()

Disabling the alarm function.

UiFlow2 Code Block:

turn_off_alarm.png

MicroPython Code Block:

rtc_0.turn_off_alarm()
clear_alarm_flag()

Clearing the alarm status flag and resetting alarm registers.

UiFlow2 Code Block:

clear_alarm_flag.png

MicroPython Code Block:

rtc_0.clear_alarm_flag()
set_daily_alarm(hours=None, minutes=None, date=None, weekday=None)

Setting a daily or periodic alarm.

Parameters:
  • hours (int) – Alarm hour.

  • minutes (int) – Alarm minute.

  • date (int) – Alarm date.

  • weekday (int) – Alarm weekday.

UiFlow2 Code Block:

set_daily_alarm.png

MicroPython Code Block:

rtc_0.set_daily_alarm(hours=7, minutes=0)
set_timer_mode(mode=0, value=0)

Setting the countdown timer mode and initial value.

Parameters:
  • mode (int) – Timer clock frequency mode.

  • value (int) – Initial countdown value.

UiFlow2 Code Block:

set_timer_mode.png

MicroPython Code Block:

rtc_0.set_timer_mode(mode=2, value=60)
get_timer_value()

Getting the current countdown timer value.

Returns:

Current timer value.

Return type:

int

UiFlow2 Code Block:

get_timer_value.png

MicroPython Code Block:

rtc_0.get_timer_value()
check_if_timer_on()

Checking if the timer flag is triggered.

Returns:

True if timer is triggered, False otherwise.

Return type:

bool

UiFlow2 Code Block:

check_if_timer_on.png

MicroPython Code Block:

rtc_0.check_if_timer_on()
turn_off_timer()

Disabling the timer and clearing the timer flag.

UiFlow2 Code Block:

turn_off_timer.png

MicroPython Code Block:

rtc_0.turn_off_timer()
clear_timer_flag()

Clearing the timer status flag.

UiFlow2 Code Block:

clear_timer_flag.png

MicroPython Code Block:

rtc_0.clear_timer_flag()