0

I am using LS s40-6-1000ZT(H) 1000 p/r rotary encoder for my project with raspberry pi 3,but raspberry not working properly as per encoder specification(i.e not counting event properly).What could be the issue? Any solution??

from RPi import GPIO
from time import sleep
import tkinter as tk
a=22
b=23
GPIO.setmode(GPIO.BCM)
GPIO.setup(a,GPIO.IN)
GPIO.setup(b,GPIO.IN)
root=tk.Tk()
clkLastState=GPIO.input(a)
def call():
   global clkLastState
   global counter
   clkState=GPIO.input(a)
   if clkState != clkLastState:
           dtState=GPIO.input(b)
           if dtState != clkState:
              counter +=1
           else:
              counter -=1
           text=str(counter/2)
           print (text[0:-2])
           var.set(text)
   clkLastState=clkState

def my_callback(channel):
   call()

counter=0
clkLastState=GPIO.input(a)
#GPIO.add_event_detect(23,GPIO.BOTH,callback=my_callback)
print (counter)
var=tk.IntVar()
L1=tk.Label(root,bg="orange",fg="red",textvariable=var)
L1.pack()
# var.set(counter)
# root.after(500,main)
root.geometry("100x100")
#GPIO.add_event_detect(22,GPIO.BOTH,callback=my_callback)
GPIO.add_event_detect(23,GPIO.BOTH,callback=my_callback,debouncetime=6)
root.mainloop()
#var=tk.IntVar()
#L1=tk.Label(root,bg="orange",fg="red",textvariable=var)
#L1.pack()  
#GPIO.add_event_detect(23,GPIO.BOTH,callback=my_callback)

GPIO.cleanup()
1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – goldilocks
    Feb 13, 2020 at 15:11

1 Answer 1

1

pigpio has a more deterministic response to GPIO level changes.

Try my pigpio Python example.

It requires the pigpio daemon.

sudo pigpiod

If Python can't keep up you will have to use C instead. Perhaps try this pdif2 C example. It also use the pigpio daemon.

6
  • ,I tried with "pigpio Python example.".It work fine only to read but as per my application,I am showing that pos value on tkinter.when i add tkinter part with your code ,then pos value reduced to approx 900 per rotation and more faulty count coming with anticlockwise direction.
    – ram
    Feb 13, 2020 at 16:08
  • I can't help with that. I don't see that tkinter should affect the readings unless the system is almost overloaded.
    – joan
    Feb 13, 2020 at 16:12
  • ,there is nothing more on my system (i.e raspberry pi 3).only encoder reading and showing on tkinter window which contain 4 buttons and button function.Am i using tkinter in wrong way?Should i put tkinter part in another class like you create class decoder in your pigpio code?
    – ram
    Feb 13, 2020 at 16:18
  • ,thanks for your valuable suggestion.Only one issue coming in your ` Python example.`code i.e at of sudden it stuck and it start only after rebooting raspberry Pi.
    – ram
    Feb 14, 2020 at 8:26
  • is it for decoder.cancel() or pi.stop in the code?
    – ram
    Feb 14, 2020 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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