Rotary encoder
Moderators: grovkillen, Stuntteam, TD-er, enesbcs
- budman1758
- Normal user
- Posts: 301
- Joined: 15 Apr 2017, 05:13
- Location: Riverside CA USA
Rotary encoder
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?
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".
Re: Rotary encoder
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.
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.
- budman1758
- Normal user
- Posts: 301
- Joined: 15 Apr 2017, 05:13
- Location: Riverside CA USA
Re: Rotary encoder
I've tried a couple different circuit setups using resistors and capacitors and it either doesn't help or makes it worse.
What would that look like from a schematic point of view?So maybe you can clean up the signal by using a transistor per A/B signal.
"The glass is twice as big as it needs to be".
Re: Rotary encoder
Well a typical rotary encoder is connected like this:
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:
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:
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
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:
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:
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
Re: Rotary encoder
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.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.
- budman1758
- Normal user
- Posts: 301
- Joined: 15 Apr 2017, 05:13
- Location: Riverside CA USA
Re: Rotary encoder
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.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.
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".
Re: Rotary encoder
I did not do anything special, just wired directly clk, dt, + and gnd to RPI. Tested with a similar module: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?
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?
- budman1758
- Normal user
- Posts: 301
- Joined: 15 Apr 2017, 05:13
- Location: Riverside CA USA
Re: Rotary encoder
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 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".
Re: Rotary encoder
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?
Are the capacitors (if you used any) and resistors on both A/B lines the same?
- budman1758
- Normal user
- Posts: 301
- Joined: 15 Apr 2017, 05:13
- Location: Riverside CA USA
Re: Rotary encoder
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".
Re: Rotary encoder
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)
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)
- budman1758
- Normal user
- Posts: 301
- Joined: 15 Apr 2017, 05:13
- Location: Riverside CA USA
Re: Rotary encoder
It works either way. I can reverse the 2 connections and it will go the opposite.The direction is also correct?
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".
Re: Rotary encoder
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.
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.
Who is online
Users browsing this forum: No registered users and 0 guests