class RTC – real time clock
The RTC is an independent clock that can track the date and time.
Example:
rtc = machine.RTC()
rtc.datetime((2020, 1, 21, 2, 10, 32, 36, 0))
print(rtc.datetime())
Constructors
- class machine.RTC(id=0, ...)
Create an RTC object. See the init method for the initialization parameters.
Methods
- RTC.datetime([datetimetuple])
Get or set the date and time of the RTC. With no arguments, this method returns an 8-tuple containing the current date and time. With 1 argument (an 8-tuple), it sets the date and time.
The format of the 8-tuple is as follows:
(year, month, day, weekday, hours, minutes, seconds, subseconds)
The meaning of the subseconds field depends on the hardware.
- RTC.init(datetime)
Initialize the RTC. datetime is a tuple of the following form:
(year, month, day, hour, minute, second, microsecond, tzinfo)
All eight arguments must be provided. The microsecond and tzinfo values are currently ignored but may be used in the future.
Availability: CC3200, ESP32, MIMXRT, SAMD. On the stm32 and renesas-ra ports, the rtc.init() method simply (re)starts the RTC and does not accept arguments.
- RTC.now()
Get the current date and time tuple.
Availability: WiPy.
- RTC.deinit()
Reset the RTC to the time of January 1, 2015, and start running again.
- RTC.alarm(id, time, *, repeat=False)
Set the RTC alarm. time can be a millisecond value, used to set the alarm to the current time + time_in_ms in the future, or it can be a date-time tuple. If the time passed is in milliseconds, repeat can be set to True to make the alarm trigger periodically.
- RTC.alarm_left(alarm_id=0)
Get the number of milliseconds before the alarm expires.
- RTC.alarm_cancel(alarm_id=0)
Cancel the running alarm.
The mimxrt port also exposes this function as RTC.cancel(alarm_id=0), but it is planned to be removed in MicroPython 2.0.
- RTC.irq(*, trigger, handler=None, wake=machine.IDLE)
Create an irq object triggered by the real time clock alarm.
trigger must be RTC.ALARM0
handler is the function to call when the callback is triggered.
wake specifies which sleep mode the system can be woken from by this interrupt.
- RTC.memory([data])
RTC.memory(data) writes data to the RTC memory, where data is any object that supports the buffer protocol (including bytes, bytearray, memoryview and array.array). RTC.memory() reads the RTC memory and returns a bytes object.
Data written to the RTC user memory is retained after a restart, including soft resets and machine.deepsleep().
On the ESP32, the maximum length of the RTC user memory is 2048 bytes by default, and 492 bytes on esp8266.
Availability: esp32, esp8266 ports.
Constants
- RTC.ALARM0
irq trigger source
Note:
Although the RTC can track the time and date, its precision has certain flaws: a second-level error overflows every 7:45h, so it is recommended to calibrate the time every 7 hours.
Since the timer cannot keep time after power off, your device will revert to the initial time of January 1, 2000 before the next power-on. Therefore, to have precise control of the time, we need to calibrate the time at boot.
You can use the
ntptimemodule for network time calibration.