rotary encoder does not decrement

Status
Not open for further replies.

lamikam

Active member
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;
  }
  
}
 
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?

SDS00003.png

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:

ALPS.png

Here is the setup I used for testing:

Setup.jpg
The green wire from the encoder is the integrated push button [not used in this example].


Hope this helps.

Regards,
Paul
 
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:
View attachment 14072
 
Hi Lamikam,

View attachment 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)
 
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:

Capture.PNG

Paul
 
Status
Not open for further replies.
Back
Top