gbr1 / rp2040-encoder-library

Arduino quadrature encoder library based on RPI Pico pio example
GNU Lesser General Public License v3.0
23 stars 2 forks source link

Debouncing suggestions? #6

Closed ace-johnny closed 1 week ago

ace-johnny commented 1 week ago

Is there a recommended strategy for debouncing with this library? I understand that max_step_rate can be used to divide sys_clk for the PIO state machine's frequency, but I couldn't find a value that prevented getCount() from bouncing noticeably, especially during quick rotations.

Another quadrature decoder library that I've used over the years, based on Ben Buxton's original state machine code, has proven absolutely rock-solid with no bounce whatsoever, but requires interrupts to function reliably.

I'd love to figure out a similar method for this elegant PIO implementation, since this code operates independently from the CPU and requires no resources other than an available PIO block.

Am I missing an obvious technique? Is there any other suggested code I should study?

gbr1 commented 1 week ago

Hi @ace-johnny ! Which kind of encoder are you using? I suggest you to try working more on electrical connections, maxing a RC filter on lines if it is mechanical. Let me know!

ace-johnny commented 1 week ago

At the moment I'm just prototyping with a few cheap KY-040 modules, which typically use whatever EC11 encoder the manufacturer had handy. They have built-in pullups, which simplifies breadboarding, but they're awfully bouncy.

KY-040 rotary encoder module

The library I mentioned earlier has become a favorite of mine because it does not bounce at all due to its clever state machine transitions, but its use of interrupts isn't always appropriate, especially when using multiple encoders.

I didn't expect a change in your library (which is a delight by the way, thanks!), but was wondering what approach you or others took to solve debouncing when using PIO programs to decode manual mechanical rotaries.

And one more question since I have your attention. Does the max_step_rate value represent a physical unit, such as steps per microsecond or something similar? I noticed that it uses a multiple of 14 during system clock division, but wasn't entirely sure the best way to use it, if at all.

gbr1 commented 1 week ago

Hi @ace-johnny ! max_step_rate is used to set the frequency of the PIO machine. (probably it shouldn't be a parameter 🤔 )

Unfortunately I think that building a debounce with PIOs is quite complex 😢 . Another user was using the same encoder of yours and we found that you need to change the count mode. Have you already tried it?

ace-johnny commented 1 week ago

Yes, as I stated in my first post, I'm aware that max_step_rate controls the PIO frequency, but I was wondering why you chose a multiple of 14 rather than the value 10 from the original Raspberry Pi PIO example. And, as I also mentioned, changing the PIO frequency had no appreciable effect on debouncing, so I agree that setting is likely best left at default.

I've also been using these KY-040 modules for quite some time, and am well aware of their ideal configuration. Yes, they do require a count mode of COUNT_2X, and usually a flip setting of true (or just swapping A/B pins), but none of that relates to debouncing.

I appreciate the response, but if there's no method to debounce mechanical rotary encoders with this library, I'll just move on. Thanks.