import machine
import utime as time
from machine import Pin
# Rotary encoder input pins
clk = Pin(25, Pin.IN, Pin.PULL_DOWN) # Using CLK (A) pin 25
dt = Pin(26, Pin.IN, Pin.PULL_DOWN) # Using DT (B) pin 26
# Now read the initial state of CLK pin
last_state_clk = clk.value()
counter = 0
while True:
# Read current state of CLK
current_state_clk = clk.value()
# If last and current state of CLK are different, then we have a pulse
# We only react to HIGH to LOW change
if (current_state_clk != last_state_clk and current_state_clk == 0):
# If DT state is different than CLK, encoder is rotating Clockwise
if (dt.value() != current_state_clk):
print("Clockwise")
counter = counter + 1
print(counter)
else:
print("CounterClockwise")
counter = counter - 1
print(counter)
# Remember our last CLK state
last_state_clk = current_state_clk;