KY040 Rotary Encoder Module
View on PyPi.org
- Description
- Features
- Install
- Usage
- Arguments
A simple module for working with KY040 and similar encoders on the Raspberry Pi SBC. This project is part of a much bigger project design to assist in the designing and implementation of LCD/oLed user interfaces
-
Callback Functions You can callback encoder actions such as
onchange
,increment
anddecrement
to your own defined functions. /eg/:def MyFunction(): print("Turned Clockwised!") ... encoder.register(increment=MyFunction) ...
-
Variable motion control Specify how many 'ticks' of the encoder contribute to an action
-
Threading This module uses threading to prevent blocking of the running script
-
Customisable pin configuration Set which pins connect the encoder to the Raspberry PI GPIO
pip3 install RPi-GPIO-Rotary
Creates instance of rotary using the provided arguments: (clk,dt,sw,tck)
Argument | Description | Optional | Default |
---|---|---|---|
clk | Clock Pin | No | |
dt | DT Pin | No | |
sw | Button Switch Pin | Yes | |
tck | Tick value (int), how many ticks contribute as a rotational movement | Yes | 2 |
Callbacks are optional and can be configured ad-hock providing the current rotary object is stopped / not started
Argument | Description |
---|---|
increment | Callback when encoder is turned clockwise |
decrement | Callback when encoder is turned counter-clockwise |
onchange | Callback on any change of rotation, returns current counter |
pressed | Callback when button is pressed |
Starts monitoring the encoder
Stops monitoring the encoder, useful when dealing with changes in the UI
from RPi_GPIO_Rotary import rotary
## Define Callback functions
def cwTurn():
print("CW Turn")
def ccwTurn():
print("CCW Turn")
def buttonPushed():
print("Button Pushed")
def valueChanged(count):
print(count) ## Current Counter value
## Initialise (clk, dt, sw, ticks)
obj = rotary.Rotary(23,24,25,2)
## Register callbacks
obj.register(increment=cwTurn, decrement=ccwTurn)
## Register more callbacks
obj.register(pressed=buttonPushed, onchange=valueChanged)
## Start monitoring the encoder
obj.start()
...
## Your code here ##
...
## Stop monitoring
obj.stop()
from RPi_GPIO_Rotary import pypi
...
## Callback functions ##
...
## Setup First Encoder
menu_encoder = rotary.Rotary(23,24,25,2)
menu_encoder.register(increment=menu_up,decrement=menu_down,pressed=selected)
menu_encoder.start()
## Setup Second Encoder
volume_encoder = rotary.Rotary(17,27,22,1)
volume_encoder.register(onchange=setVolume,pressed=mute)
volume_encoder.start()