# Effector (mover) |  Encoder KY-040 by Keyes - filament monitor

[![grafik.png](https://wiki.stadtfabrikanten.org/uploads/images/gallery/2026-06/scaled-1680-/tUA5qJ3rrZFIkITQ-grafik.png)](https://wiki.stadtfabrikanten.org/uploads/images/gallery/2026-06/tUA5qJ3rrZFIkITQ-grafik.png) [![grafik.png](https://wiki.stadtfabrikanten.org/uploads/images/gallery/2026-06/scaled-1680-/tmTm9HW42uKelsWK-grafik.png)](https://wiki.stadtfabrikanten.org/uploads/images/gallery/2026-06/tmTm9HW42uKelsWK-grafik.png) [![grafik.png](https://wiki.stadtfabrikanten.org/uploads/images/gallery/2026-06/scaled-1680-/LTr8kxJN1kKFucwO-grafik.png)](https://wiki.stadtfabrikanten.org/uploads/images/gallery/2026-06/LTr8kxJN1kKFucwO-grafik.png) [![grafik.png](https://wiki.stadtfabrikanten.org/uploads/images/gallery/2026-06/scaled-1680-/MkbVPUcvvLFc0Uez-grafik.png)](https://wiki.stadtfabrikanten.org/uploads/images/gallery/2026-06/MkbVPUcvvLFc0Uez-grafik.png) [![grafik.png](https://wiki.stadtfabrikanten.org/uploads/images/gallery/2026-06/scaled-1680-/jqQr2D8ZxlXapNEb-grafik.png)](https://wiki.stadtfabrikanten.org/uploads/images/gallery/2026-06/jqQr2D8ZxlXapNEb-grafik.png)

**Technical data:**

- Operating temperature: -40 °C to 125 °C
- Setting points per revolution: 12 (you should measure for your self because this varies from vendor to vendor)
- with switch button
- Fixed hole size: 2.0 mm
- Product size: 32.0 mm \* 15.0 mm
- Working voltage: 3.0 V ~ 5.3 V

**Warning**: Wrong wiring can destroy CLK/DT lines (painfully tested)

## Used for

- [Filament sensing with KY-040 Rotary Encoder](https://old.stadtfabrikanten.org/display/TH/Filament+sensing+with+KY-040+Rotary+Encoder)

## Basic example script for testing with Raspberry Pi (not used in production!)

[https://pypi.org/project/pigpio-encoder](https://pypi.org/project/pigpio-encoder/)  
[<span>https://github.com/joan2937/pigpio</span>](https://github.com/joan2937/pigpio)

```bash
apt install python3-pip
pip3.7 install pigpio_encoder
cd /opt/gpio/
git clone https://github.com/modmypi/Rotary-Encoder/
cd Rotary-Encoder
vim rotaryEncoder.py #used pins: clk=6, dt=12, sw=5
```

```bash
from pigpio_encoder import pigpio_encoder
 
def rotary_callback(counter):
    print("Counter value: ", counter)
 
def sw_short():
    print("Switch short press")
 
def sw_long():
    print("Switch long press")
 
my_rotary = pigpio_encoder.Rotary(clk=6, dt=12, sw=5)
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()
```

```bash
#enable pigpiod daemon (required to run pigpio things)
vim /etc/systemd/system/pigpiod.service
 
[Unit]
Description=Pigpio daemon
 
[Service]
Type=forking
#disallow port 8888 from outside
ExecStart=/usr/bin/pigpiod -l
 
[Install]
WantedBy=multi-user.target
 
systemctl enable pigpiod.service
systemctl start pigpiod.service
service pigpiod restart
```

```bash
#finally run the script
python3.7 /opt/gpio/rotaryEncoder.py #do not use Python2 because it will fail
```