rotaryio – Support for reading rotation sensors — Adafruit CircuitPython 1 documentation (original) (raw)

The rotaryio module contains classes to read different rotation encoding schemes. SeeWikipedia’s Rotary Encoder page for more background.

For more information on working with rotary encoders using this library, seethis Learn Guide.

All classes change hardware state and should be deinitialized when they are no longer needed if the program continues after use. To do so, either call deinit() or use a context manager. SeeLifetime and ContextManagers for more info.

Available on these boards

class rotaryio.IncrementalEncoder(pin_a: microcontroller.Pin, pin_b: microcontroller.Pin, divisor: int = 4)

IncrementalEncoder determines the relative rotational position based on two series of pulses. It assumes that the encoder’s common pin(s) are connected to ground,and enables pull-ups on pin_a and 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 constructed.

Parameters:

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

Warning

On RP2350 boards, any pulldowns used must be 8.2 kohms or less, to overcome a hardware issue. See the RP2350 warning in digitalio for more information.

deinit() → None

Deinitializes the IncrementalEncoder and releases any hardware resources for reuse.

__enter__() → IncrementalEncoder

No-op used by Context Managers.

__exit__() → None

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

divisor_: int_

The divisor of the quadrature signal. Use 1 for encoders without detents, or encoders with 4 detents per cycle. Use 2 for encoders with 2 detents per cycle. Use 4 for encoders with 1 detent per cycle.

position_: int_

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