Tuesday 3 June 2014

Hacking an optical mouse

Have you ever wondered ...

... how a mouse works?

I don't mean the real old fashioned kind, with proper balls and stuff, but an optical mouse or even a laser mouse (similar to optical but higher precision).

Well, I did. ... Wondered, that is ....

So, after Googling around for a bit and finding a number of hacks (like this one or this one or several others), I decided to have a go myself. Which is why I engineered an early exit from work on a Friday afternoon that resulted in me scuttling into Maplin to pick up an Arduino Uno for the best part of twenty-five quid (yes, I know they're cheaper online, but I'm not such a patient hacker) .... and on Saturday morning, I hunted in various boxes to find a sacrificial mouse ... this one .... (as with all the images, click to see a larger version in a new window)

Sacrificial mouse
Sacrificial mouse
Removing the single screw under the QC label gets the top off the mouse to reveal the relatively simple circuit inside. If you click to the full size view, you can see I've marked the LED assembly, the optical sensor - in this case a Unity MCS-12085 (just Google the numbers on the chip!) - a microcontroller (MX84510, but that's irrelevant 'cos we don't need it), the micro-switches for the mouse buttons and four wires heading off to the PS/2 plug at the computer end. I removed the scroll wheel temporarily to avoid dropping it repeatedly, which was annoying me after the seventh or eighth time ....
Top view before
Top view (before hacking)
After finding the datasheet for the MCS12085 optical sensor, to identify the pin-outs, it's easy enough to flip the circuit board over and find the four little beauties that are apparently the only ones we need to hack into the sensor itself. I didn't figure this out myself - I have to give credit to the earlier hackers, who already described the principle .... but anyway, let's press on ...

The four pins we need are two for power - GND and VDD (+5V) - the clock - marked SCK - and the serial input/output marked SDIO. Take a look at these on the image below and also note the connections to the existing PS/2 cable that I've highlighted in a yellow rectangle ...

Bottom view 1
Bottom view showing 4 pin-outs and PS/2 cable
Now if, like me, you're a lazy sort of bugger that can't be bother to hacksaw the PCB or drill holes into the copper tracks, like some of the other guys did, then you might sit and scratch your head for a bit and idly eyeball (visually trace) the existing connections to see where the four pins end up. This proves useful ...

The power and ground (VDD and GND) predictably end up at the outermost two wires of the PS/2 cable (because that's how the mouse is powered, after all), and the clock and data pins (SCK and SDIO) head off to the microcontroller - which normally drives the sensor and then outputs the processed PS/2 signals back to the 'pooter.

This is actually good news, because we don't need the microcontroller at all, and can remove it from the circuit and hook things up however we like.

So - in the image below - notice that two output pins from the right of the microcontroller connect to the remaining two wires in the PS/2 cable - the two between the marked VDD and GND inputs .... you see where I'm going with this?

Bottom view 2
Cunning plan forming ...
Well, what I'm thinking is that, with the controller chip gone, we just need to link the PCB tracks as shown below in red to get our four needed connections handily wired up to the existing PS/2 cable, with minimal need to physically fart around too much. Cool eh?

Bottom view 3
Cunning plan confirmed
So that's what I did.

I fired up the soldering iron, removed the microcontroller chip, and soldered links in to the PCB at the locations marked in red above. After doing this, the top view of the hacked mouse now looks like this ...

Top view after
Top view (after hacking)
Now just screw the cover back on the mouse and, to all intents and purposes, it doesn't look any different to when I started.

Except of course for the necessity to cut off the PS/2 plug and hook up the four wires to my Arduino. Something like this ...

Arduino
Arduino hooked up to the mouse sensor

The VDD and GND from the mouse go to +5V and GND on the Arduino, and I've arbitrarily chosen to wire the serial IO to digital pin 7 and the clock to digital pin 4.

Now breathe a big sigh of relief, because from now on it's all software, with which I'm much more familiar ...

... and after a bit of tinkering, I produced this Arduino sketch (click to download the source code) that provides a simple interface to the mouse sensor by typing commands in the Arduino Serial Port Monitor. Take a look at the code and you'll see what I mean - you can force the LED to be permanently awake (necessary to get a good image), switch it back to normal mode (where it will sleep after 1.5 seconds of no movement), and dump the 18 x 18 grid of pixel values to check that we get some kind of realistic data. Note that the pixel values are all in the range 0 to 63 because two bits of each 8-bit pixel are used to indicate data validity and start of frame - refer to the datasheet for the gory details.

Now, almost (but not quite) the final picture - here is the mouse attached to the Arduino, and all that's needed is to plug the Arduino into the USB port of the computer and try to get some output ....

Breadboard
Just add USB cable ...

... and here's a dump (if you see what I mean) from the Arduino Serial Monitor, in which you can "see" the pixel data returned by the mouse sensor ...

Pixel dump
Pixel values dumped as text

Now, looking at grids of numbers is not really why we started this, so I adapted the original Arduino sketch to provide a simple - but much faster - streaming output, which I then hooked into a C++ application to display the (rescaled) pixels as a grayscale image. You can download the streaming version of the sketch here, but I'm going to hold off on releasing the source code for the viewer for a bit because I have some more cunning plans for it in a later blog post ..... this could get interesting ....

For now, here's a snapshot of a single frame as seen by my sacrificial mouse as it glides effortlessly over an (unpaid) credit card statement ......

Pixel display

I'll post again soon with more .......... hope this was useful ....

2 comments:

  1. How do optical mouse work on ComputerHow do optical mouse tackle Computer – Mouse is overall used with PC for pointing, clicking, scrutinizing, climbing and down things present on the screen. The mouse can be related with PC by methods for wire or Bluetooth device.

    ReplyDelete