Introduction: Optical Mouse Odometer for (Arduino) Robot

Accurately determining the progress of a wheeled robot can be pretty tricky (and expensive!). Dead reckoning assumes that our motors are perfectly matched, our wheels don't slip and the surface we are running on is perfectly flat: most of these conditions are unattainable and are never guaranteed. Rotary Encoders on the wheels or motors are more accurate, they certainly remove the need for matched motors and can deal with incline changes, however slippage is still an issue. Ultrasonic or Laser range sensors circumvent all of the mechanical issues that plague dead reckoning and encoders but require stationary fixed points of reference, are subject to interference (objects coming between the robot and the reference point) and have issues with regard to range, accuracy and resolution (the more you want, the more it costs!).

Ideally, we want a sensor that can accurately measure positional changes in two dimensions, has a simple interface, has low power requirements, is easily obtainable and cheap into the bargain. The solution: that old optical mouse you have lying around! Computer mice are designed to do just what we want: very accurately track the two dimensional movement of an object in near to real time. They actually have a resolution 1000dpi which means we are able to measure a movement of ~0.03mm in any direction!

We do have to choose our mouse a little carefully however. We need one that either has a PS/2 connector (6 pin mini-DIN) or a USB mouse that is PS/2 compatible (should be stated on the bottom of the mouse somewhere, see picture). This is because the PS/2 protocol is really easy to implement (particularly for microcontrollers) whereas USB is somewhat more difficult!

I happened to have an old Microsoft USB1.1/PS/2 compatible mouse hanging around that I wasn't using, so I decided to sacrifice it to my robotical pursuits in the manner described herein.

Step 1: Tear Down

First things first, we need to open the case and liberate the electronic goodies within. I chose to do this because the mouse is far too big and bulky for my robot. If you don't want to this you just need a USB Type A or PS/2 socket that can be wired to the processor (Arduino in this case) into which the mouse can be plugged without modification, just skip this bit.

