Rotary encoder

Moderators: grovkillen, Stuntteam, TD-er, enesbcs

Post Reply
Message
Author
User avatar
budman1758
Normal user
Posts: 252
Joined: 15 Apr 2017, 05:13
Location: Riverside CA USA

Rotary encoder

#1 Post by budman1758 » 16 Jul 2020, 23:05

I'm in the process of trying to set up a rotary encoder in a project. The basic outline is to be able to set a value with the encoder and put that value in a dummy device for further processing. Similar to setting a temp on a thermostat. I have one just like the one in the wiki. The one without the pcb.https://www.letscontrolit.com/wiki/imag ... er_001.jpg

I have it connected on a breadboard. The center pin is tied to ground and the other two are on GPIO 23 & 24 which are both set as input-pullup. It is "working" but not very well at all. If you turn the encoder very slow it will count up or down as it should "most" of the time but still misses steps or sometimes the count will go the opposite direction. If you turn the encoder rapidly it does not count the steps correctly at all. In other words it is extremely unreliable to the point of being pretty useless.

The encoder has a total of 20 steps per revolution. Connected to a Pi4. Latest version of RPIEasy

Is there some way to increase its usefulness? Am I missing something in the wiring or setup?
"The glass is twice as big as it needs to be".

TD-er
Core team member
Posts: 3300
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rotary encoder

#2 Post by TD-er » 17 Jul 2020, 01:18

Most of these rotary encoders do not have a particularly clean signal.
If you look at the signal with a scope, you will see it will probably give some extra pulses per state change.

Most techniques to overcome these is to add some delay in the software where you only accept a pulse if it is "high" or "low" for at least some time.
But that does act against the counting of higher frequency steps.

Not sure how it is dealt with on the pi and maybe also the pins don't have a HW interrupt?

What you can do to test is make the steps a bit more clean.
So maybe you can clean up the signal by using a transistor per A/B signal.
Or maybe use a level converter chip, which does something similar.

User avatar
budman1758
Normal user
Posts: 252
Joined: 15 Apr 2017, 05:13
Location: Riverside CA USA

Re: Rotary encoder

#3 Post by budman1758 » 17 Jul 2020, 05:32

I've tried a couple different circuit setups using resistors and capacitors and it either doesn't help or makes it worse.
So maybe you can clean up the signal by using a transistor per A/B signal.
What would that look like from a schematic point of view?
"The glass is twice as big as it needs to be".

TD-er
Core team member
Posts: 3300
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rotary encoder

#4 Post by TD-er » 17 Jul 2020, 12:06

Well a typical rotary encoder is connected like this:
Image

So either it is pulled up, via the resistors, or it is pulled to GND via the switch.
Using lower value resistors, you make it less sensitive to noise and using a larger capacitor you dampen the false pulses.
But both also have an effect on the max pulse frequency as the capacitors also round off the square wave pulses.
Also lowering the resistor value will give a higher power consumption as in rest it is very likely one of A/B switches (or both) may pull to GND.

With adding an extra resistor to limit the current when discharging the capacitor, you get something like this:
Image
By limiting the discharge current you make sure the rising time is similar to the falling edge of the pulse.

With a simple inverter chip you get something like this:
Image
See this Hackaday article about it.

The inverter chip does a bit more to reshape the pulse compared to a simple transistor.
N.B. if you invert both pulses of A/B you effectively reverse the rotation direction

User avatar
enesbcs
Normal user
Posts: 533
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Rotary encoder

#5 Post by enesbcs » 18 Jul 2020, 17:37

budman1758 wrote: 16 Jul 2020, 23:05 If you turn the encoder very slow it will count up or down as it should "most" of the time but still misses steps or sometimes the count will go the opposite direction. If you turn the encoder rapidly it does not count the steps correctly at all.
In the rotary plugin i've used event_detection with 10 millisec bouncetime protection (intermediate events will be ignored) which worked fine with my Raspberry Zero, but it might not work well with RPI4. I will add a parameter to finetune it.

