In this tutorial I’ll describe how you can connect the optical sensor inside a cheap mouse to your Arduino and have it read out the x- and y-movements. This will enable your Arduino to handle mouse input, detect surface movements, measure surface speed, etc.
This is what you need:
- Arduino board (any *duino will do) and the Arduino IDE.
- Optical mouse containing the PAN3101, ADNS-2610, ADNS-2083 or ADNS-2051 optical sensor. These sensors come in many mice sold today. If yours has a different sensor, don’t worry too much; they are all much alike. With a bit of luck, the library I wrote will work with other sensors as well.
- OptiMouse library for Arduino
- Some wires and soldering gear.
Step 1 – Open up your mouse!
The optical sensor and LED were covered with the black protective cap. Take it off carefully.
Step 2 –Take a look at the datasheets
Check out the datasheet of the sensor you’re dealing with (Google).We only need to connect 4 pins of the sensor to the Arduino, of which two are used for data and two for the power supply. The sensor uses bi-directional serial communcation over one data line (SDIO, pin 3). The second data pin (SCLK, pin 4) is used to time the bits. In case of a different type of sensor, the pin numbers might be different.
Step 3 – Cutting the controller wires (optional)
To make sure the mouse’s own controller does not interfere with the Arduino, I had cut the SDIO and SCLK wires running to the chip in the mouse. I’m not sure whether this is really necessary, so you might try first without cutting them.
Step 4 – Connecting wires
Solder wires to the four legs of the sensor that I indicated above. The +5V should go to the 5V Power pin on the Arduino, the GND should go to the GND Power pin on the Arduino. Pretty straightforward. Connect the SCLK to Arduino digital pin 2 and SDIO to Arduino digital pin 3. (You are free to use other pins as well, but in this tutorial I’m using pin 2 and 3.)
Step 5 – Example sketch
Download this .zip package with the library and example sketches. Expand the archive and move it into:
Restart the Arduino IDE. The examples should appear in File > Sketchbook > Examples > Library-OptiMouse. Open the Coordinates example and upload it to your Arduino board. Start Arduino’s serial monitor (38400 baud) to see the coordinates of your mouse coming in… Done!
2008-11-18 : Added some support for the ADNS-2083
2009-01-30 : Fixed compatibility issue with Arduino 0012 (Thanks Mark for notifying me.)
This tutorial is based on Benoît Rousseau’s example on the Arduino Forum. I just wrapped his functions inside a library.
If you have suggestions for improvements of this tutorial or the library, feel free to mail me: post [at] martijnthe [dot] nl. Update: the library is a bit outdated for the current Arduino versions. I am still providing the library as is, but I am not proving support for it anymore.