So, the process to deconstruct my mouse was as follows (also see pictures):

  1. Remove rubber adhesive feed covering screws (at the rear) and clips (at the front
  2. Remove the two screws at the back and then use a small flat bladed screwdriver to push back the clips holding the top and bottom parts of the case together at the front. (This may take a bit of fiddling, I actually inadvertently broke one of the clips in the process.)
  3. Once the case is open, remove the scroll wheel and then any screws hold the circuit board to the case. There may also be a few small plastic clips that retain the circuit board. The small screwdriver used before and some wiggling should free the circuit board(s) from the case.
  4. There should be a clear plastic piece under the circuit board (the lens) which will also need, so retain this and the circuit boards, discard (or file!) the rest.

If you plug the mouse into a computer or laptop, ensuring the lens is correctly in place on the bottom, the mouse should work normally when you move it around.

Step 2: Initial Circuit Modifications

Now we've got our hands on the board we need to connect it to our microcontroller. To do this we can either use the existing cable (USB) in my case and wire an appropriate socket or we can replace this wire with some of our own. We also need to determine the function of each of our new wires. This is done simply by using a multimeters continuity setting and working out which connection on the board is connected to which pin on the USB (or PS/2) connector. The original wiring can be seen in the first image above and functions are, left to right, as follows:

  1. Red: Vcc (USB pin 1)
  2. White: Data (USB pin 2)
  3. Green: Clock (USB pin 3)
  4. Black: Gnd (USB pin 4)
  5. Black: Shield

I chose to desolder the existing cable form the board and replace it with some wires with Arduino friendly Dupont connectors. I also chose not to connect a wire to the shield connection as its not strictly necessary. We then connect the mouse to our microcontroller of choice, an Arduino Leonardo Nano clone in this case, as follows:

  • Mouse Vcc to Arduino Vcc
  • Mouse Gnd to Arduino Gnd
  • Mouse Clock to to Arduino Pin 6
  • Mouse Data to Arduino Pin 5

Step 3: Software

Now we've connected everything up we need some software for the microcontroller. Firstly we need some code that implements the PS/2 protocol. Helpfully there is an Arduino library available for this, however I found it to be a little outdated and I wanted to add some new functionality so I reimplemented and extended it a bit and it can be found here: https://github.com/jazzycamel/PS2Mouse.

The library contains a simple example sketch (PS2Mouse.ino) which requests the mouses status and position delta (the change in X and Y directions since we last asked) and prints it to the serial port once every second. If we program the Arduino, and everything has gone well, the mouse will light up and your favourite terminal application (or the Arduino IDE's serial monitor) will show a stream of status, X and Y deltas once a second. The X and Y data has a (theoretical) range of -255 to +255 and each step (theoretically) represents 0.25mm which means we can (theoretically) measure up to 63.75mm change in any direction (for X positive values are to the right, for Y positive values are forwards).

Note: the above numbers are theoretical for the following a reasons:

  1. Whilst a mouse can report the range -255 to 255, so far all the mice I've used only return -128 to 127.
  2. Whilst each step supposedly represents 0.25mm, I've found this to be very dependent on the mouse and the surface its on. Generally the step size seems a lot smaller.

Step 4: Further Modifications and Finishing Up

Once I'd got everything up and running I decided to remove the extra circuit board which has the electronics for the mouses buttons and scroll wheel as I didn't need these features and they were just taking up space. I desoldered both of the ribbon cables, plugged everything in and... nothing. The mouse wouldn't light up and I got nothing from the serial port. A little investigation with the multimeter told me that three of the pins (1,3, and 6) on the rightmost connector (looking from the top) were usually connected together when the button board was connected, so I just shorted them together with some scrap wire as shown in the picture. I then plugged everything back in and Hey Presto! We were back in business!

The final touch was to superglue the clear plastic lens in place on the circuit board to retain it and so I wouldn't lose it.

The next step will be to mount this on my robot and put it to use. I actually intend to cannibalise another mouse in the same way and fit both to the robot so as to be able to accurately determine orientation as well as progress (as described here).

I hope people find this little tutorial useful, please feel free to ask any questions. Thank you.

Comments

author
dhanu2 made it!(author)2017-06-27

Hi , Thanks for nice Tutorial. I used PS2 mouse as you used. but in the serial terminal it gave x = -1 and y = -1 and this value is not change when i move the mouse.

author
AnuraagS5 made it!(author)2016-03-27

Hello,

Thank you so much for the awesome tutorial! For some reason the serial port just says "setup" and then doesn't say anything after that. What could be causing this?

author
jazzycamel made it!(author)2016-03-28

The step after "setup" is the initialisation of the mouse, so if its hanging there i can only imagine that either the mouse is not connected correctly (in which case, check your connections) or not functioning. Sorry i cant be more help.

author
AnuraagS5 made it!(author)2016-03-30

No problem! Thank you for the tutorial! :)

author
NisheetS made it!(author)2017-06-01

Hey. I incurred the same error. Any clue if there is something besides the connection error?

author
MateuszK55 made it!(author)2017-02-28

Hello, according to PS2 wiki interface the range should be from -127 to 127.
I know it's due to char being 8-bit, however changing like all posible values from char to int doesn't work for me either.
Any solutions, how to increases range to -255,255?

author
jazzycamel made it!(author)2017-02-28

You can use Arduino's map() function:

int result=map(ps2value, -127, 127, -255, 255);

author
MateuszK55 made it!(author)2017-02-28

ain't sure if it's legit solution.

changing data to 9bit would result in increasing measurement up to 6,4mm in each direction,
mapping still gives me with 3,2mm

author
jazzycamel made it!(author)2017-02-28

Sorry, I misunderstood you original question.

I've updated the library generally and changed it to use 'int' for the x and y variables. I've put the library on github (https://github.com/jazzycamel/PS2Mouse) to make it easier to distribute updates. Please be advised that I don't currently have the hardware setup to test it, so all bug fixes or problems gratefully received.

author
jazzycamel made it!(author)2017-02-28

Also, I changed some of the method names to be more 'Arduino-y', so check the example (PS2Mouse.ino) to see whats changed.

author
jazzycamel made it!(author)2017-02-28

@MateuszK55

I've now hooked up some hardware and tested everything actually works... and it does!

Some interesting points to note are that: 1) despite now being able to handle the full -255 to 255 range, the mouse only ever sends -128 to 127! 2) The overflow bit never seems to be set! 3) The whole "4 counts per mm" is total rubbish and wildly inaccurate! All of these things may be due to a cheap mouse that I'm using to test this however...

