IncrementalEncoder – Track the relative position of an incremental encoder

IncrementalEncoder determines the relative rotational position based on two series of pulses.

class rotaryio.IncrementalEncoder(pin_a, pin_b)

Create an IncrementalEncoder object associated with the given pins. It tracks the positional state of an incremental rotary encoder (also known as a quadrature encoder.) Position is relative to the position when the object is contructed.

Parameters:
  • pin_a (Pin) – First pin to read pulses from.
  • pin_b (Pin) – Second pin to read pulses from.

For example:

import rotaryio
import time
from board import *

enc = rotaryio.IncrementalEncoder(D1, D2)
last_position = None
while True:
    position = enc.position
    if last_position == None or position != last_position:
        print(position)
    last_position = position
deinit()

Deinitializes the IncrementalEncoder and releases any hardware resources for reuse.

__enter__()

No-op used by Context Managers.

__exit__()

Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.

position

The current position in terms of pulses. The number of pulses per rotation is defined by the specific hardware.