interfacing ps2 mouse with arduino

hello everyone…
i am doing a project using a ps2 mouse.
i want to get the X and Y coordinates as we move the mouse…
can anyone please help me with codes and libraries of arduino …
i have these codes but it is not working perfectly

problems with codes are…

  1. Once we start moving mouse X and Y readings increase but max reading we got was 101 in +ve X direction.
  2. Once mouse is halted its origin gets shifted and new position is treated as new origin(0,0).
  3. Also from little search I came to know that optical mouse provides 400 CPI(Counts per Inch) but I think it is pre-scaled in code somewhere.

mouse1.ino (1.77 KB)

PS2Mouse.cpp (5.26 KB)

PS2Mouse.h (826 Bytes)

I belive that the mouse is designed to give you the delta x and delta y for a fixed period of time, not the absolute position. If you think how a mouse actually works, this makes sense. It will also be consistent with your findings. So rather than setting X and Y to the delta x and delta y reading from the mouse ieach time, you need to increment them.

takes the datasheet of the onBoard camera(the only pic in the mouse) i don’t remember but you can use only ps2.h library and lot of write to set the maximum resolution.

i post my last prooject (the setup set the mouse from 2D to 3D mode that enable the z , that is the wheel )

#include <ps2.h>

 * Pin 5 is the mouse data pin, pin 6 is the clock pin
 * Feel free to use whatever pins are convenient.
PS2 mouse(6, 5);
  char mstat;
  char mx;
  char my;
  char mz;
  byte button;
  byte resolution;
  byte samplingrate;
  boolean bLeft;
  boolean bZ;
  boolean bRight;
void mouse_init()
  mouse.write(0xff);  // reset so for default it's setted on 2D mode;  // ack byte;  // blank */;  // blank */
  mouse.write(0xf0);  // remote mode;  // ack
  mouse.write(0xe8);//set resolution e8 up to 2^3(second number) count per mm
  mouse.write(0x03);;// ack
  mouse.write(0xf2);//whole code for set 3d mouse;
  byte in =;
   Serial.println("Setting up at 3D");
    Serial.println("Setting up at 2D, it hasn't z axis");*/

void setup()


void loop()

  /* get a reading from the mouse */
  mouse.write(0xeb);  // give me data!;      // ignore ack
  mstat =;
  mx =;
  my =;
  mz =;

  mouse.write(0xe9);  // give me data!;      // ignore ack
  button =;//0 rightbutton 1 middel button 2 left button 3 always a 0 
  resolution =;
  samplingrate =;
  /* send the data back up */
  bLeft=((B0000100 & button)>>2);
  bZ=((B0000010 & button)>>1);
  bRight=(B0000001 & button);
  Serial.print(mx, DEC);
  Serial.print(my, DEC);
  Serial.print(mz, DEC);
  Serial.print(bLeft, BIN);
  Serial.print(bZ, BIN);
  Serial.print(bRight, BIN);
  delay(20);  /* twiddle */