Anywho, I hope this is useful to you or someone :)

author
MateuszK55 made it!(author)2017-02-28

Many thanks for fast reply and the code :)
However I misstypo'ed my first comment, but I guess You already found out.
http://www.computer-engineering.org/ps2mouse/
According to this site, the range of movement should be from -255 to 255, not -127,127 like I said, so I apologize for all the chaos I caused.

I'm using both: A4Tech and some 'no-name cheaper-oooo' mouse but still getting same values like You, I guess it's just meant to be like that.
Also noticed that setting resolution either 4counts or 8 didnt do much difference.
Many thanks for all Your effort, I really appreciate that :)

author
fordnichols made it!(author)2017-02-17

Hi,

So I got the code working, was wondering if there was a way to modify it so the position didn't "Reset" every second? Like, if I move the mouse from (0,0) to (120,0), so it will stay there until it reaches X reaches 127 upon which it rolls over. Then I could use some kind of counter when it rolls over. As opposed to resetting to (0,0) every second, which is what is happening now. I'm having a hard time knowing how to approximate the distance if it is constantly resetting. Hope this makes some sense.

Also is there a commented version of the .cpp file? Trying to tinker around with it but I'm a little lost.

Thanks!

author
Mohammadt35 made it!(author)2017-01-11

thnaks , can you help me how can i use three mous for odometry?

author
HarshC17 made it!(author)2016-12-31

The code works just fine. But can any one tell me how I can include a click in my program.

author
jazzycamel made it!(author)2017-01-01

Hi there,

The 'click' data is actually part of the data returned from the mouse in the 'stat' byte returned from mouse_pos(). Have a look at this gist: https://gist.github.com/jazzycamel/c9e1f320d8fc709... for a brief example.

Hope it helps :)

author
HarshC17 made it!(author)2017-01-03

Thank you very much for the help. I have been able to include the click using stat.

author
ekoymen made it!(author)2017-01-01

Thank you for this great post. But the code did'nt work for me. There isn't any connection problem and my mouse is PS2. But my arduino board is mega 2560. Can it be the reason?

author
AnikD8 made it!(author)2016-07-30

hello...nice tutorial.... but I have 2 questions....
1. for accurate navigation ...when we will use two mice together....can arduino nano or uno(atmel 328) derive those large formulas??
2. the code runs smoothly for a PS2 compatible USB microsoft mouse .... but I have some frontech USB n PS2 mice...they are not working....but they all have the same type of sensor (MX8732A or MX8733) which is USB n PS2 compatible ....
but they are either stopped at "set up"...or printing "stat=1000, x=0 , y=0" 5 or 6 times ,,..then blank!! why??

author
ChiC19 made it!(author)2016-10-29

Hi AnikD8, I am facing the same problem like yours (number 2). May I know how to solve the problem? Or anyone out there can help? :(

author
ARINDAMD7 made it!(author)2016-04-03

The code works !!! Thanks for this awesome post

author
praveenreddypadala made it!(author)2016-02-19

Hi,It was really good to see that you had edited the old ps2 library and posted it here.Thanks a lot.Will this code work for any type of ps2 mouse?If not can you please mention the ones that it will work with.Thank You.

author
jazzycamel made it!(author)2016-03-28

Should work with any mouse that uses the standard PS/2 protocol.

author
PearlO1 made it!(author)2016-02-17

Hi

is there any other way to access the rest of your project? I have not been able to get access to research gate. Will really like to see how to get direction from the mouse.

Thank you

Pearl

author
jazzycamel made it!(author)2016-02-18

Hi Pearl,

I've updated the link to the paper so you should be able to get to it now (I don't have a Research Gate login either!).

I am developing an algorithm for this technique based on the paper combined with quite a lot of my own research/experience, however it's not something I would inflict on anyone just yet as its quite rough, has no comments or documentation and is hardly tested. Other commitments mean I haven't spent much time on it recently, but some interest from others always tends to prompt action, so I will try and look at it this weekend and get back to you and/or publish it.

Thanks, Rob

