Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: rotary encoder does not decrement

  1. #1

    rotary encoder does not decrement

    From existing examples below, If I turn the encoder in either direction, position is always being incremented. Encoder has connection to ground, and the two output pins. Any suggestions?

    Code:
    #include <Encoder.h>
    
    Encoder knobLeft(34, 35);
    
    void setup() {
      Serial.begin(9600);
      Serial.println("TwoKnobs Encoder Test:");
      knobLeft.write(0);
    }
    
    long positionLeft  = -999;
    
    
    void loop() {
      long newLeft;
      newLeft = knobLeft.read();
    
      if (newLeft != positionLeft ) {
        Serial.print(newLeft);
        Serial.println();
        positionLeft = newLeft;
      }
      
    }

  2. #2
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    122
    Hi Lamikam,

    When I tried your code, and rotated the encoder fast, I got the same issue.
    But when I rotated the encoder very slowly [<1 click/sec], the output nicely counted up & down as expected.
    Hooking up the oscilloscope showed a lot of spikes between the clicks of the encoder at fast rotating [sorry, no picture, should have hit the Print button then...].
    That reminded me that the encoder, an ALPS EC11E1544505, is rather bouncy/noisy. Especially when it's been laying around for a few weeks. I guess some kind of corrosion builts up?

    After a minute or 5 of fast rotating the encoder back & forth, the output became more stable [meaning counting reliably] at higher rotation speeds.
    The number of spikes decreased and counting looked OK. Best visible in the yellow trace. I guess rotating the encoder for a longer time cleans the internal contacts?

    Click image for larger version. 

Name:	SDS00003.png 
Views:	15 
Size:	21.3 KB 
ID:	15652

    Anyway, the Encoder library does not seem to like those bouncy/noisy encoders, so I coded my own encoder handler that seems to be more bounce/noise resistant:

    Code:
    #define EncoderPinA 2                    // yellow wire
    #define EncoderPinB 3                    // purple wire
    
    boolean StateA;
    volatile boolean StateB;
    volatile boolean RotationDetected = false;
    int8_t EncoderClicks = 0;
    
    void setup()
    {
      Serial.begin(9600);
      pinMode(EncoderPinA, INPUT_PULLUP);
      pinMode(EncoderPinB, INPUT_PULLUP);
      attachInterrupt(digitalPinToInterrupt(EncoderPinA), EncoderRotated, CHANGE);
    }
    
    void loop()
    {
      if (RotationDetected)                  // Check whether encoder is rotated
      {
        delay(1);                            // Debounce the interrupt pin for 1ms before reading the state
        StateA = digitalRead(EncoderPinA);   // Read interrupt pinA
        if (StateA != StateB)                // Compare the states of both pins
        {
          ++EncoderClicks;                   // CW, increment EncoderClicks by 1
        }
        else
        {
          --EncoderClicks;                   // CCW, decrement EncoderClicks by 1
        }
        RotationDetected = false;            // Clear the flag
        Serial.println(EncoderClicks);
      }
    }
    
    void EncoderRotated()                    // ISR for encoder
    {
      StateB = digitalRead(EncoderPinB);     // read PinB
      RotationDetected = true;               // exit the ISR, further processing by the main loop
    }
    By the way, this is the encoder switching diagram:

    Click image for larger version. 

Name:	ALPS.png 
Views:	29 
Size:	19.6 KB 
ID:	15647

    Here is the setup I used for testing:

    Click image for larger version. 

Name:	Setup.jpg 
Views:	15 
Size:	99.4 KB 
ID:	15648
    The green wire from the encoder is the integrated push button [not used in this example].


    Hope this helps.

    Regards,
    Paul

  3. #3
    Awesome! You are the Man!

  4. #4
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    122
    You're welcome.
    Please let us know how it works out for you.

    Paul

  5. #5
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Location
    Florida
    Posts
    457
    Or you could use an external circuit that greatly reduces the bounce that affects encoders. Iíve had great success using this circuit from Bourns and I havenít missed any steps as far as I can tell:
    Attachment 14072

  6. #6
    Thanks will take a look

  7. #7
    Senior Member
    Join Date
    Feb 2016
    Location
    Australia
    Posts
    250
    Quote Originally Posted by PaulS View Post
    Hi Lamikam,

    Click image for larger version. 

Name:	ALPS.png 
Views:	29 
Size:	19.6 KB 
ID:	15647


    Regards,
    Paul
    I found my detent stability point on the rotary encoder I purchased always gave gnd on a and b channels.

    The pulses would only appear between the indents. I thought maybe this was normal as some sort of mechanical debounce thing.. anyway it made reading the encoder give the same result as the op. (Depending on how fast I’m checking the inputs)
    Can I get confirmation that mine were duds..
    (Asdume I know how to use my scope)

  8. #8
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    122
    You may want to check the datasheet of the particular decoder you used.
    Another version ALPS decoder from the series I used has the following switching diagram:

    Click image for larger version. 

Name:	Capture.PNG 
Views:	7 
Size:	30.8 KB 
ID:	15744

    Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •