# Used mouse rotary encoder

**URL:** <https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811>\
**Category:** General Electronics\
**Created:** [February 9, 2026, 2:35am UTC](https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811 "2026-02-09T02:35:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gowbow1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gowbow1/32/1059949_2.png) [@gowbow1](https://forum.arduino.cc/u/gowbow1)\
**Post date:** [February 9, 2026, 2:35am UTC](https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811/1 "2026-02-09T02:35:23Z")

</div>

Hi there !

I am currently doing experiments with a salvaged mouse rotary encoder.

the problem i’m facing isn’t from the code itself, ‘cos i tried a bunch of ‘em i’ve found on internet

the problem is, this encoder is quite old, it come from a mouse i’ve been using for a while now, so of course it is used, and the contacts A and B aren’t tight anymore.

here’s an example of code i used to make it work (not my code !) :

```arduino
#define encoder0PinA 2
#define encoder0PinB 3
#define encoder0Btn 4
int encoder0Pos = 0;
void setup() {
  Serial.begin(9600);
  pinMode(encoder0PinA, INPUT_PULLUP);
  pinMode(encoder0PinB, INPUT_PULLUP);
  pinMode(encoder0Btn, INPUT_PULLUP);
  attachInterrupt(0, doEncoder, CHANGE);
}
int valRotary,lastValRotary;
void loop() {
  int btn = digitalRead(encoder0Btn);
  Serial.print(btn);
  Serial.print(" ");
  Serial.print(valRotary);
  if(valRotary>lastValRotary)
  {
	Serial.print(" CW");
  }
  if(valRotary {

	Serial.print(" CCW");
  }
  lastValRotary = valRotary;
  Serial.println(" ");
  delay(250);
}
void doEncoder()
{
  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB))
  {
	encoder0Pos++;
  }
  else
  {
	encoder0Pos--;
  }
  valRotary = encoder0Pos/2.5;
}

```

and using it with a new rotary encoder (not from a mouse), it works very well.

But with the old mouse encoder here’s what happens (on the serial monitor) :

rotating in one way, i get “rotating clockwise”. Good, it works.

now the other way, i get “rotating counter-clockwise” then “clockwise” the “counter..” etc.

does anyone knows if i can compensate in a way or an other the fact the this component is used ? i already tried using a delay, it doesn’t change anything.

---

<div class="post-metadata">

**Author:** ![dilberto](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dilberto/32/1126465_2.png) [@dilberto](https://forum.arduino.cc/u/dilberto)\
**Post date:** [February 9, 2026, 3:41am UTC](https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811/2 "2026-02-09T03:41:18Z")

</div>

Hi, can you please post an oscillogram of the A and B waveforms, both rotating CW and CCW?

---

<div class="post-metadata">

**Author:** ![kmin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kmin/32/1147370_2.png) [@kmin](https://forum.arduino.cc/u/kmin)\
**Post date:** [February 9, 2026, 11:27am UTC](https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811/3 "2026-02-09T11:27:13Z")

</div>

You could clean it with contact cleaner.  
Also add few millisec debounce to your doEncoder.  
And don't use decimal value to scale valRotary (which is int).

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [February 9, 2026, 4:25pm UTC](https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811/4 "2026-02-09T16:25:35Z")

</div>

Hth:  
[https://community.element14.com/members-area/personalblogs/b/blog/posts/rotary-encoders---part-3-capturing-input-on-an-arduino](https://community.element14.com/members-area/personalblogs/b/blog/posts/rotary-encoders---part-3-capturing-input-on-an-arduino)

[https://community.element14.com/members-area/personalblogs/b/blog/posts/rotary-encoders---part-1-electronics](https://community.element14.com/members-area/personalblogs/b/blog/posts/rotary-encoders---part-1-electronics)

---

<div class="post-metadata">

**Author:** ![gowbow1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gowbow1/32/1059949_2.png) [@gowbow1](https://forum.arduino.cc/u/gowbow1)\
**Post date:** [February 10, 2026, 1:07am UTC](https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811/5 "2026-02-10T01:07:58Z")

</div>

hey ! i’ll do that as soon as i can

---

<div class="post-metadata">

**Author:** ![Brazilino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/brazilino/32/1198404_2.png) [@Brazilino](https://forum.arduino.cc/u/Brazilino)\
**Post date:** [February 10, 2026, 11:00am UTC](https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811/6 "2026-02-10T11:00:46Z")

</div>

> [@gowbow1](#):
>
> `attachInterrupt(0, doEncoder, CHANGE);`

Couldn’t it be that int.0 is related to digital pin 2? Is your board an Uno?

---

<div class="post-metadata">

**Author:** ![Rintin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rintin/32/1013044_2.png) [@Rintin](https://forum.arduino.cc/u/Rintin)\
**Post date:** [February 10, 2026, 11:35am UTC](https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811/7 "2026-02-10T11:35:41Z")

</div>

The use of `digitalPinToInterrupt()` is recommend when using `attachInterrupt()`.

---

<div class="post-metadata">

**Author:** ![gowbow1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gowbow1/32/1059949_2.png) [@gowbow1](https://forum.arduino.cc/u/gowbow1)\
**Post date:** [February 10, 2026, 10:05pm UTC](https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811/8 "2026-02-10T22:05:43Z")

</div>

yes it is.

btw here’s the real code i used to make my tests (even though the one i’ve shown in my first post is quite similar) :

```arduino
int pinA = 6;
int pinB = 7;
int encoderPosCount= 0;
int pinAlast;
int aVal;
boolean bCW;

void setup{
  //SET pinA and pinB and input
  pinMode(pinA, INPUT_PULLUP);

  pinMode(pinB, INPUT_PULLUP);
  pinALast = digitalRead(pinA);
  Serial.begin(9600);
  Serial.println("BEGIN");
  Serial.println();
}

void loop()
{
  aVal= digitalRead(pinA);
  if(aVal |=pinAlast)
  {
    if(digitalRead(pinB) != aVal) //rotatingClockwise
    {
      encoderPosCount ++;
      bCW = true;
    }
    else
    {
      bCW = false
      encoderPosCount --;
    }
    if(bCW)
    {
      Serial.println("rotate clockwise");
    }
    else
    {
      Serial.println("rotate counter-clockwise");
    }
    Serial.print("encoder count: ");

    Serial.println(encodePosCount);
    Serial.println();
  }
  pinAlast = aVal;
}

```

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [March 11, 2026, 5:53pm UTC](https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811/9 "2026-03-11T17:53:07Z")

</div>

> [@gowbow1](#):
>
> `if(aVal |=pinAlast)`

Did you intend this to be `aVal |= pinAlast` (OR EQ) or `aVal != pinAlast` (NOT EQ)?

Also... this...

> [@gowbow1](#):
>
> `bCW = false`

needs an ending semi-colon.

And this...

> [@gowbow1](#):
>
> `void setup{`

needs an open/close parenthesis...  
`void setup() {`

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 7, 2026, 5:53pm UTC](https://forum.arduino.cc/t/used-mouse-rotary-encoder/1429811/10 "2026-09-07T17:53:24Z")

</div>

Closed on September 7, 2026, 5:53pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
