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 |
Top view (before hacking) |
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 showing 4 pin-outs and PS/2 cable |
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?
Cunning plan forming ... |
Cunning plan confirmed |
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 hacking) |
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 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 ....
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 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 ......
I'll post again soon with more .......... hope this was useful ....
Really Cool !
ReplyDeleteHow 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