Problem with rotary encoder

Hey guys, I just started learning about the arduino and got me an arduino nano with some additional parts.
Already got Ds18b20 working together with an oled display.
The next thing I wanted to work with is an rotary encoder, but this one gives me a hard time.

The problem is, no matter what library I try, it just counts up in both directions..
The encoder has 5 pins ( gnd, +, SW, DT, CLK) and I hooked up gnd to ground, + to 5v, sw to pin d4, dt to pin d3 and I did not use CLK, because I don't want to use the push funktion of it.

Do you guys have any idea what the problem is?
Thanks alot

1stkeks:
Hey guys, I just started learning about the arduino and got me an arduino nano with some additional parts.
Already got Ds18b20 working together with an oled display.
The next thing I wanted to work with is an rotary encoder, but this one gives me a hard time.

The problem is, no matter what library I try, it just counts up in both directions..
The encoder has 5 pins ( gnd, +, SW, DT, CLK) and I hooked up gnd to ground, + to 5v, sw to pin d4, dt to pin d3 and I did not use CLK, because I don't want to use the push funktion of it.

Do you guys have any idea what the problem is?
Thanks alot

Rotary encoders I am familiar with use the phase relationship between two outputs, Usually labeled 'A' and 'B'.

Your encoder is using DT, and CLK, (I am guessing, but is DT direction and CLK the pulse?) You are going to have to decode what these signals produce.

On a normal 'A,B' encoder, the relationship between 'A' and 'B' is what identifies the direction.

I write my code like this:

#define APIN 2 // arduino pin attached to the 'A' encoder output
#define BPIN 3 // Arduino Pin attached to the 'B' encoder output
#define RANGE 32 // resolution of encoder for one revolution

volatile bool aState;     // last state of 'A' input, either High or Low
volatile bool bState;    // last state of 'B' input, either High or Low
volatile word position;  // current rotation position, between 0 and (range-1)

void aISR(){
aState = digitalRead(APIN);
if(aState==bState) { // 'A' is following 'B', therefore CCW rotation 
  position = (position + RANGE -1); // decriment, account for underflow
  }
else position = position + 1; // increment 

position = position%RANGE; // limit to 0..(range-1)
} 

void bISR(){
bState = digitalRead(BPIN);
if(aState==bState) { // 'B' is following 'A', therefore CW rotation 
  position = position + 1; // increment
  }
else position = position + RANGE -1; // Decremint, handle underflow 

position = position%RANGE; // limit to 0..(range-1)
} 

void setup(){
pinMode(APIN,INPUT);
pinMode(BPIN,INPUT);
attachInterrupt(digitalPinToInterrupt(APIN), aISR, FALLING); 
attachInterrupt(digitalPinToInterrupt(BPIN), bISR, FALLING);
}

Position is the absolute position reported by the encoder. if you wanted, you could add a volatile boolean to both ISR's to record the last direction.

Chuck.

Hi,
after reading your answer and reading my request again, I noticed I wrote my SW and DT are connected to the arduino and NOT the CLK. I will check that when I am at home in a few hours, because it seems, I just mixed up CLK with SW...

I'll report back later :slight_smile: