Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

martinohanlon/KY040

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

This repository and project has now been archived and is no longer supported. The code will remain available for reference and information.

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.

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