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

**URL:** <https://community.particle.io/t/solved-rotary-encoder-using-interrupts-pullups-dont-work/8412>\
**Category:** Troubleshooting\
**Created:** [November 25, 2014, 3:47pm UTC](https://community.particle.io/t/solved-rotary-encoder-using-interrupts-pullups-dont-work/8412 "2014-11-25T15:47:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stahl](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/stahl/32/2665_2.png) [@stahl](https://community.particle.io/u/stahl)\
**Post date:** [November 25, 2014, 3:47pm UTC](https://community.particle.io/t/solved-rotary-encoder-using-interrupts-pullups-dont-work/8412/1 "2014-11-25T15:47:33Z")

</div>

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.

```auto
    //------ 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
    }

```

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [November 25, 2014, 3:53pm UTC](https://community.particle.io/t/solved-rotary-encoder-using-interrupts-pullups-dont-work/8412/2 "2014-11-25T15:53:31Z")

</div>

@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. 😄

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [November 25, 2014, 3:55pm UTC](https://community.particle.io/t/solved-rotary-encoder-using-interrupts-pullups-dont-work/8412/3 "2014-11-25T15:55:33Z")

</div>

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

[Here’s the link](http://docs.spark.io/firmware/#setup-pinmode) to the documentation of what is said above.

---

<div class="post-metadata">

**Author:** ![stahl](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/stahl/32/2665_2.png) [@stahl](https://community.particle.io/u/stahl)\
**Post date:** [November 25, 2014, 4:06pm UTC](https://community.particle.io/t/solved-rotary-encoder-using-interrupts-pullups-dont-work/8412/4 "2014-11-25T16:06:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [November 25, 2014, 4:08pm UTC](https://community.particle.io/t/solved-rotary-encoder-using-interrupts-pullups-dont-work/8412/5 "2014-11-25T16:08:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [May 9, 2018, 5:48am UTC](https://community.particle.io/t/solved-rotary-encoder-using-interrupts-pullups-dont-work/8412/6 "2018-05-09T05:48:41Z")

</div>

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

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.
