[Solved] Rotary encoder using interrupts (Pullups don't work)

Hi there!

I can’t get a rotary encoder to work using the following code. The code is working OK on arduino.
Any help appreciated.

    //------ Encoder Variables
    int encoderPinA = D2;
    int encoderPinB = D3;
    int pushButton = D4;
    
    volatile int lastEncoded = 0;
    volatile long encoderValue = 0;
    long lastencoderValue = 0;
    int lastMSB = 0;
    int lastLSB = 0;
    
    //----- OUTPUTS
    int led1 = D0;
    int led2 = D1;
    
    bool state = HIGH;
    
    void setup() {
        // Initialize D0 + D1 pins as output for the two LEDs
    	pinMode(led1, OUTPUT);
    	pinMode(led2, OUTPUT);
    	
    	// Initialize Encoder
    	pinMode(encoderPinA, INPUT); 
    	pinMode(encoderPinB, INPUT); 
    	pinMode(pushButton, INPUT);
    
    
    	digitalWrite(encoderPinA, HIGH);	// turn on pullup resistor
    	digitalWrite(encoderPinB, HIGH);	// turn on pullup resistor
    	digitalWrite(pushButton, HIGH);		// turn pullup resistor on
    
    	// attach interrupts
    	attachInterrupt(encoderPinA, updateEncoder, CHANGE);
    	attachInterrupt(encoderPinB, updateEncoder, CHANGE);
    	
    	Serial.begin(9600);
    	
    	
    
    }
    
    void loop() {
        delay(50);
        state = !state;
        digitalWrite(led1, state);
        Serial.println(encoderValue);
    }
    
    void updateEncoder() {
    	int MSB = digitalRead(encoderPinA); //MSB = most significant bit
    	int LSB = digitalRead(encoderPinB); //LSB = least significant bit
    	
    	int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
    	int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
    
    	if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
    	if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
    
    	lastEncoded = encoded; //store this value for next time
    }

@stahl, the Core works differently than an Arduino for enabling pull-ups on inputs. Change your pinMode statements to:

	// Initialize Encoder
	pinMode(encoderPinA, INPUT_PULLUP); 
	pinMode(encoderPinB, INPUT_PULLUP); 
	pinMode(pushButton, INPUT_PULLUP);

and remove the digitialWrite () lines:

	digitalWrite(encoderPinA, HIGH);	// turn on pullup resistor
	digitalWrite(encoderPinB, HIGH);	// turn on pullup resistor
	digitalWrite(pushButton, HIGH);		// turn pullup resistor on

Another thing to note is that “int” on the core is 4 bytes whereas it is only 2 bytes on the Arduino. You could use int16_t instead to define a 2 byte integer. :smile:

I give up; @peekay123, you’re waaaaay too fast… :wink:

Here’s the link to the documentation of what is said above.

THX for the ultra fast reply! it’s working now :wink:

I’m going to mark this as solved but if you end up having additional problems feel free to respond to this thread and I’ll un mark it accordingly.

Just popping in much later to say this helped me on a project, thanks all! :slight_smile:

I did use input pullup and such, found the rotary encoder really needed the core pin on ground after all, the pulldown pin couldn’t sink enough current.