author
PearlO1 made it!(author)2016-02-18

Awesome. Thanks Rob. Its a great project. I am very new to arduino in general. And all I am trying to do is to be able to get absolute location from a mouse(using imaging). SO I am trying to figure out how Processing works..

author
PearlO1 made it!(author)2016-02-10

Awesome

20160210_203808.jpg
author
ArielC11 made it!(author)2015-08-15

Thanks A LOT for sharing.

In this link In this link http://playground.arduino.cc/uploads/ComponentLib...


mouse_read(); /* mouse id, if this value is 0x00 mouse is standard, if it is 0x03 mouse is Intellimouse */
// Serial.print("Setting wheel\n");
mouse_write(0xe8); /* Set wheel resolution */
mouse_read(); /* ack */
mouse_write(0x03); /* 8 counts per mm */
mouse_read(); /* ack */
mouse_write(0xe6); /* scaling 1:1 */
mouse_read(); /* ack */
mouse_write(0xf3); /* Set sample rate */
mouse_read(); /* ack */
mouse_write(0x28); /* Set sample rate */
mouse_read(); /* ack */
mouse_write(0xf4); /* Enable device */
mouse_read(); /* ack */

, wheel work, but I do not understand the code could you add the function Z axis for the wheel.

Thank you and apologize for my bad English .

11889507_10153562838596952_3056934571044407231_n.jpg20150815_121055.jpg20150815_122355.jpg
author
Pinaut made it!(author)2015-02-12

thx for sharing!

Also useful for a old (non optical) ball mouse! The 2 incremental wheels from the mouse gives you a nice feedback for something rotating. Enables me build a servo motor with multiple turns using a simple gear DC motor.

author
manu1975 made it!(author)2015-01-25

Great! And very useful!

author
theDiverDK made it!(author)2015-01-21

Cool article, guess il have to find an old optical mouse to try it myself :)

author
jazzycamel made it!(author)2015-01-22

You should, its fun! Best of luck, thanks for your comments :)

author
peppypickle made it!(author)2015-01-21

This is fantastic, thanks for sharing this really creative Instructable!

author
jazzycamel made it!(author)2015-01-22

Thanks you :)

author
msraynsford made it!(author)2015-01-21

We tried this 10+ years ago at uni and concluded that optical mice didn't maintain that accuracy over any great distance and that when used as a mouse the human closed the loop on the control system (you move the mouse a little bit further if it doesn't quite get to where you want it to be)

I'd love to see what results you get when you put it on your robot, please update us all :)

author
jazzycamel made it!(author)2015-01-22

I too tried this at uni (also about 10 years ago!), and I would agree there are accuracy issues over certain ranges. We found you could get the best precision by reading the mouse as often as possible so that the deltas were small (and we would also avoid overflow). I think we did this via a timer interrupt so as to accumulate the values for the slower motor control logic.

For my application I am mostly interested in determining the robots orientation so as to make relatively accurate turns. With this in mind I will use two (or even three) mice in combination with ultrasonic/laser sensors to try and achieve a good consensus.

I will definitely post an update when I have something, thanks for your comments :)

author
mtamanini made it!(author)2015-01-21

Nice and helpfull BUT...

It would be better (and way more cooler) if you could show how to use the sensor directly on arduino without relying on the PCB from the old mouse. In this way, if you got 2 different mouses, you don't have to have 2 different set ups.

How about that?

P.S.: Don't get me wrong. loved the idea anyway. Best reggards.

author
jazzycamel made it!(author)2015-01-22

I think you will need most of the components on the original PCB to support the sensors operation, so I'm not sure of the advantages of removing it from the board. There are people out there who have bypassed the onboard microcontrollers (example here) to talk to the sensor directly, but they still tend to keep the original board too.

As to multiple mice: my intention is connect two mice to the same Arduino (the same one in fact that runs my robots motors and other sensors), so I'm not sure what you mean by two setups...?

Thanks for your comments :)

About This Instructable

54,048views

242favorites

License:

More by jazzycamel:Quick Arduino Hygrometer (Humidity Sensor)Non-blocking Ultrasonic Sensor for ArduinoOptical Mouse Odometer for (Arduino) Robot
Add instructable to: