Rotary Encoder with Nano gives same result irrespective of direction turned

I have a rotary encoder connected to my Nano v3.0 and I have written a simple sketch to read the input of the encoder. My goal is to have the ‘count’ increase/decrease when the encoder is turned clockwise/counterclockwise.

In its current form, I can only get the ‘count’ to move in one direction, irrespective of the direction I turn the encoder shaft.

I have attached a schematic of the circuit setup, which includes capacitors on the encoder outputs in an attempt to prevent switch debounce.

The sketch is as follows:

//pin 2 of nano has to be used for interrupt

int encoderPinA = 2;														
int encoderPinB = 4;

// volatile variables for use in ISR

volatile int count = 100;
volatile int newCount = 100; 


void setup() {
  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  //enable pull-up resistors on inputs to prevent fluctuations 
  digitalWrite(encoderPinA, HIGH);              							
  digitalWrite(encoderPinB, HIGH);											
  attachInterrupt(digitalPinToInterrupt(encoderPinA), turn_ISR, FALLING); 
  Serial.begin(9600);  
}

void loop () {
  IF (newCount != count) {
    Serial.print("Count: ");
    Serial.println(newCount);
    count = newCount;
}

void turn_ISR() {
  IF (encoderPinB == LOW) {    		//cw turn
    newCount = count + 1;
  } ELSE {						//ccw turn
    newCount = count - 1;  
  }
}

RotaryEncoderWaveform.gif

5bb85c91efedbda5e76c3ea13e56958d651a1f59.gif2950bfe28cb0460eca17bcf150ef64da4cbef821.jpg

Using an interrupt to read a rotary encoder! :astonished: :astonished:

What should i do then?? I took the following from the arduino official documentation
" Good tasks for using an interrupt may include reading a rotary encoder, or monitoring user input"

Some hints as to the way to resolve would have been useful

olliejspurway:
What should i do then?? I took the following from the arduino official documentation
" Good tasks for using an interrupt may include reading a rotary encoder, or monitoring user input"

There's the problem for a start - reading the "Arduino official documentation"! :grinning:

//pin 2 of nano has to be used for interrupt

int encoderPinA = 2; 
int encoderPinB = 4;

// volatile variables for use in ISR

volatile int count = 100;
volatile int newCount = 100;


void setup() {
  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  //enable pull-up resistors on inputs to prevent fluctuations
  digitalWrite(encoderPinA, HIGH);               
  digitalWrite(encoderPinB, HIGH); 
  attachInterrupt(digitalPinToInterrupt(encoderPinA), turn_ISR, FALLING);
  Serial.begin(9600); 
}

void loop () {
  IF (newCount != count) {
    Serial.print("Count: ");
    Serial.println(newCount);
    count = newCount;
}

void turn_ISR() {
  IF (digitalRead(encoderPinB) == LOW) {     //cw turn
    newCount = count + 1;
  } ELSE { //ccw turn
    newCount = count - 1; 
  }
}

Why do interrupts not work for this application Paul - Logically it can't see why it shouldn't work - what am I missing?

There's a difference between "work" and "work properly". I explained it in another thread.

Did you try my code correction?

IF (encoderPinB == LOW) {

What is IF? I thought the keyword in question was if. Can you write it all uppercase like that and not get a compiler error?

Also, encoderPinB is defined as 4. LOW is defined as 0. The two will NEVER be equal.

http://www.gammon.com.au/tips
See Trap #2.

Delta_G:
Also, encoderPinB is defined as 4. LOW is defined as 0. The two will NEVER be equal.

Did you examine my code correction?

Hi,
Have you actually measured the 5v and 0v levels at the output, what is the spec of the encoder.
Some encoders need pull-up resistors on their outputs to give proper output levels.
I see no 5v supply to the rotary encoder.

Tom… :slight_smile:

TomGeorge:
I see no 5v supply to the rotary encoder.

I do!

 //enable pull-up resistors on inputs to prevent fluctuations
  digitalWrite(encoderPinA, HIGH);               
  digitalWrite(encoderPinB, HIGH);

Hi,

 //enable pull-up resistors on inputs to prevent fluctuations
  digitalWrite(encoderPinA, HIGH);  
  digitalWrite(encoderPinB, HIGH);

That's OK for the output devices of the encoder, what about the rest of the internal circuitry?
As I have said before, I prefer to see pull-ups externally.
What value of pull up does the encoder spec recommend.

Tom.... :slight_smile:

TomGeorge:
That’s OK for the output devices of the encoder, what about the rest of the internal circuitry?

Internal circuitry”? :astonished:

My betting is that he is talking about one of these:

.

Now, these modules do appear to have pull-ups on the board.

.

Hi,
Post #8

Have you actually measured the 5v and 0v levels at the output, what is the spec of the encoder.

Still waiting…
Tom… :slight_smile: