Resman says hi.

Optical Mouse Cam

OK, so I finally decided to get my hands on the Arduino platform and see what all the buzz is really about. And I must say, I'm impressed. This is by far the most developer-friendly development platform I've ever seen. It's main advantages :

So, a while ago I came across this great optical mouse hack by Sprite. I decided it would be great to do as my first Arduino project (beyond the "Blinky LED" example, that is.)

First step, dissection of a wireless mouse from a cheap Typhoon wireless mouse / keyboard combo.


Unscrew it. Look for screws under the pads and stickers on the bottom.

Mouse open

My model came with two PCBs which are coupled using a 14 pins, two rows header :

first PCB removed

The unusual looking chip in the middle is an ADNS-2051 optical sensor. This chip holds a 16x16, 64 levels of gray image sensor and a DSP which computes movements from the optical flow of the captured images :

PCB top PCB bottom

Underneath the second PCB is the optical bloc with lens and LED collimator :

Optical bloc

Better view of the optics. It was manufactured by Agilent technologies or its spinoff Avago technologies :

Optical bloc

Agilent/Avago provides the optical block, motion sensor and LED assembly of most optical mice. Here's how everything stacks up :

Exploded view

OK, so going this far was not really necessary, I just like to see how things are made and to take macro shots of it :-) Going back to the ADNS-2051 chip and the Arduino, you need to make the following connections :

ADNS-2051 pinout
ADNS-2051 pinArduino pin
1 SCLKDigital 2
16 SDIODigital 3
15 PDDigital 4
13 VDD+5V

All the other pins should have the required circuitry already on board, so they need not be touched. In my case however I had to wire the LED, too, since it was not directly wired to VCC but switched on by the controller. This is because it was a wireless mouse build with a chip intended for a corded design, so they had to diverge a bit from the reference design. I was lucky, the sensor board had a female header which I could use to wire everything, so I did not have to solder anything. But you probably will.

Ignore the prototype board on top of the Arduino, you can wire everything directly to the Arduino headers.

You can download the Arduino script and the PC program below. The program is in java and requires the RXTX library to be installed. You need to get the "RXTX 2.1 for use withOUT Sun's CommAPI (namespace" version. No, as of 2008 java still does not natively support the serial port. It should work with Linux, Mac OS X and Windows, but I only tested it under Windows.

[Addendum: RXTX seems to have been discontinued and the website is now a blank page. I suggest using PureJavaComm instead, it works great and does not require to be installed directly in the JRE. The project may need to be modified a bit to adapt it to this library.]

Once RXTX has been installed you can run the jar file simply by double-clicking on it or in the command line like this :

java -jar mousecam.jar

The window will show up. Simply select the serial port your Arduino board is connected to and click Connect. The sensor data will be drawn on screen in real time. You can increase or lower the number of lines to read from the sensor in each run. The frame dump routines are a bit slow so if you dump a full frame you'll get only about 5 fps. If you're moving the mouse at the same time, there will be severe shearing artifacts and the scan will not look pretty. In this case, reduce the capture window to 2 or 3 lines.

Application window

If you move the mouse while the application runs you can "scan" the surface below it. As you can see the quality is unmatched. You need to keep the mouse orientation exactly the same while scanning since there is no way to detect rotations, and this distorts the capture even more.

Here's how a white sheet of paper, the fabric from my pants and the small print on a 20 euro bill are seen by the sensor :

White paper Fabric 20 Euro bill

Alright, what else can we do with this very nice sensor ? Looking at the datasheet something struck my eye : the sensor is sensitive in a wide range of wavelenghts, from ultraviolet to infrared. So why not replace the standard red LED by an RGB LED ? :-)

Sensor sensitivity RGB LED RGB LED installed

I skip all the gory details, here's the not so impressive result :

RGB application

I was attempting to scan a detail of a 20 euro bill. I've had several issues with the sensor when switching between diferent colors. The sensor would be nearly saturated or very dark on the border of the frames. I solved the problem by adding a 100ms delay after each color change so that the sensor could adapt. Another issue is that the sensor seems to use some kind of automatic aperture control, which makes it impossible to balance the colors. Below is what the same detail looks like with a "real" camera. We can kinda recognize the blue lines :-)

Actual detail

If you wish to reproduce this amazing result (who wouldn't ?), you need to do the following :

Further information :

OK, enough time wasted on this already ! :-)