User avatar
budman1758
Normal user
Posts: 252
Joined: 15 Apr 2017, 05:13
Location: Riverside CA USA

Re: Rotary encoder

#6 Post by budman1758 » 19 Jul 2020, 21:54

In the rotary plugin i've used event_detection with 10 millisec bouncetime protection (intermediate events will be ignored) which worked fine with my Raspberry Zero, but it might not work well with RPI4. I will add a parameter to finetune it.
I'm not having much luck at all here. Tried the circuits above except the one with the inverter and nothing seems to make any difference, Tried the bounce time setting also. Most of the time the encoder seems like there is no response at all. As if the software is not "listening" to it.

Can you tell me how you have it set up on your Pi Zero? Wired direct or what buffer circuit are you using?
"The glass is twice as big as it needs to be".

User avatar
enesbcs
Normal user
Posts: 533
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Rotary encoder

#7 Post by enesbcs » 19 Jul 2020, 23:19

budman1758 wrote: 19 Jul 2020, 21:54 Can you tell me how you have it set up on your Pi Zero? Wired direct or what buffer circuit are you using?
I did not do anything special, just wired directly clk, dt, + and gnd to RPI. Tested with a similar module:
https://www.gmelectronic.com/data/produ ... -029.1.jpg
Swapping clk and dt ends in weird results as i remember.

Did you try 0 bouncetime? Did you use any other device on this RPI?

User avatar
budman1758
Normal user
Posts: 252
Joined: 15 Apr 2017, 05:13
Location: Riverside CA USA

Re: Rotary encoder

#8 Post by budman1758 » 20 Jul 2020, 05:36

I seem to have it working better now. Seems to work best with the bounce time somewhere between zero and 5 ms. Found an inverter chip and that helps a lot too.
The only thing now is that for each detent it turns the plugin counts 2 steps. Setting is at 1 step. Seems to be doubling the step count unless that is normal. Its pretty consistent too. If I set the steps to 2 it counts 4 and so on.
"The glass is twice as big as it needs to be".

TD-er
Core team member
Posts: 3300
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rotary encoder

#9 Post by TD-er » 20 Jul 2020, 09:56

Does it always count double, even at (very) low speed?
Are the capacitors (if you used any) and resistors on both A/B lines the same?

User avatar
budman1758
Normal user
Posts: 252
Joined: 15 Apr 2017, 05:13
Location: Riverside CA USA

Re: Rotary encoder

#10 Post by budman1758 » 20 Jul 2020, 19:00

TD-er wrote: 20 Jul 2020, 09:56 Does it always count double, even at (very) low speed?
Are the capacitors (if you used any) and resistors on both A/B lines the same?
It is counting double quite consistently. Not using capacitors at all. Does not work at all with them.
Output of encoder goes thru a resistor and then thru the inverter chip.
Also works the same going direct from encoder to inverter without any resistors.
If I hold the encoder in-between detents it will count 1 step. So it would seem to be "correct" for my particular encoder but not very intuitive.
"The glass is twice as big as it needs to be".

TD-er
Core team member
Posts: 3300
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rotary encoder

#11 Post by TD-er » 20 Jul 2020, 21:21

The direction is also correct?
If so, then I guess it is using the 'inbetween' steps merely to make sure it is possible to read the rotation direction and thus effectively halve the number of steps you could make. (or double the step rate)

User avatar
budman1758
Normal user
Posts: 252
Joined: 15 Apr 2017, 05:13
Location: Riverside CA USA

Re: Rotary encoder

#12 Post by budman1758 » 21 Jul 2020, 01:53

The direction is also correct?
It works either way. I can reverse the 2 connections and it will go the opposite.
It is also working just as well using just the inverter chip with no resistors or capacitors.
"The glass is twice as big as it needs to be".

TD-er
Core team member
Posts: 3300
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rotary encoder

#13 Post by TD-er » 21 Jul 2020, 11:14

Great.
So the inverter chip does a good job in cleaning up the pulses.

Such inverter chips are a very old technique to clean up and protect a circuit.
You could often find them right where signal cables enter a device like a (parallel) printer.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests