Help with rotary encoders

Hello!
I'm using rotary encoders in my arduino project and I have some questions.
I noticed that rotary encoders have 5 pins (A B COM VCC SW) but in PC mouses there are only 3 pins (A B COM). I know that SW is for the push button so mouses don't need it. But why is there a VCC? I wired the mouse encoder as COM to GND, A and B to digital inputs and to 5V with a 10k resistances. Why is it separated on the 5 pin one?

Furthermore, in the mouse encoder, there is a physical object which is making that clicking noise, and with the proper code, 1 step is 1 click. At the 5 pin encoder which can rotate infinitly, what is defining what is 1 step exactly? Maybe, with a 3D printed knob, is it possible to make it clicky? I want it becuse the physical steps are too small for the mouse encoder.

Thanks for the answers!

There are many encoders. Post links to the ones you have.

There are many types of rotary encoders so one answer does not fit all. What exact encoder do you have? Post a link to where you got it or technical data for the encoder.

I have this 3 pin mouse encoder:
https://www.google.com/search?q=inside+of+mouse+encoder&client=ms-android-huawei-rev1&prmd=ivsn&sxsrf=ALiCzsZabwzpb02MznG239HZbUW2-i9RdA:1661016398288&source=lnms&tbm=isch&sa=X&ved=2ahUKEwiL74qj-NX5AhXMxqQKHQgAA0IQ_AUoAXoECAIQAQ&biw=424&bih=752&dpr=2.55#imgrc=ugkSi5av-gnFJM&imgdii=APBgrZs4TKq2zM

I don't have the 5 pin encoder but I will buy it if it is good for my project:
https://www.google.com/search?q=rotary+encoder+pinout&oq=rotar&aqs=chrome.0.69i59l2j69i57j69i60j0i512j69i65j46i199i465i512j0i512l3.4158j0j9&client=ms-android-huawei-rev1&sourceid=chrome-mobile&ie=UTF-8#imgrc=rfrN_4d8r_hJgM

Encoders likely provide a kind of "home signal", a zero position. The mouse system doesn't use that. Look at the computer screen when it's just turned on. The cursor/mouse pointer can be just any ware. You move it into controlled areas.

1 Like

image

That type of rotary encoder is basically 3 switches. 1 on the push switch and 1 each on the A and B outputs. Your mouse encoder uses a slotted optical encoder so is very different. So you wire the encoder as you would switches with pullup resistors on the outputs. On a bare encoder, like above, I use the internal pullups in the processor. The encoders on the small PC boards have the pullup resistors on that small board so needs to have Vcc for those pullups.

Some reading: https://howtomechatronics.com/tutorials/arduino/rotary-encoder-works-use-arduino/

I like this library for rotary encoders.

1 Like

Encoder
Consider adding these two capacitors here. I used surface mount 0.1uF to almost eliminate switch bounce.

Also here is the simplest encoder code I use all the time in my projects.

#define ClockPin 2 // Must be pin 2 
#define DataPin 3 // Must be pin 3
#define readA bitRead(PIND,2)//faster than digitalRead()  (((value) >> (bit)) & 0x01)
#define readB bitRead(PIND,3)//faster than digitalRead()  (((value) >> (bit)) & 0x01)
volatile long count = 0;
long lastCtr;
unsigned long SpareCycles;

void Encoder(bool A) {
  (readB == A)  ? count++ : count--;
}

void setup() {
  Serial.begin(115200); //115200
  pinMode(ClockPin, INPUT);
  pinMode(DataPin, INPUT);
  /* 1 Step */
  attachInterrupt(digitalPinToInterrupt(ClockPin), [] {Encoder( true);}, RISING);
  /**/

  /*  2 step count
  attachInterrupt(digitalPinToInterrupt(ClockPin), [] {Encoder( readB);}, CHANGE);
  */
  
  /* Full 4 Step Count
  attachInterrupt(digitalPinToInterrupt(ClockPin), [] {Encoder( readB);}, CHANGE);
  attachInterrupt(digitalPinToInterrupt(DataPin ), [] {Encoder( !readA);}, CHANGE);
  */
}

void loop() {
  long Counter;
  SpareCycles++;
  noInterrupts ();
  Counter = count;
  interrupts ();
  if ((lastCtr != Counter) & (SpareCycles > 10)) {
    Serial.println(Counter);
    SpareCycles = 0;
  }
  lastCtr = Counter;
}

Z

1 Like

+1 for the caps.

1 Like

+1 again for using caps!

@delta_fox - your links dont work. We need to know the specific item you are using, not a google search for mouse encoders!

1 Like

I cannot find the exact product number, but I have a kaihl mouse encoder like this:

And I'm looking for one of these:

So if I understand correctly, that plate with holes determine what is a full step, and the physical design making the clicks?

The breakout board attached to the encoder only adds 10K pull-up resistors connected from Out A, another from Out B, and a third from Switch each connected to VCC otherwise the encoder is the same without the breakout. If you provide your own "Pull-Up" power to Out A, Out B, and Switch, the encoder and Switch will short them to the ground as it rotates giving you the pulses needed.
Z

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