Blog  /  Mouse Rotary Encoder: The Working Principle and How to Connect It With the Arduino

Mouse Rotary Encoder: The Working Principle and How to Connect It With the Arduino

Mouse rotary mechanism

Mouse rotary mechanism

Source: Wikimedia Commons


Do you deal with applications like printers, USB devices, mechanical encoders, or CNC machines with PCB assembly? Then, you need a device that will deliver fine digital control.    That said, the mouse rotary encoder is the ideal device for the job as the sensor helps you convert the oscillation of a knob to an output signal. And the output signal also allows you to know the direction of the rotating knob or mouse wheel encoder.       

Also, we’ve two major types of mouse rotary encoders: incremental and absolute. The absolute shows you the number of increments as the shaft or encoder rotary moves. On the other hand, the absolute tells you the knob’s precise position in degrees.      

That’s not all. We’ll give you a detailed breakdown of the device, how it works, its digital code, circuit diagram, conductive encoders, and more.    

Let’s dive in!       


What Is a Mouse Encoder?


Mouse rotary wheel encoder

Mouse rotary wheel encoder

Source: Wikimedia Commons


The mouse encoder falls under the category of the mechanical incremental rotary encoder. And it comes with feedback data of two things: the rotary speed and direction. Furthermore, the mouse encoders help to convert linear or rotary motion into digital signals.

Also, the device has a standard Grove interface and encoder compatibility. In other words, the mouse encoder features a standardized connector prototyping system that eases connection. Hence, the device saves you the stress of excess wiring and programming.

Plus, it adjusts easily to a harsh and heavy-duty environment. Most times, manufacturers design the rotation speed in radian per minute. And the value is usually less than 1000. So, you can apply the mouse encoder in printers, robots, toys, and other consumer input devices.

Further, this absolute encoder is a reliable device with detent and a nice feel.


Applications of the Mouse Rotary Encoder


The mouse encoder is pretty valuable for applications like:




  • The device has low-cost feedback.
  • It is compact in size.
  • You can use mouse encoders in existing applications
  • The device comes with a high resolution
  • It’s versatile for different conditions
  • Mouse encoders are pretty reliable and precise
  • It combines digital and optical technology
  • The device has integrated electronics




  • The mouse encoder is prone to dust, oil, and dirt contaminates


How Does a Mouse Rotary Encoder Work?


Mouse encoder circuit

Mouse encoder circuit

Source: Wikimedia Commons


When you look at the inner part of the mouse rotary encoder with a void loop, you’ll notice a slotted disk. And the disk connects to optical type pins: contact pin A, contact pin B,  and ground pin C.

So, based on the direction you rotate the knob and encoder wheels, the contact pin A and contact pin B will connect to the common ground pin C. When this happens, the encoder wheel holder produces signals. Then, the quadrature encoding takes place.

Quadrature encoding is when the signals produce an “out of phase” shift (at 900). And they do this concurrently—while the pins interact.

So, when you rotate the knob in a counterclockwise direction, the B pin connects initially, then the A pin follows. But, if you twist the knob in a clockwise direction, the opposite will happen. That is, A will connect first, and B pin follows.

Thus, if you want to know the direction of the rotating knob device type, track it when the pins link and detach off the ground. So, all you need to do is understand the direction of motion or form of movement. How? Take note of the B pin when the A pin switches up.


Mouse Rotary Encoder Circuit Diagram

Mouse Rotary Encoder Circuit Diagram

Source: Wikimedia Commons


The Mouse Rotary Encoder Pinout


To better understand how the mouse rotary encoder works, you must identify the various contact pattern pinouts. So, here are the pinouts listed in no particular order:

CLK (Output A) refers to the primary output pulse, and it helps you know the number of rotations. Hence, any time you turn the knob by a click in a direction (clockwise or counterclockwise), the CLK output experiences a cycle of going HIGH  and LOW.

GND—refers to the Ground connection.

SW—this pinout is the most vital truncated push-button switch. So, the voltage reduces or goes LOW when you push the knob. 

DT (Output B)—this pinout is similar to the CLK, and the major difference is that the DT delays the CLK within a 90 degrees phase shift. So, you can use this pinout to know the oscillation’s direction. 

VCC—this pinout has a value of 3.3 or 5V. And it’s the positive supply voltage.


How Do You Wire the Mouse Rotary Encoder to Arduino?


First off, begin with attaching the module’s positive pin to the Arduino’s 5V. Then, put the GND to the ground. 

With this, you can link the DT pins and pin 3 together and join the CLK to pin 2. Afterward, join the digital pin 4 and the SW pin together. 

Now that all the connections are intact, create a sketch that will make the setup work. And the function of the sketch is to let you know when the mouse encoder is rotating. 

Below is an excellent example of what your sketch should look like: 

If all goes well, your serial monitor should display this output:

If the output doesn’t look like the image above, try changing the DT and CLK lines. 


What Does the Code Mean?


First off, the code says that Arduino pins connect to the rotary encoder’s pins. Then, the following line defines some integers. And the variable “counter” represents the number of counts changing each time the knob turns in a click.

The other variables, “lateStateCLK” and “currentStateCLK,” grip the CLK output’s state. Plus, it helps you know the number of available rotations. “currentDir” is a string that comes in handy when you need the sequent monitor to print the latest rotation’s direction. “last button press'' is a variable that helps you with switch debounce. 

As for the Setup part, we represented the encoder connections as inputs. With that, we allowed the input pull-up resistor on the SW pin. Plus, we structured the sequent monitor and got the latest CLK pin value. Then, we stored the CLK pin value as a variable (lastStateCLK). 

If there was a pulse when the knob turned, the CLK and “lastStateCLK” values are different in the loop section. Also, we have to confirm that “currentStateCLK” is one—to prevent double count. 

To know the rotation’s direction, compare the latest CLK pin with the DT pin on the encoder module. If you see different values, it means the knob turned counterclockwise. 

Then, we reduced the set and counter “currentDir” to “CCW.” But if the values are similar, the knob turns clockwise—so you can increase the set and counter “currentDir'' to “CW.” 


How Do You Use the Mouse Scroll Wheel Encoder With Arduino?


To do this, you have to link two digital outputs to the Arduino (pin 6 and 7). Then, you can read the pins in the code. That way, you’ll form the state variable with the two values.

With this, you can confirm if the value of the State changes compared to the initial measure. If the value remains the same, the wheel doesn't move. But, if the value changes, you need to know the direction of change with the variable “CW.” That way, you can subtract or add where necessary concerning the course. 

Initially, we noticed that the State had this sequence: 1<->3<->2<->0<->1. 


Initially, we noticed that the State had this sequence: 1<->3<->2<->0<->1.

Mouse Rotary Encoder--Initially, we noticed that the State had this sequence: 13201.

Initially, we noticed that the State had this sequence: 1<->3<->2<->0<->1.


However, if you want the values to appear opposite on the serial port, use the plotter for confirmation. And you can do this by separating the variables with "/t" to print in one line. That way, it’s easy for the plotter to spot the different variables and display them independently. 


Mouse Rotary Encoder


Final Words


The mouse rotary encoder (MRE) is a mechanical device that is resourceful for different applications ranging from robotics to consumer input devices. Plus, it adapts effortlessly to a heavy-duty and harsh environment. And it has a standard Grove interface that eases wiring and programming.

Overall, the devices’ working principle isn’t so difficult to understand. But if you need more help, feel free to contact us.