Raspberry pi : Master
Rotary Encoder: Slave (14 Bit of Transmission ,3 wire communication (MISO,CLK,CE))

After each cycle of communication it is compulsory to toggle CS pin from high to low.

when clock is toggle rotary encoder send data to PI at each 9600 HZ frequency

and using MISO PIN when we rotate rotary encoder angel will print on screen clockwise rotation & anticlock wise rotation,it will display on screen using SPI protocol i have Code of Rotary Encoder in Arduino but i want to run that same Encoder on raspberry pi using Python here i mention Arduino program


  import spidev
import time
from sys import stdout
import RPi.GPIO as GPIO           # import RPi.GPIO module  

GPIO.setmode(GPIO.BCM)            # choose BCM or BOARD
GPIO.setup(8, GPIO.OUT) # set a port/pin as an output   
GPIO.output(8, 1)       # set port/pin value to 1/GPIO.HIGH/True  

#Define all constants.


counterSize = 1
spi = spidev.SpiDev() 
spi.open(0, 0)
spi.max_speed_hz =9600

def readCounter():
    result = spi.xfer2(READ_COUNTER)
    GPIO.output(8, 0)
    data = [] #Double check that a list i necessary for this. 
    EncoderCount = 0

    #spi.open(0, 0)
    for i in range(counterSize):
        data = spi.xfer2([0x00])
        EncoderCount = EncoderCount*16384 + data[0]
    return EncoderCount

def readStatus():
    data = []
    data = spi.xfer2([0xFF])
    return data

while True:
      EncoderCount =readCounter()
      stdout.write("\rCount is [%s] status is %s.     " % (str(EncoderCount), str(readStatus())))


i tried with this code but still so many lines missing as per my requirement

  • 1
    What exactly is your question? Jul 8 at 12:23
  • run this potentiometer.in/R_Ruggedsensore.php encoder on raspberry pi Jul 8 at 12:29
  • 1
    that's not a question; it's a statement... what have you tried? Any problems / error messages. Please update your question with relevant details
    – Dirk
    Jul 8 at 13:26
  • please check now my question i hope now you get my question Jul 9 at 4:27
  • 1
    still no question ... do you know what a question is?
    – jsotola
    Jul 9 at 7:06

this article could help I believe: how to use rotary encoder with RPI

  • my encoder is diffrent not that type of encoder Jul 8 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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