Debounced rotary encoder module

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024

Комментарии • 12

  • @moeburn
    @moeburn 10 месяцев назад +2

    Here's how to do it in code:
    enum { PinA=2, PinB=3, IPINMODE=INPUT };
    static byte abOld; // Initialize state
    volatile int count; // current rotary count
    int old_count; // old rotary count
    void setup() {
    pinMode(PinA, IPINMODE);
    pinMode(PinB, IPINMODE);
    attachInterrupt(0, pinChangeISR, CHANGE); // Set up pin-change interrupts
    attachInterrupt(1, pinChangeISR, CHANGE);
    abOld = count = old_count = 0;
    Serial.begin(115200);
    Serial.println("Starting Rotary Encoder Test");
    }
    // On interrupt, read input pins, compute new state, and adjust count
    void pinChangeISR() {
    enum { upMask = 0x66, downMask = 0x99 };
    byte abNew = (digitalRead(PinA)

    • @CuriousScientist
      @CuriousScientist  10 месяцев назад +1

      Thank you! I pinned this comment so other viewers can see it more easily. I will try this code and then probably make a short clip about it.

  • @Ender_Wiggin
    @Ender_Wiggin 10 месяцев назад +4

    Cool but have to say seems a bit overkill. For normal micro switches you want that level of debounce if you want a fast response but the encoder look quite good. Keep in mind most decent micro controllers already have a schmitt trigger input.

    • @CuriousScientist
      @CuriousScientist  10 месяцев назад +2

      Hi! Thanks! Yes, I know it is a bit of an overkill, I even mentioned this in my article on my website. However, it is a great project (at least for me) to understand the topic. Hopefully, others will also learn from it. I am also aware of the built-in Schmitt triggers, but as I said in the video, sometimes we want to use encoders in a circuit without a microcontroller. Maybe it is more useful there.

  • @ivolol
    @ivolol 10 месяцев назад +1

    The pulses of a two state rotary encoder have a set of valid state transitions. Given current state X,Y the only valid transitions are the previous state and the next, and there are four transitions from the combination of 2 bit states. If you use a very simple state machine algorithm in your MCU, which tracks the state, and at any time it receives interrupts from the encoder, it checks that the new state is either the valid next or previous state (for forwards/backwards) and if and only if it was, then advances in program logic, you already get a very satisfactory response that is 90% cleaned up from the naive algorithm, even with no RC filter etc.
    If you turn the big audio encoder on knobs on a bunch of different brands of cars, and check if they sometimes recedes backwards when you do a fast spin forwards or if it just gets glitchy, you can check if the people making the interface used the naive algorithm or the state machine one :)

    • @CuriousScientist
      @CuriousScientist  10 месяцев назад

      Absolutely! I also mentioned this that probably it is already solvable via code. I just wanted to take the hardware approach to learn more about it. There is a very nice article and discussion on Hackaday around this topic, you've probably already came across it: hackaday.com/2022/04/20/a-rotary-encoder-how-hard-can-it-be/

  • @lancelot4915
    @lancelot4915 6 месяцев назад

    Thanks a lot for the explanation and video. I'm building myself a diy generic sim cockpit and this circuit comes perfect to help solve the bouncing issues i have with the encoders. My trials to solve it by code have been poor on the results. And since i'm making my own pcb, and i have very limited knowledge on electronics, your circuit falls like a gift from heaven!! 🎁

    • @CuriousScientist
      @CuriousScientist  6 месяцев назад

      Hi! It is great to hear that my circuit can help you! I hope you'll have fun with the sim cockpit once it's done with the new encoders.

  • @sebastiannielsen
    @sebastiannielsen 10 месяцев назад

    A cool idea would be to convert this to a UP/DOWN pulse. Like this:
    A + B is connected to D and CLK of a D-flip flop
    A + B is also connected to a XOR gate
    Output of XOR gate, is connected to input A of 2 AND gates.
    Input B of And-gate 1 is connected to Q of D-flipflop.
    Input B of And-gate 2 is connected to /Q of D-flipflop.
    Now you have a up pulse when the encoder is rotated clockwise, and a down pulse when encoder is rotated anticlockwise.
    ----------------------------------------------------------------------------------------------------
    Next addition to the circuit would be following:
    A shifted rotary encoder. By using the push button as shift mechanism.
    This requires 4 more and-gates + 1 NOT gate.
    A + B is connected to D and CLK of a D-flip flop
    A + B is also connected to a XOR gate
    Output of XOR gate, is connected to input A of 2 AND gates.
    Input B is connected to pushbutton output, BUT with a NOT-gate in front of one of the AND gate. Call these AND-gates P and /P.
    Output of P is connected to Input A of 2 AND-gates, call these 1 and 2.
    Output of /P is connected to input A of 2 additional AND-gates, call these 3 and 4.
    Input B of And-gate 1 & 3 is connected to Q of D-flipflop.
    Input B of And-gate 2 % 4 is connected to /Q of D-flipflop.
    Now you have 2 separate UP/DOWN pairs, one UP/DOWN pair that will actuate when you turn rotary encoder, and one UP/DOWN pair that will actuate when you PUSH and TURN the rotary encoder, acting as a shift level.
    Finish with some timer, that will extinguish the signal after half a second or 0.25 second, since some equipment don't like its up or down button being held down.
    After that, you will have a nice "Convert UP/DOWN button to rotary encoder circuit".

    • @CuriousScientist
      @CuriousScientist  10 месяцев назад

      Thanks! I don't know where I could use this circuit, but it definitely sounds interesting. At this point, I am happy with this debounced encoder, I do not want to (and I don't need it) make it more complicated.

  • @Aaron_Dayton
    @Aaron_Dayton 10 месяцев назад

    Good job! This is totally not overkill, I saw some kits on tindie a while back that converted the output to i2c. I really like your approach! minimal parts and great explanation. This kit should be the new standard on parts available on amazon/ebay/everywhere. I wouldn't mind paying a couple more nickels if it didn't mean pulling my hair out trying to troubleshoot these on an esp32. Cheers! Love the project and attention to detail.

    • @CuriousScientist
      @CuriousScientist  10 месяцев назад

      Thank you! I wonder if people will start to adopt this technique, but probably it is "too expensive" on a large (or industrial) scale to add the Schmitt trigger. You can save ~400 USD if you don't use it for every 1000 boards. Also, the double-sided PCB might be challenging to solder. However, I think it is still a cool project for hobby makers. If you end up trying this design for your ESP32 projects, please let me know how it went.