🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Rotary encoder on M5Stack - interrupts?

    Lessons and Guides
    rotary encoder interrupt
    3
    4
    11.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      New_Comix
      last edited by

      Hi,

      I am using an M5Stack to develop a WiFi throttle for model trains. All works fine and I can connect and drive a locomotive whilst being connected to a Märklin CS3.

      However I am struggling to use a rotary encoder to control the speed. My current code is not using interrupts and struggling with bouncing. I am trying to use interrupts, but to me it looks like those are not recognized.

      I suspect I am not using the right ports. Any hint would be welcome.

      Many thanks in advance

      Jörg

      m5-docsM 1 Reply Last reply Reply Quote 0
      • m5-docsM
        m5-docs @New_Comix
        last edited by

        @new_comix
        Hello, specific description please.

        M5Stack documentation URL

        https://docs.m5stack.com

        1 Reply Last reply Reply Quote 0
        • lukasmaximusL
          lukasmaximus
          last edited by

          Sounds like a cool project Jorg. As @watson asked please provide us with more information so we can help you with the issue.

          Specifically what programming environment are you using? Arduino, Micropython, Uiflow?

          Please provide us with the code if you don't mind sharing it

          A schematic, or if its simply hooked up to the pins of the m5stack without any other components, please tell us the pins you used. Perhaps including the model of the encoder you are using could also help to

          Thanks

          Lukas

          1 Reply Last reply Reply Quote 0
          • N
            New_Comix
            last edited by

            Hi,

            sorry about the delay in answering, but I have been really busy with my real life job.

            The rotary encoder (ALPS STEC12E07) is connected to pins 2 and 3 of the M5Stack and GND.

            I also have used pins 22 and 23 in the past, but those can crash the app as soon as you are using the TFT of the M5Stack (looked into the header files of M5Stack and found that pin 22 is connected to the TFT).

            I am using the following code, which you can find in variations on the web:

            /* Read Quadrature Encoder
            Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.

            Sketch by max wolf / www.meso.net
            v. 0.1 - very basic functions - mw 20061220

            */

            int encoder0PinA = 2;
            int encoder0PinB = 3;

            int encoderPos = 0;
            int lastReportedPos = 0;

            int aValue = LOW;
            int bValue = LOW;
            int aValueLast = LOW;

            void setup() {
            pinMode (encoder0PinA, INPUT);
            pinMode (encoder0PinB, INPUT);
            digitalWrite(encoder0PinA, LOW);
            digitalWrite(encoder0PinB, LOW);

            encoderPos = 2000;

            Serial.begin (115200);

            }

            void loop() {

            delay(5);

            aValue = digitalRead(encoder0PinA);
            bValue = digitalRead(encoder0PinB);

            if ((aValue != aValueLast) && (aValue == LOW)) { //knob rotated, when aValues changes, BUT use only if aValue is LOW

            if (bValue == LOW) {
              encoderPos++;
            } else {
              encoderPos--;
            }
            

            }

            aValueLast = aValue;

            if(lastReportedPos != encoderPos)
            {

             Serial.print("Position: ");
             Serial.println(encoderPos, DEC);
            
              lastReportedPos = encoderPos;
            

            }

            The code works from time to time, but its not reliable on the M5Stack. Tomorrow I will buy another rotary encoder just to exclude any hardware issues.

            Kind regards
            Jörg

            1 Reply Last reply Reply Quote 0
            • First post
              Last post