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

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

Basic example script for testing with Raspberry Pi (not used in production!)
https://pypi.org/project/pigpio-encoder
https://github.com/joan2937/pigpio
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
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()
#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
#finally run the script
python3.7 /opt/gpio/rotaryEncoder.py #do not use Python2 because it will fail