A python module for reading the values from a KY040 rotary encoder module using a raspberry pi
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
ky040
.gitattributes
.gitignore
LICENSE
README.md
RaspberryPiKY040.fzz
RaspberryPiKY040_bb.jpg
ky040.jpg
setup.py

README.md

KY040 Python Module

Martin O'Hanlon (martin@ohanlonweb.com)

http://www.stuffaboutcode.com

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

KY040

Usage

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()
    GPIO.cleanup()

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()
    GPIO.cleanup()

Using a custom bounce time:

# Create a KY040 and start it
ky040 = KY040(CLOCKPIN, DATAPIN, SWITCHPIN, rotaryChange, switchPressed, rotaryBouncetime=40, switchBouncetime=40)
ky040.start()

Wiring

KY040 Wiring schema

Version history

  • 0.1 - Initial stable version
  • 0.2 - Minor improvement

License

This project is under MIT License.