Skip to content
/ KY040 Public
forked from martinohanlon/KY040

A python module for reading the values from a KY040 rotary encoder module using a raspberry pi

License

Notifications You must be signed in to change notification settings

peqoud/KY040

 
 

Repository files navigation

KY040 Python Module - GPIOZERO based

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

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()

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()

Wiring

KY040 Wiring schema

Version history

  • 0.1 - Initial stable version based on work of Martin O'Hanlon

License

This project is under MIT License.

About

A python module for reading the values from a KY040 rotary encoder module using a raspberry pi

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%