rotary encoder reading error discussion notes

Active today
Viewed 10 times

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
def call():
   global clkLastState
   global counter
   if clkState != clkLastState:
           if dtState != clkState:
              counter +=1
              counter -=1
           print (text[0:-2])

def my_callback(channel):

print (counter)
# var.set(counter)
# root.after(500,main)

  • Ah, let me see. (1) Nuvoton is MCU (Micro Controller), like Arduino, so you can ask her to pay attention to do one thing and just one thing, counting pulses, so never missing any. On the other hand, Rpi is a “multi-tasking” computer, like a naught girl, doing many tasks at the same time, jumping up and down, here and there, therefore you cannot ask her to pay attention to just one thing. (2) If you like, Rpi is sort of ADHD (Attention-deficit/hyperactivity disorder), always absent mindedly miscount pulses. / to continue, … – tlfong01 1 hour ago    
  • (3) In real life, one workaround is big sister Rpi partnering with the little sister WiFi MCU, ESP8266/32, which won’t miscount pulses and at the same time doing WiFi work (OK, she is doing two tasks at the same time, but not multitasking. Me giving up explaining deeper, you google :)) – tlfong01 1 hour ago    
  • Actually there are a couple of other work around to reduce miscount. Say if you are using Rpi to do a Micky Mouse project using low speed geared cheapie, ugly looking, yellow toy motors, you can use the “Discard Odd Man Out” moving average counting method. Take an oversimplified sample. Suppose your motor speed is about 600 rpm (revolutions per minute). You can either (1) count pulses every minute, and get the results 600, 601, 599 rpm etc. – tlfong01 13 mins ago   
  • But this is not very accurate, because Rpi, during that minute, might pause a bit to do other side jobs. Now the trick is to count pulses not every minute, but every second. So now you would get almost always 10, 11, 9, 10 revolutions and so on. If is likely that once in a blue moons, you get abnormal results in some seconds, like 5, or 6 counts in a second. The reason for this odd man out result is because Rpi during that second pause counting and divert to do other house keeping chores, such as answer the door etc. – tlfong01 9 mins ago   
  • Now you have have already guessed that you simply discard the odd man out and use say only 10 normal second results and multiple by 10 to get the rpm. Actually this trick is not used in toy projects. Even CalTech JPL (Jet Propulsation Lab)’s rocket scientist, using the same, yes, same trick, but using analog hardware, such as operational amplifiers to find the odd man out. They called this trick digital filtering and the most famous version is Karman Filter. – tlfong01 2 mins ago   Edit   
  • You might heard of other Rpi and Arduino guys using this digital filtering trick their balancing their two wheel car algorithms. Ah, evening tea time. See you later. PS I am just thinking aloud, my apologies for all the typos. – tlfong01 26 secs ago   Edit   


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.

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: