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.

machine.Timer

Classes

Timer

Construct a new timer object of the given id. id of -1 constructs a

Module Contents

class machine.Timer.Timer(id=-1, *args, **kwargs)

Construct a new timer object of the given id. id of -1 constructs a virtual timer (if supported by a board). id shall not be passed as a keyword argument.

See init for parameters of initialisation.

deinit() None

Deinitialises the timer. Stops the timer, and disables the timer peripheral.

init(*, mode=PERIODIC, freq=-1, period=-1, callback=None) None

Initialise the timer. Example:

def mycallback(t):
    pass

# periodic at 1kHz
tim.init(mode=Timer.PERIODIC, freq=1000, callback=mycallback)

# periodic with 100ms period
tim.init(period=100, callback=mycallback)

# one shot firing after 1000ms
tim.init(mode=Timer.ONE_SHOT, period=1000, callback=mycallback)
Keyword Arguments:
  • of (- freq - The timer frequency, in units of Hz. The upper bound) –

    • Timer.ONE_SHOT - The timer runs once until the configured period of the channel expires.

    • Timer.PERIODIC - The timer runs periodically at the configured frequency of the channel.

  • of – the frequency is dependent on the port. When both the freq and period arguments are given, freq has a higher priority and period is ignored.

  • milliseconds. (- period - The timer period, in)

  • period. (- callback - The callable to call upon expiration of the timer) – The callback must take one argument, which is passed the Timer object. The callback argument shall be specified. Otherwise an exception will occur upon timer expiration: TypeError: 'NoneType' object isn't callable

ONE_SHOT: Incomplete

Timer operating mode.

PERIODIC: Incomplete

Timer operating mode.