Based on the work of Martin O'Hanlon See: https://github.com/martinohanlon/KY040
A python module for reading the values from a KY040 rotary encoder module using a Raspberry Pi.
http://www.stuffaboutcode.com/2015/05/raspberry-pi-and-ky040-rotary-encoder.html
Listen for rotary change and switch button press:
# Define your pins
CLOCKPIN = 5
DATAPIN = 6
SWITCHPIN = 13
# Callback for rotary change
def rotaryChange(direction):
print "turned - " + str(direction)
# Callback for switch button pressed
def switchPressed():
print "button pressed"
# Create a KY040 and start it
ky040 = KY040(CLOCKPIN, DATAPIN, SWITCHPIN, rotaryChange, switchPressed)
ky040.start()
# Keep your proccess running
try:
while True:
sleep(0.1)
finally:
ky040.stop()
If you're not using switch button, only listen for rotary change:
# Define your rotary pins
CLOCKPIN = 5
DATAPIN = 6
# Callback for rotary change
def rotaryChange(direction):
print "turned - " + str(direction)
# Create a KY040 and start it
ky040 = KY040(CLOCKPIN, DATAPIN, rotaryCallback=rotaryChange)
ky040.start()
# Keep your proccess running
try:
while True:
sleep(0.1)
finally:
ky040.stop()
Using a custom bounce time:
The time is in seconds.
# Create a KY040 and start it
ky040 = KY040(CLOCKPIN, DATAPIN, SWITCHPIN, rotaryChange, switchPressed, rotaryBouncetime=0.40, switchBouncetime=0.40)
ky040.start()
- 0.1 - Initial stable version based on work of Martin O'Hanlon
This project is under MIT License.