Skip to main content

Pigpio Encoder

Version: 0.2.4

Requires Python 3

Python module for the KY040 rotary encoder.

This module has been developed for quickly interface a rotary encoder with Raspberry Pi. It's based on the pigpio library (cause it proved to be faster than rpi.GPIO or gpiozero libraries) so you need to install pigpio library and run pigpio daemon before starting your script.

Features

  • Easy to setup callback functions for the Rotary Encoder and the Switch.
  • The Rotary Encoder has customizable min and max values (default 0-100).
  • The Rotary Encoder increase/decrease value is customizable (default 1).
  • The Switch can be activated or not.
  • The Switch can have two different functions for short press or long press.
  • Both Rotary and Switch have a customizable debounce value (default 300ms)

Installation

  • Install the pigpio library (check pigpio documentation for alternative installation method)
  • sudo apt-get update
  • sudo apt-get install pigpio python-pigpio python3-pigpio
  • Install the pigpio_encoder library
  • python3 -mpip install pigpio_encoder (consider add --user option)
  • start pigpio daemon
  • sudo pigpiod

How to use

  • import the module

    from pigpio_encoder.rotary import Rotary
    
  • create a callback function for the Rotary Encoder counter.

    You must pass a positional argument to retrieve the counter value.

    def rotary_callback(counter):
        # some action with counter...
    
  • create a callback function for Up-Rotation events.

    def up_callback():
        # some action if rotated upward
    
  • create a callback function for Down-Rotation events.

    def down_callback():
        # some action if rotated downward
    
  • create callbacks functions for the Switch

    If you intend to use the switch you must create at least the "short press" callback. The "long press" callback is necessary if you want to use that feature.

    def sw_short_callback():
        # some action...
    
    def sw_long_callback():
        # some action...
      ```
    
  • create the rotary object

    here you setup the gpio id as keyword argument. If you don't pass the switch parameter the switch won't be activated.

    my_rotary = Rotary(
            clk_gpio=<gpio_id of clk signal>,
            dt_gpio=<gpio_id of dt signal>,
            sw_gpio=<gpio_id of switch signal>
            )
    
  • setup the rotary encoder for counting

    here you can setup min and max values for the encoder, the increase/decrease value, a debounce value (default 300ms) and the callback function.

    my_rotary.setup_rotary(
            min=<min_value>,
            max=<max_value>,
            scale=<scale_value>,
            debounce=<debounce_value>,
            rotary_callback=<rotary_callback>
            )
    
  • Optional setup for up and down rotation events

    here you can setup min and max values for the encoder, the increase/decrease value, a debounce value (default 300ms) and the callback functions.

    my_rotary.setup_rotary(
            min=<min_value>,
            max=<max_value>,
            scale=<scale_value>,
            debounce=<debounce_value>,
            up_callback=<up_callback>
            down_callback=<down_callback>
            )
    
  • setup the switch

    if you have specified the switch pin when creating the encoder object, here you can setup the debounce value, the long press option and the callbacks.

    my_rotary.setup_switch(
            debounce=<debounce_value>,
            long_press=<True>,
            sw_short_callback=<sw_short_callback>,
            sw_long_callback=<sw_long_callback>
            )
    
  • start the listener

    my_rotary.watch()
    

    Please note that calling .watch() this is only for demostration purposes. It is not neccessary for the encoder to operate it can even cause problems.


Basic example using default values

from pigpio_encoder.rotary import Rotary


def rotary_callback(counter):
  print("Counter value: ", counter)


def sw_short():
  print("Switch pressed")


def up_callback():
  print("Up rotation")


def down_callback():
  print("Down rotation")


my_rotary = Rotary(clk_gpio=27, dt_gpio=22, sw_gpio=17)
my_rotary.setup_rotary(
  rotary_callback=rotary_callback,
  up_callback=up_callback,
  down_callback=down_callback,
)
my_rotary.setup_switch(sw_short_callback=sw_short)

my_rotary.watch()

Please note that calling .watch() this is only for demostration purposes. It is not neccessary for the encoder to operate it can even cause problems.


Example using all the Features

from pigpio_encoder.rotary import Rotary


def rotary_callback(counter):
  print("Counter value: ", counter)


def sw_short():
  print("Switch short press")


def sw_long():
  print("Switch long press")


my_rotary = Rotary(
  clk_gpio=27,
  dt_gpio=22,
  sw_gpio=17
)
my_rotary.setup_rotary(
  min=10,
  max=300,
  scale=5,
  debounce=200,
  rotary_callback=rotary_callback
)
my_rotary.setup_switch(
  debounce=200,
  long_press=True,
  sw_short_callback=sw_short,
  sw_long_callback=sw_long
)

my_rotary.watch()

Please note that calling .watch() this is only for demostration purposes. It is not neccessary for the encoder to operate it can even cause problems.


Thanks to...

Metadata

Release files for pigpio-encoder 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pigpio-encoder 0.2.4
File Size Uploaded
pigpio_encoder-0.2.4.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pigpio-encoder 0.2.4
File Interpreter ABI Platform
pigpio_encoder-0.2.4-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 14.6 kB

Release files / pigpio_encoder-0.2.4.tar.gz

Download URL pigpio_encoder-0.2.4.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6efbcc0e7353b42fab6708e2cc076189fafac611f89ac9a18826e17607950f10
BLAKE2b-256 checksum
How to use checksums
4c5462d40dcc1052ca899bfff87fb735146037e7ab2aec9eff4fa1841fa61620
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.7.3 Linux/5.8.0-0.bpo.2-amd64

Release files / pigpio_encoder-0.2.4-py2.py3-none-any.whl

Download URL pigpio_encoder-0.2.4-py2.py3-none-any.whl
Size 7.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
00a41c22ebe64bca449f2317a03e2e28e2893381b12c0bc44e66bb4c3ae172dd
BLAKE2b-256 checksum
How to use checksums
2208c04bdb75f07afa3927a05acf991e4437324616ec66c241b534114271d06a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.7.3 Linux/5.8.0-0.bpo.2-amd64

Release history Release notifications | RSS feed

This release

0.2.4 This release

2 release files

0.2.2

1 release file

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page