This is the documentation for the latest development branch of MicroPython and may refer to features that are not available in released versions.

If you are looking for the documentation for a specific release, use the drop-down menu on the left and select the desired version.

class RTC – real time clock

The RTC is an independent clock that keeps track of the dateand time.

Example usage:

rtc=machine.RTC()rtc.datetime((2020,1,21,2,10,32,36,0))print(rtc.datetime())

Constructors

classmachine.RTC(id=0,...)

Create an RTC object. See init for parameters of initialization.

Methods

RTC.datetime([datetimetuple])

Get or set the date and time of the RTC.

With no arguments, this method returns an 8-tuple with the currentdate and time. With 1 argument (being an 8-tuple) it sets the dateand time.

The 8-tuple has the following format:

(year, month, day, weekday, hours, minutes, seconds, subseconds)

The meaning of thesubseconds field is hardware dependent.

RTC.init(datetime)

Initialise the RTC. Datetime is a tuple of the form:

(year,month,day,hour,minute,second,microsecond,tzinfo)

All eight arguments must be present. Themicrosecond andtzinfovalues are currently ignored but might be used in the future.

Availability: CC3200, ESP32, MIMXRT, SAMD. The rtc.init() method onthe stm32 and renesas-ra ports just (re-)starts the RTC and does notaccept arguments.

RTC.now()

Get get the current datetime tuple.

Availability: WiPy.

RTC.deinit()

Resets the RTC to the time of January 1, 2015 and starts running it again.

RTC.alarm(id,time,*,repeat=False)

Set the RTC alarm. Time might be either a millisecond value to program the alarm tocurrent time + time_in_ms in the future, or a datetimetuple. If the time passed is inmilliseconds, repeat can be set toTrue to make the alarm periodic.

RTC.alarm_left(alarm_id=0)

Get the number of milliseconds left before the alarm expires.

RTC.alarm_cancel(alarm_id=0)

Cancel a running alarm.

The mimxrt port also exposes this function asRTC.cancel(alarm_id=0), but this isscheduled to be removed in MicroPython 2.0.

RTC.irq(*,trigger,handler=None,wake=machine.IDLE)

Create an irq object triggered by a real time clock alarm.

  • trigger must beRTC.ALARM0

  • handler is the function to be called when the callback is triggered.

  • wake specifies the sleep mode from where this interrupt can wakeup the system.

RTC.memory([data])

RTC.memory(data) will writedata to the RTC memory, wheredata is anyobject which supports the buffer protocol (includingbytes,bytearray,memoryview andarray.array).RTC.memory() reads RTC memory and returnsabytes object.

Data written to RTC user memory is persistent across restarts, includingSoft Reset andmachine.deepsleep().

The maximum length of RTC user memory is 2048 bytes by default on esp32,and 492 bytes on esp8266.

Availability: esp32, esp8266 ports.

Constants

RTC.ALARM0

irq trigger source