Mouse Encoder Hack – ElectroSchematics.com

I want to write about something a little different this time. I removed the mechanical scroll wheel rotary encoder from a discarded optical mouse and used it in a little project. I really liked the result!

The story of the midnight hour hack

I ordered a Grove-Mouse Encoder more than two months ago from China, but it wasn’t delivered in time (later got refunded).

My intention was to use it in a simple dc halogen lamp dimmer project for someone close to me. To make him happy I salvaged one from an old optical mouse I had. This was another midnight project.

This is a quick view of the circuit board of my old Chinese usb optical mouse. As you can see, there’s a small (9mm) mouse scroll wheel rotary encoder and a single chip optical mouse sensor – MX8733.

This is the schematic of the optical mouse, provided by the maker of the MX8733 chip – Shenzhen LIZE Electronic Technology Co., Ltd (http://www.lizhiic.com).

As usual, the rotary encoder has three pins. The ‘data’ pins (AB) are wired directly to pins 1 and 8 of the MX8733 chip. I lifted off the rotary encoder from the circuit board as it was the only thing needed for my project. Below you can see the pinout drawing of that 24-step, 9mm mouse scroll wheel rotary encoder.

As shown above, the rotary encoder has two data pins (AB) and a common pin (C). Basically a rotary encoder has two outputs, A and B, that are offset in time as the shaft rotates, so the microprocessor can determine the direction and amount of rotation applied to the encoder. The below figure depicts the anticipated voltage wave forms as the rotary encoder is turned.

An in-depth description of the theory of quadrature encoding is certainly beyond the scope of this post. You may go to related primers and projects published elsewhere in this website.

Back to my story, I rigged up a small ‘test circuit’ on a mini breadboard powered by a regulated 5VDC source. See my test setup:

The circuit outputs are ‘low’ as default ie both LEDs (LED1 and LED2) are in off state. When the shaft/knob is turned, there will be pulses on the outputs (JP-A and JP-B), depending on the direction the rotary encoder is turned. This is the circuit diagram of my quick test setup with two 5mm low-current amber LEDs as visual indicators. The ‘low’ outputs are 0V here, and the ‘high’ outputs are around 1.8V (actually VF of the amber LEDs in use).

To be of more use, the phase shift encoded signals can be translated into two logic signals eg UP and DOWN. If you turn the wheel clockwise, the UP signal is registered on each step. If counterclockwise, the DOWN signal is registered at each step. Luckily, using the mouse scroll wheel encoder with an Arduino Uno proved to be easy after I made a slight modification (see below).

Here, JP-A and JP-B are renamed as D11 and D12 respectively. You can see a new LED (LED3) linked to another circuit point labelled as D9. The intended application is the main tuning/dimmer knob of a halogen lamp dimmer. The following session describes how I employed this salvaged mouse scroll wheel encoder in the ‘blueprint’ of my halogen lamp dimmer project. The previous LED3 (with a series resistor) is used for quick testing/debugging of the prototype in lieu of the real dc halogen bulb (and its driver circuitry). LED3 is in fact a 5mm ultrabright water clear red LED originally used as the light source in another defunct optical mouse ie like the part marked as D2 in the MX8733 schematic depicted earlier.

The wiring is an easy job. Simply route the two data pins (A&B) of the encoder to pins D11 and D12 of Arduino Uno. Arduino’s GND rail is common to the rotary encoder’s common pin (C) and the cathode of LED 3, while the 5V rail is used to pull-up the data pins (A & B). Arduino’s D9 pin controls LED3. During power on, the LED lights up at near 50% brightness (as defined in the code). Below you can see the PWM waveform available at D9 on initial set off.

Now you can regulate its brightness from around 4% to 98% by turning the encoder wheel/knob clockwise (up) and counter clockwise (down). As you turn the encoder, the rotary sequence will be translated into binary code, from which Arduino can evoke the number of steps and the direction of rotation.

The Arduino Sketch (code) for the project is included below. Hopefully, this code should be easy enough to grasp and polish!

[code]


/* Mouse Encoder & LED PWM Dimmer :: Arduino Sketch v1 */


int brightness = 120;    // Initial Lamp Brightness ~50%

int fadeAmount = 10;   

unsigned long currentTime;

unsigned long loopTime;

const int enc_A = 11;  // D11 Encoder A I/P

const int enc_B = 12;  // D12 Encoder B I/P

unsigned char encoder_A;

unsigned char encoder_B;

unsigned char encoder_A_prev = 0;




void setup()  {


  pinMode(9, OUTPUT); // D9 Lamp Drive O/P

  pinMode(enc_A, INPUT);

  pinMode(enc_B, INPUT);

  currentTime = millis();

  loopTime = currentTime;

}


void loop()  {


  currentTime = millis();

  if (currentTime >= (loopTime + 5)) {


    encoder_A = digitalRead(enc_A);

    encoder_B = digitalRead(enc_B);

    if ((!encoder_A) && (encoder_A_prev)) {


      if (encoder_B) {


        if (brightness + fadeAmount <= 255) brightness += fadeAmount;

      }

      else {


        if (brightness - fadeAmount >= 0) brightness -= fadeAmount;

      }


    }

    encoder_A_prev = encoder_A;


    analogWrite(9, brightness);

    loopTime = currentTime;

  }

}

[/code]

These are the breadboard snaps from my workbench:

This was not my most beautiful build.

One thing to note – cheap mouse encoders are known to cause a lot of contact bounce so you will need to debounce the signal from the encoders. I have tried some variants for C1-C2, and settled with 2.2uF (a bit higher than the norm) electrolytic capacitors for my specific mouse encoder.

Be prepared for a lazy web walk if you decide to build a rotary encoder-controlled pulse width modulated lamp dimmer!

Next in line…

Long ago I laid my hands on a really old optical mouse sensor ADNS-2610, but sadly I quit that experiment (https://www.electroschematics.com/optical-mouse-learn-to-hack/) halfway with no headway . I still want to work with that sensor, and now I’ve a comparatively new optical mouse sensor to play with. The MX8733 possesses an internal switch for USB DP/DM and PS2 CLK/Data I/O. Its circuitry hardly needs an external crystal. There’s not much to it – almost all optical mouse sensors have a rather simple serial interface (clock and data) suitable for linking to a microcontroller like Arduino. Anyway, tutorials available online are rather scarce!

There is not much to do with a common optical mouse sensor, but I see no reason to snub the robust ‘mouse brain’. However, this time I’m not going to get into the stream of my random project ideas but at some point, I’ll share a few of them here.

BTW, I’ve found a couple of optical mouse sensor hacks across the web, and came across a small Arduino code module that reads the delta-X and delta-Y values ​​from the old MCS12085 optical (PS/2) mouse sensor as it move. More details of the project are in this link https://blog.jgc.org/2012/09/conversion-of-cheap-optical-mouse-to.html. The datasheet of MX8733 promises that it supports PS/2 interface as well. I have not been able to make it to work with my Arduino Uno (loaded with the aforesaid code) so far! How can I switch MX8733 from its default USB mode to the requisite PS/2 mode? Please share your thoughts!

Lastly, I admit I just used this space to share a crude idea on employing a cheap mouse encoder in an Arduino PWM light dimmer project.

Addendum:

In my halogen lamp dimmer project discussed here, I’ve used a 5W/12V halogen lamp. To drive the halogen lamp, one prewired D4184 Power MOSFET (http://www.agspecinfo.com/pdfs/D/D%204184.PDF ) module is used as it has an onboard optocoupler to drive the mosfet with galvanic-isolation between the microcontroller and the halogen lamp. For more details, go to https://protosupplies.com/product/d4184-

mosfet-control-module/

Leave a Comment