import rotary
The rotary module reads common Arduino-style incremental rotary encoder boards, including KY-040 style encoders with CLK, DT, and an optional push switch. The firmware uses GPIO interrupts to track movement, while user code reads position or delta synchronously. Use ``rotary.Encoder(...)`` to create independent encoder objects without manually choosing native channels.
Quick example
import rotary
import system
volume = rotary.Encoder(clk_pin=4, dt_pin=5, sw_pin=6)
menu = rotary.Encoder(clk_pin=18, dt_pin=19)
def poll_knob():
movement = volume.delta()
if movement != 0:
print("volume", volume.read(), "delta", movement)
if volume.clicked():
print("volume button clicked")
system.schedule("knob", 25, 25, -1, poll_knob)
import rotary
knob = rotary.Encoder(4, 5, steps_per_detent=2, invert=True)
knob.reset(position=10)
print(knob.info())
importrotary
Rotary encoder module.
The rotary module reads common Arduino-style incremental rotary encoder boards, including KY-040 style encoders with CLK, DT, and an optional push switch. The firmware uses GPIO interrupts to track movement, while user code reads position or delta synchronously. Use ``rotary.Encoder(...)`` to create independent encoder objects without manually choosing native channels.
rotary.Encoder(clk_pin: int, dt_pin: int, sw_pin: int = None, steps_per_detent: int = None, pullup: bool = None, active_low: bool = None, invert: bool = None, debounce_ms: int = None) -> rotary_encoder
rotary.Encoder(clk_pin: int, dt_pin: int, sw_pin: int = None, steps_per_detent: int = None, pullup: bool = None, active_low: bool = None, invert: bool = None, debounce_ms: int = None) -> rotary_encoderCreate an independent rotary encoder object.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
clk_pin |
int |
positional or keyword | Yes | GPIO pin connected to encoder CLK or A. |
dt_pin |
int |
positional or keyword | Yes | GPIO pin connected to encoder DT or B. |
sw_pin |
int |
positional or keyword | No | Optional GPIO pin connected to the encoder push switch. Defaults to no switch. |
steps_per_detent |
int |
positional or keyword | No | Optional encoder transitions per physical click. Defaults to 4 for common KY-040 style encoders. |
pullup |
bool |
positional or keyword | No | Optional boolean. Enables internal pull-ups by default. |
active_low |
bool |
positional or keyword | No | Optional boolean. Treats the switch as pressed when low by default. |
invert |
bool |
positional or keyword | No | Optional boolean. Reverses clockwise/counterclockwise direction. |
debounce_ms |
int |
positional or keyword | No | Optional switch debounce time in milliseconds. Defaults to 20. |
rotary_encoder rotary_encoder object.
rotary.Encoder().read() -> int
rotary.Encoder().read() -> intRead this encoder's current position.
rotary.Encoder().delta(reset=True) -> int
rotary.Encoder().delta(reset=True) -> intRead movement since the previous delta read.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
reset |
bool |
positional or keyword | No | Optional value. Defaults to True. |
rotary.Encoder().reset(position=0) -> bool
rotary.Encoder().reset(position=0) -> boolReset this encoder's position.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
position |
int |
positional or keyword | No | Optional value. Defaults to 0. |
rotary.Encoder().direction() -> int: Read last direction
rotary.Encoder().direction() -> int: Read last direction1, -1, or 0.
rotary.Encoder().button() -> bool
rotary.Encoder().button() -> boolRead the current push switch state.
rotary.Encoder().clicked(reset=True) -> bool
rotary.Encoder().clicked(reset=True) -> boolRead and optionally clear a debounced click.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
reset |
bool |
positional or keyword | No | Optional value. Defaults to True. |
rotary.Encoder().info() -> dict
rotary.Encoder().info() -> dictReturn this encoder's configuration and state.
rotary.Encoder().close() -> bool
rotary.Encoder().close() -> boolRelease this encoder's pins and native slot.
rotary.begin(clk_pin: int, dt_pin: int, sw_pin: int = None, channel: int = None, steps_per_detent: int = None, pullup: bool = None, active_low: bool = None, invert: bool = None, debounce_ms: int = None) -> bool
rotary.begin(clk_pin: int, dt_pin: int, sw_pin: int = None, channel: int = None, steps_per_detent: int = None, pullup: bool = None, active_low: bool = None, invert: bool = None, debounce_ms: int = None) -> boolInitialize a rotary encoder.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
clk_pin |
int |
positional or keyword | Yes | GPIO pin connected to encoder CLK or A. |
dt_pin |
int |
positional or keyword | Yes | GPIO pin connected to encoder DT or B. |
sw_pin |
int |
positional or keyword | No | Optional GPIO pin connected to the encoder push switch. Defaults to no switch. |
channel |
int |
positional or keyword | No | Optional rotary channel. Defaults to 0. Supported channels are 0 through 3. |
steps_per_detent |
int |
positional or keyword | No | Optional encoder transitions per physical click. Defaults to 4 for common KY-040 style encoders. Use 1 or 2 if your encoder reports too slowly. |
pullup |
bool |
positional or keyword | No | Optional boolean. Enables internal pull-ups by default. |
active_low |
bool |
positional or keyword | No | Optional boolean. Treats the switch as pressed when low by default, matching common modules wired to ground. |
invert |
bool |
positional or keyword | No | Optional boolean. Reverses clockwise/counterclockwise direction if your wiring reports movement backward. |
debounce_ms |
int |
positional or keyword | No | Optional switch debounce time in milliseconds. Defaults to 20. |
bool True when the encoder pins and interrupts are configured.
rotary.read(channel: int = None) -> int
rotary.read(channel: int = None) -> intRead the current encoder position.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | No | Optional rotary channel. Defaults to 0. |
int Integer position count.
rotary.delta(channel: int = None, reset: bool = None) -> int
rotary.delta(channel: int = None, reset: bool = None) -> intRead movement since the previous delta read.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | No | Optional rotary channel. Defaults to 0. |
reset |
bool |
positional or keyword | No | Optional boolean. Defaults to True, which clears the accumulated delta after reading. |
int Integer movement count. Positive and negative values indicate direction.
rotary.reset(channel: int = None, position: int = None) -> bool
rotary.reset(channel: int = None, position: int = None) -> boolReset encoder position.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | No | Optional rotary channel. Defaults to 0. |
position |
int |
positional or keyword | No | Optional integer position to store. Defaults to 0. |
bool True when the position and accumulated delta are reset.
rotary.direction(channel: int = None) -> int
rotary.direction(channel: int = None) -> intRead the last movement direction.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | No | Optional rotary channel. Defaults to 0. |
int Integer direction: 1 for positive movement, -1 for negative movement, or 0 when no direction has been recorded since reset.
rotary.button(channel: int = None) -> bool
rotary.button(channel: int = None) -> boolRead the current push switch state.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | No | Optional rotary channel. Defaults to 0. |
bool True when the optional switch pin is currently pressed.
rotary.clicked(channel: int = None, reset: bool = None) -> bool
rotary.clicked(channel: int = None, reset: bool = None) -> boolRead whether the push switch was pressed since the previous check.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | No | Optional rotary channel. Defaults to 0. |
reset |
bool |
positional or keyword | No | Optional boolean. Defaults to True, which clears the click flag after reading. |
bool True when a debounced press has been recorded.
rotary.detach(channel: int = None) -> bool
rotary.detach(channel: int = None) -> boolDetach a rotary encoder channel.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | No | Optional rotary channel. Defaults to 0. |
bool True when an initialized channel was detached.
rotary.info(channel: int = None) -> dict
rotary.info(channel: int = None) -> dictReturn rotary encoder state.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
channel |
int |
positional or keyword | No | Optional rotary channel. Defaults to 0. |
dict Dict with ``initialized``, ``channel``, ``clk_pin``, ``dt_pin``, ``sw_pin``, ``position``, ``delta``, ``direction``, ``button``, ``clicks``, ``steps_per_detent``, ``pullup``, ``active_low``, ``invert``, and ``debounce_ms`` fields.