utime – time related functions

The utime module provides functions for getting the current time and date, measuring time intervals, and for delays. utime supports a subset of the functionality of the CPython time module.

Time Epoch: uses standard for POSIX systems epoch of 1970-01-01 00:00:00 UTC.



Convert a time expressed in seconds since 1970 (see above) into a 9-tuple which contains: (year, month, mday, hour, minute, second, weekday, yearday, dst) If secs is not provided or None, then the current time from the RTC is used.

  • year includes the century (for example 2014).
  • month is 1-12
  • mday is 1-31
  • hour is 0-23
  • minute is 0-59
  • second is 0-59
  • weekday is 0-6 for Mon-Sun
  • yearday is 1-366
  • isdst is 0 (for compatibility)

This is the inverse function of localtime. It’s argument is a 9-tuple which expresses a time as per localtime. It returns a floating point number which is the number of seconds since Jan 1, 1970.


Sleep for the given number of seconds. Seconds is a floating-point number with millisecond resolution.


Delay for given number of milliseconds, should be positive or 0.


Delay for given number of microseconds, should be positive or 0


Returns an increasing millisecond counter with arbitrary reference point, that wraps after some (unspecified) value. The value should be treated as opaque, suitable for use only with ticks_diff().


Just like ticks_ms above, but in microseconds.


Similar to ticks_ms and ticks_us, but with higher resolution (usually CPU clocks).

utime.ticks_add(ticks, delta)

Offset ticks value by a given number, which can be either positive or negative. Given a ticks value, this function allows to calculate ticks value delta ticks before or after it, following modular-arithmetic definition of tick values (see ticks_ms() above). ticks parameter must be a direct result of call to ticks_ms(), ticks_us(), or ticks_cpu() functions (or from previous call to ticks_add()). However, delta can be an arbitrary integer number or numeric expression. ticks_add() is useful for calculating deadlines for events/tasks. (Note: you must use ticks_diff() function to work with deadlines.)


# Find out what ticks value there was 100ms ago
print(ticks_add(time.ticks_ms(), -100))

# Calculate deadline for operation and test for it
deadline = ticks_add(time.ticks_ms(), 200)
while ticks_diff(deadline, time.ticks_ms()) > 0:

# Find out maximum ticks value)
print(ticks_add(0, -1))
utime.ticks_diff(old, new)

Measure period between consecutive calls to ticks_ms(), ticks_us(), or ticks_cpu(). The value returned by these functions may wrap around at any time, so directly subtracting them is not supported. ticks_diff() should be used instead. “old” value should actually precede “new” value in time, or result is undefined. This function should not be used to measure arbitrarily long periods of time (because ticks_*() functions wrap around and usually would have short period). The expected usage pattern is implementing event polling with timeout:

# Wait at most 500us for the battery voltage to climb to 12.5V
start = time.ticks_us()
while batt() < 12.5:
    if time.ticks_diff(start, time.ticks_us()) > 500:
        raise TimeoutError

Returns the number of seconds (as a floating point number) since Jan 1, 1970 with millisecond resolution.


Returns the number of milliseconds which have elapsed since start.

This function takes care of counter wrap, and always returns a positive number. This means it can be used to measure periods up to about 12.4 days.


start = utime.ticks_ms()
while utime.elapsed_millis(start) < 1000:
    # Perform some operation

Returns the number of microseconds which have elapsed since start.

This function takes care of counter wrap, and always returns a positive number. This means it can be used to measure periods up to about 17.8 minutes.


start = utime.ticks_us()
while utime.elapsed_micros(start) < 1000:
    # Perform some operation