Tuesday, January 30, 2024

Rotary Encoder--Hardware Debounce

 Readers: If you want to build the project featured in today's post, please go to PCBWAY's Community pages--a gerber ready to download and/or fabricate as well as KiCAD files, PDFs, a BOM, etc., are here.  

Also please visit PCBWAY's site using the link here--it will help this blog immensely. Thanks.

======

Hidy Ho! Welcome back for more blog fun. 

I have been super busy with my day job--correct, I don't blog for a living. 

But I've had a tiny bit of time this month to work with Elton at Otter Mods to design a small PCB for debouncing a cheap rotary encoder.

For a previous scribbles re: rotary encoders see the post here

I got a $2USD clone rotary encoder to "work" with an RP2040 and 1306 display, but try as I might I couldn't craft an effective algorithm in C++ to debounce the cheesy thing. 

Sure, there is a takes-no-brains-to-use debounce library for Arduino, but dammit Gil Amelio we are no longer in Kansas--I tried to port the C++ Arduino library into something for RP2040 and so far no dice...not enough time.

Is it a cop out to use hardware for this? Well, maybe, but here it is:


The Encoder hardware debounce board; available from the blog's generous sponsor PCBWAY. Get it from their community site here.

 

SE HABLA HARDWARE?

Elton at Otter Mods and I passed some KICAD schematics and PCB ideas back and forth, and here's what we came up with:


R1 and C1 integrate the encoder output while the 4011's make things into a nice clear rising or falling edge. Q1 and Q2 buffer, invert, and set the logic "high" level found at J5 and J6.

Laying out the board in Kicad was easy. I used SMD 1206 parts where I could--they are easier to swap out versus through hole if a value change is needed; no solder sucking--heat up the SMD part, get it out of there with tweezers, and drop in the new part. However, I only had DIP 4011 IC's so that's what ended with on the PCB.  

To bring the logic levels up to whatever voltage was suitable for my MCU (usually 3.3V or 5V, but in some situations I may need something else) I ended up employing trusty 2N3904 transistors: they were what I had lying around. This means: the +5V feeding R3 and R4 should be whatever logic "high" voltage your circuit needs.

Due to how the 4011 is wired up, the resulting outputs (J5, J6) behave differently than if you simply wired pins A and B of the encoder to your MCU. Specifically when rotating the encoder one way (let's say clockwise) A will change and B will not; rotating in the opposite direction B will change and A will not.

That means a simple embedded C example looks like this--easy!

void read_from_encoder()
{
 
        A = gpio_get(ENC_A);
        B = gpio_get(ENC_B);
        if ((A != A_prev) && (B == B_prev))
            {               
                display_write("CW");               
            }
             

         if ((A == A_prev) && (B != B_prev))    
                {           
                display_write("CCW");              
                }
     

        A_prev = A;
        B_prev = B;
         
}

THE BUILD

Super easy--fab took less than an hour and worked the first time. 

As far as mistakes: a few of the legends on the board were incorrect; this is fixed on the 1-29-24 version I uploaded to PCBWAY. 

Otherwise, the board seems to be A-OK.

Some useless build photos:

Happiness: unbagging the boards....

"SMD ahoy"


Oops, extra solder bit by the 4011, and note the Sharpee corrected legends, but hey it worked.


Testing....

The result is super clean edges for the encoder outputs, instead of the hash and scrambled eggs the encoder produced on its own.

Scope at 500uS per division--using the PCB the encoder state change produces a clean rising edge--good enough for what I do.


OUTTRO

I am still looking for the killer software debounce algorithm, but in the meantime, I see this RC/4011 idea used for debouncing and "quickly rising edge" square wave creation.  

I figure it'll work no matter what MCU is in your design. 

Coming up: I have a Moog hi-pass filter clone on the bench to next blow up; also, I am considering employing the debounce/encoder madness in this post into some sort of simple clock multiplier. Hope you can stay tuned.

No comments:

Post a Comment

Rotary Encoder Expermenter's Board: Improving the Hardware

Quick one this time....I have posted a few projects lately that incorporated a Raspberry Pi Pico, rotary encoder, and .96" OLED:  here ...