Comment left by slam on 2020-06-11 17:53:20 :
i try on paw5021 sensor but always returns -128, -128 or 1,1
i found the clock (D- pin 7), IOdata (D+ pin 8), Vcc and Ground, looks like the same USB port connected to arduino's digital pin
Comment left by Erwin . on 2020-03-04 00:58:20 :
In past I was having idea about this Optical mouses I suspicious it had sort of sensors detect laser reflection from all axes , my thinking way were way complex and I never thought someday we will able to have video player that get live video images from Mouse camera .
Comment left by Katherine on 2020-02-28 20:04:49 :
Tousse ensemble, tousse ensemble, ouais, ouais.
Comment left by Panda on 2019-12-30 12:01:06 :
Datasheet, datasheet...
Je préfère datashort, avec votre approbation.
Comment left by LaFolle on 2019-12-05 10:49:35 :
Avec mes mini-strings, ça déborde de partout.
Comment left by joao on 2019-03-20 19:47:46 :
nice work my friend... a "few" more step and you have a fingerprint sensor :)
Comment left by Picsou on 2019-01-14 18:55:21 :
Quand je fais de la purée Mousline, je suis sûre que tout le monde en reprend...
Comment left by Mamad Agig on 2018-11-16 19:42:52 :
ok i added this to my car bumper with solenoid on my brake but often i still hit objects. you should have warning for anyone trying to use for object collision with there real vehicle. did thousand dollars in damage.
Comment left by 9195917110 on 2017-12-12 23:39:05 :
Comment left by Henry Starck on 2017-11-26 23:31:55 :
J'ai réussi à installer ma Marvell Libertas sur Debian, alors là APPLAUSE !!!
Comment left by Ojasvi on 2017-10-13 19:33:20 :
What are the modifications for using purejavacomm mentioned in the tutorial?
Also i tried with an mfizz rxtx version specifically for windows 64bit, tried running the program to get the following:

Exception in thread "main" java.lang.NoClassDefFoundError: gnu/io/UnsupportedCommOperationException
at mousecam.MouseCam.main(
Caused by: java.lang.ClassNotFoundException:
at Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

Looked up online to find a complicated process, to sort this out. Suggestion Please??
Comment left by Docteur Mayfield on 2017-04-02 12:36:48 :
What is Arduino ?
Comment left by Andre on 2017-03-31 07:08:39 :
Wonder if I could DIY some sort of very primitive camera for my Hubsan drone so that it avoids objects? Sort of like autoland.
Comment left by Colonel Rumford on 2017-02-22 18:52:09 :
Et, demain, j'enfile ma mini-jupe pour aller au travail.
Comment left by pdbperks on 2017-01-21 12:58:50 :
Very helpful article. Just trying to set this up to display scans on a Nokia 3310 LCD.
Comment left by Sietse on 2016-12-09 08:47:12 :
is it possible to use the SPCP168A?
Comment left by Aung Myo Myat on 2016-06-07 11:16:32 :
how about ADNS-2620 with arduino?
Comment left by BradenT on 2016-04-13 06:12:31 :
I have a mouse with 16 pins but I figured out it does not need arduino as it has d+and d- so I just traced the lines back and soldered it to a USB connector NOTE: I have not tested it yet as I don't have a working computer. The chip is a mx8871
Comment left by janesko on 2016-03-14 19:34:34 :
I am trying to make the capture window bigger any advice? I changed PixelImagePanel from to ( 1000, 1000 ); in the the recompliedeverything but its not working.
Comment left by Dmod on 2016-02-05 20:14:40 :
Has anybody modified the optics of an optical mouse to be able to image at a distance, say 30 cm from the mouse?
Comment left by need101 on 2015-09-20 16:24:34 :
Excuse me Sir, May I just ask how can I improve the image quality of the image captured by the optical mouse. Thank you very much. here is my account Sir
Comment left by elewin on 2015-03-21 21:39:47 :
Oh jeez, I am really sorry about posting numerous times. I was hitting the refresh button.
Reply from the author :
No worries, I deleted the redundant messages. Looking at the datasheet it does not look like this chip supports reading the image data.
Comment left by elewin on 2015-03-21 21:26:27 :
I don't have a PD pin. I am useing a PAW3205DB-TJ3T (datasheet: is there an equivalent pin?
Comment left by inevitablecrafts on 2015-01-09 13:11:36 :
thank you so much, i just opened a mouse and googled a2051 and arduino :)
Comment left by Marcus on 2014-10-28 20:59:06 :
thank you for this interesting project!
I have bought a few Mice at ebay 8("USB 2.0 3D LED Optical Wheel Wired Mouse" <$2) and it contains a KA2.B D1032CB3 but I am unable to find a data sheet. Any idea what inexpensive mouse would have a documented sensor? I am trying to find some sensors to use for a course I am giving at a local school.
Comment left by Sinan on 2014-07-24 08:18:18 :
I'm trying to do this project using PAW3305DK. However I have some problems about communicating with it. When I checked the datasheet I couldn't find anything can help. Do you have any suggestion?
Reply from the author :
From a quick glance at the datasheet it looks like this chip uses an open drain SPI interface, make sure to connect a pull-up resistor between the SDIO line and Vcc. I don't see any command to read the image data, however.
Comment left by Someone on 2014-05-19 21:59:18 :
Update links because i cant find the files for it. And give an example on how to install the RXTX x64 please.
Comment left by SOMANSHU MEHTA on 2014-05-09 20:08:09 :
Cannot download RXTX library.I have the latest java version.Do you have any mirrors for that.
Reply from the author :
The RXTX website seems to have been replaced by some kind of generic parking crap. Try using PureJavaComm instead.
Comment left by shtface on 2013-11-29 13:39:24 :
the fact that you can actualy take photos is amazing
Comment left by boo on 2013-07-06 11:42:05 :
hi we are using zippys mouse suddenly what happen means the cable get separated from mouse so in that cable is having 4 wires one is (1)red (2)green (3)black and (4)white so which colour wire have to connect to which wire .in that mother board their is (1)gnd (2)v dd (3) data (4)clock so please how to connect the wires tell me as soon as possible kindly do a mail to my id is
Comment left by Andre on 2013-05-30 08:46:02 :
Nice idea using RGB LEDs.

