How to use Mouse Scroll Wheel Encoder with Arduino

Posted on by

Rotatory encoder of Mouse wheel

The scroll of a mouse works using a rotary encoder to detect the direction and movement of the wheel so that it helps us better handle the interface of different programs such as web browsers, PDF readers, text editors, etc. .

But a rotary encoder has its advantages and disadvantages regarding using a potentiometer with Arduino as we will see in the following tutorial.

 

A rotary encoder, also called an axis encoder or pulse generator, is an electromechanical device used to convert the angular position of an axis to a digital code, which makes it a class of transducer.

How does it work

If we do not have rotary encoder spec sheet, we can use this circuit with LED’s to find out the sequence of codes that we will need later.

Mouse wheel Rotary Encoder Test with LEDs

As we turn the wheel we will see the sequence of the LEDs on and off, being two LEDs we will translate it into binary code so that the sequence will be 01-> 11-> 10-> 00-> 01 and in the opposite direction 01-> 00-> 10-> 11-> 01. With this we can now connect to Arduino knowing the sequence from which we can extract the number of steps and the direction of rotation.

Conexión con Arduino

Rotary Encoder connection with Arduino.

We connect the two digital outputs to pins 6 and 7 of Arduino. We will constantly read these pins in the code and will form the State variable with these two values. We will check if the value of State changes with respect to the previous measure, if there is no change, the wheel has not been moved, if it is different, we will determine the direction of change (variable cw) and add or subtract in the variable Steps in relation to that direction. As we checked before the sequence of states is the following 1 <-> 3 <-> 2 <-> 0 <-> 1.

byte AInput = 6;
byte BInput = 7;

byte lastState = 0;
byte steps = 0;
int  cw = 0;
byte AState = 0;
byte BState = 0;
byte State = 0;

void setup() {
  Serial.begin(9600);
  pinMode(AInput, INPUT);
  pinMode(BInput, INPUT);
}

void loop() {
  // read the input pin:
  AState = digitalRead(AInput);
  BState = digitalRead(BInput) << 1;
  State = AState | BState;

  if (lastState != State){
    switch (State) {
      case 0:
        if (lastState == 2){
          steps++;
          cw = 1;
        }
        else if(lastState == 1){
          steps--;
          cw = -1;
        }
        break;
      case 1:
        if (lastState == 0){
          steps++;
          cw = 1;
        }
        else if(lastState == 3){
          steps--;
          cw = -1;
        }
        break;
      case 2:
        if (lastState == 3){
          steps++;
          cw = 1;
        }
        else if(lastState == 0){
          steps--;
          cw = -1;
        }
        break;
      case 3:
        if (lastState == 1){
          steps++;
          cw = 1;
        }
        else if(lastState == 2){
          steps--;
          cw = -1;
        }
        break;
    }
  }

  lastState = State;
  Serial.print(State);
  Serial.print("\t");
  Serial.print(cw);
  Serial.print("\t");
  Serial.println(steps);
  delay(1);
}

To see these values in an alternative way to the monitor of the serial port we can check it with the plotter. To use it correctly we will have to print the variables in a single line separating the variables with \t so that the plotter identifies them as different variables and shows them separately.

 Serial.print(State);
 Serial.print("\t");
 Serial.print(cw);
 Serial.print("\t");
 Serial.println(steps);
Comments are disabled