I'm using Python class Rotary Encoder to implement a Position PI on a stepper motor and using this encoder H6BM-1000-500-IE-S-H

The code is working quite well, however I'm getting an error of ~5mm when I run large distances. My guess is that I'm missing interrupts because of speed, however my whole code is on Python. I wanted to know if I could use this C code via ctypes to wrap it to Python. I'm new to this Python module (ctypes) and don't use C at all. If it is possible, how should I compile it in order to use it on Python?

It doesn't have to be exclusively the C code mentioned above, but preferably pigpio code.

It can also be this C code for Rotary Encoder.

1 Answer 1


I use the roatary_encooder driver. https://www.kernel.org/doc/Documentation/input/rotary-encoder.txt

It has device tree bindings here: http://lxr.free-electrons.com/source/Documentation/devicetree/bindings/input/rotary-encoder.txt

My dts(device tree source) is:

 * wheelhat-overlay.dts


/ {
    compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";

    fragment@0 {
        target-path = "/soc/gpio";
        __overlay__ {
            wheel_pins: wheel_pins {
                brcm,pins = <13 27>;
                brcm,function = <0>;
                brcm,pull = <2>;

    fragment@1 {
        target-path = "/soc";
        __overlay__ {
            wheel: wheel {
                compatible = "rotary-encoder";
                #address-cells = <1>;
                #size-cells = <0>;
                pinctrl-names = "default";
                pinctrl-0 = <&wheel_pins>;
                gpios = <&gpio 13 1>, <&gpio 27 1>; 
                linux,axis = <0>; /* REL_X */
    __overrides__ {
        relative_axis =  <&wheel>,"rotary-encoder,relative-axis";
        linux_axis =  <&wheel>,"linux,axis";
        rollover =  <&wheel>,"rotary-encoder,rollover";
        half-period =  <&wheel>,"rotary-encoder,half-period";
        steps =  <&wheel>,"rotary-encoder,steps";

The first fragment makes the pins have an input function with a pull-up. The rest is configuration and names exported to /boot/config.txt. You compile the DTS file and put it in /boot/overlays. The driver can do absolute x axis so you do not need to worry about losing interrupts. You need a Python event handler to catch x axis changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.