This would make a very nice microscope for imaging cells, you could use multiple LEDs such as infrared red orange yellow green blue violet and then make a composite hyperspectral image.
Comment left by inspiritus on 2013-03-20 11:51:35 :
hmm Can't download RXTX library? have Java 7 update17. mousecam.jar doesn't work > what is wrong :(
Comment left by Marvin Gascon on 2013-02-16 11:24:31 :
my mouse don't have a PD pin ? it haS the following pins---> 1=Vss, 2=Z1, 3=Z2, 4=L, 5=M, 6=R, 7=OPT, 8=LED, 9=D-/DATA, 10=D+/CLK, 11=AVDD, 12=VDD5V,,, the ic number is A2636....can you help me??
Comment left by rand3289 on 2013-02-15 16:11:47 :
This project is very interesting from the following perspective: Imagine a bunch of optical fibers running from an LED to sensors. Sensors dim the light using cheap mechanical components. This is similar to optical encoders. Dimmed light from sensors is tunneled using same type of optical fiber into a regular web cam. One of the problems with this idea is that 30fps web cams might not give you the sensor update frequency desired. As an alternative, there are optical mouse sensors. For example ADNS-3080 has a 30x30 image sensor with 2000-6469 frames/second rate!
Comment left by Jon Froehlich on 2012-11-21 20:32:14 :
Thanks for posting this. A student used this idea to help build a rope measurement hardware prototype for climbers:
Comment left by jon on 2012-11-13 14:28:58 :
kaya Likha Samaj MA nahi aya ra bal Thik Sa lik na ra sal;a
Comment left by Mikael Danielsson on 2012-09-28 19:29:25 :
Where did you get the pinout for the chip?
Comment left by Mikael on 2012-08-23 17:32:27 :
For all of those wondering wether you may use the H2000-chip (HDNS-2000), I must tell you that this chip have no sophisticated output as the one mentioned in this article. The HDNS-2000 have only PS/2 and quadrature output, thus handling all the image data internall. Sadly. This chip is very common, I've opened up many mice in hope of obtaining one to use for this purpose, but most of times found this chip.
Comment left by ashirviskas on 2012-08-01 14:25:32 :
can i use h2000 sensor?
Comment left by Issac on 2012-03-23 09:38:31 :
i dnt have a PD opition in A2610 Sensor what can i do start of with java i have no clue i am a beginner need help.
Comment left by on 2011-11-27 08:59:53 :
Can you give me code of Arduino about get coordinate x,y by usb mouse ,such as:

But i only have PAN3204 can you write for me this ic library.
Thanks a lot.
Comment left by Sylfranc on 2011-07-28 13:28:58 :
Thank you very much for this page.

For additional information:
The code works perfectly with a sensor with S2099 mark recovered from an old logitech optical mouse.
The only differences are the supply of 3.3V and the identification code returned 03 instead of 02.

A picture with comments
Comment left by Ed Xavier on 2011-04-29 08:56:13 :
Incredibly useful information. What started as a question regarding how the optical sensor worked has turned into a desire to incorporate a hacked mouse into various projects for members of my group. Your work is genuinely appreciated.
Comment left by BioCleaner on 2011-03-01 20:37:29 :
Hello! Your article helped a lot but i only have a mouse with a N1165 sensor and I can't find the datasheet. Have you ever hear of this kind of sensor? Should I try soldering it the same way as the ADNS2051?
Comment left by Mamad Agig on 2011-02-08 12:10:47 :
Hi,thanks for your great article
I have question:if i want to do this project with another mouse what changes should i apply?email me please
Comment left by sikkuno on 2010-11-24 15:17:45 :
@^ you can't do this with your mouse's usb wires. You have to wire direct to the chip outputs on the board because the external cable is the usb interface. The red black green and red wires are USB power D- D+ and ground.

Very cool post by the way! I did not realize that an optical mouse takes screenshots and moves based on the difference. I would be interested in learning more about the algorithm to know the direction of movement.
Comment left by sikku on 2010-10-07 09:34:05 :
hi we are using zippys mouse suddenly what happen means the cable get separated from mouse so in that cable is having 4 wires one is (1)red (2)green (3)black and (4)white so which colour wire have to connect to which wire .in that mother board their is (1)gnd (2)v dd (3) data (4)clock so please how to connect the wires tell me as soon as possible kindly do a mail to my id is :
Add a new comment :
Name :
Comment :

Answer the following question : Which is the smallest, a peanut or the Moon ?