Reading A Rotary Encoder in pyfirmata

178 views Asked by At

I am trying to get the data from an encoder with pyfirmata. But I couldn't find anything on this particular topic. If someone could help me I would be very grateful.

1

There are 1 answers

1
damp11113 On

Here

Pin

CLk -> D8

DT -> D7

from pyfirmata import Arduino, util
import pyfirmata.util

counter = 0

board = Arduino('COM3')

iterator = pyfirmata.util.Iterator(board)
iterator.start()

board.digital[9].mode = pyfirmata.INPUT
board.digital[8].mode = pyfirmata.INPUT

aLastState = board.digital[9].read()

while True:
    aState = board.digital[9].read()
    if aState != aLastState:
        if board.digital[8].read() != aState:
            counter +=1
        else:
            counter -= 1
        print(f'Position: {counter}')
    aLastState = aState