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 :

It's reasonably powerful. The Arduino diecimilla that I used is based on the ATMega168 microcontroller clocked at 16MHz.

It's based on open source hardware and software (If you omit the trademark controversy about the Arduino trademark). The board design is public. This has lead to the creation of several variants and clones (Freeduino) which contributed to the popularity of the platform.

Power, firmware upload and serial communications through a single USB cable.

It's expandable. The concept of "shields" that can be stacked on top of the main Arduino board is genius :) As a matter of fact, making a small prototyping shield was the first thing I did for this platform.

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.

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

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 :

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

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

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

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 pin

Arduino pin

1 SCLK

Digital 2

16 SDIO

Digital 3

15 PD

Digital 4

13 VDD

+5V

12 GND

GND

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 gnu.io)" 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.

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 :

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 ? :-)

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

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 :-)

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

Remove the red LED and replace it by the RGB LED.

Connect the LED cathode to pin 14 of the ADNS-2051 (the LED needs to be common-cathode).

Comments

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: http://www.pixart.com.tw/upload/PAW3205DB-TJ3T_DS_S_V1.0_20130514113310.pdf) 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 :

Hello,
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 bozluangkhot@gmail.com

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.

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: http://cmsc838f-f12.wikispaces.com/Assignment3_Darren_Cheng_Rope_Measure

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 ...to start of with java i have no clue i am a beginner need help.

Comment left by daianhtai2007@gmail.com on 2011-11-27 08:59:53 :

Can you give me code of Arduino about get coordinate x,y by usb mouse ,such as:http://www.martijnthe.nl/2009/07/interfacing-an-optical-mouse-sensor-to-your-arduino/

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 commentshttp://cjoint.com/11ju/AGCnufkqMFp_OPTS2099.jpgSylvain

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?Thanks!

Comment left by Mamad Agig on 2011-02-08 12:10:47 :

Hi,thanks for your great articleI have question:if i want to do this project with another mouse what changes should i apply?email me pleasemamadagig@hotmail.com

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 : honey4530@yahoo.com