| by sohil4932 | No comments

Arduino + PS/2 Mouse

In this article I attempts to explain aspect of the PS/2 mouse interface including How can you establish interface between your Arduino board and old PS/2 mouse.


The PS/2 mouse interface originally appeared in IBM’s “Personal System/2” computers in the late 80’s and it remains a widely-supported interface. However, USB has quickly caught on these last few years and will eventually replace the PS/2 interface entirely.


The PS/2 mouse interface utilizes a bidirectional serial protocol to transmit movement and button-state data to the computer’s auxiliary device controller. The controller, in turn, may send a number of commands to the mouse to set the report rate, resolution, reset the mouse, disable the mouse, etc. The host provides the mouse with a 5V ~100 mA power supply.

The standard PS/2 mouse interface supports the following inputs: X (right/left) movement, Y (up/down) movement, left button, middle button, and right button. The mouse periodically reads these inputs and updates various counters and flags to reflect movement and button states. There are many PS/2 pointing devices that have additional inputs and may report data differently than described by me in this article.

The standard mouse has two counters that keep track of movement: the X movement counter and the Y movement counter. These are 9-bit 2’s complement values and each has an associated overflow flag. Their contents, along with the state of the three mouse buttons, are sent to the host in the form of a 3-byte movement data packet.

The parameter that determines the amount by which the movement counters are incremented/decremented is the resolution. The default resolution is 4 counts/mm and the host may change that value using the “Set Resolution” (0xE8) command.

Movement Data Packet

The standard PS/2 mouse sends movement/button information to the host using the following 3-byte packet:
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Byte 1
Y overflow
X overflow
Y sign bit
X sign bit
Always 1
Middle Btn
Right Btn
Left Btn
Byte 2
X movement
Byte 3
Y movement

.

The movement values are 9-bit 2’s complement integers, where the most significant bit appears as a “sign” bit in byte 1 of the movement data packet. Their value represents the mouse’s offset relative to its position when the previous packet was sent, in units determined by the current resolution. The range of values that can be expressed is -255 to +255. If this range is exceeded, the appropriate overflow bit is set.

Mainly Two type of PS/2 mouse connectors are available, The pinouts for each connector are shown below: 
Male

(Plug)
Female

(Socket)
5-pin DIN (AT/XT): 
1 – Clock
2 – Data
3 – Not Implemented
4 – Ground
5 – Vcc (+5V)


 

Male

(Plug)
Female

(Socket)
6-pin Mini-DIN (PS/2):
1 – Data
2 – Not Connected
3 – Ground
4 – Vcc (+5V)
5 – Clock
6 – Not Connected


How to connect it with Arduino?
Image is taken from Arduino Playground

Plug the power pin into the +5 header pin, the ground to ground. Then connect the data and clock pins to two of the arduino digital pins. I uses Digital pin 5 as  DATA  and 6  as CLOCK.


Now get PS2Mouse.zip from link given below:
{The library is written by Kris Chambers
Than unpack it in the hardware/libraries directory in your Arduino software folder.


Note : No of libraries are available for PS2 Protocol.Here I post one more link of the library called PS2 here.

Code for Arduino to control your  two motors robot using your PS2 Mouse : 
————————————————————————————————————–
#include <PS2Mouse.h>
#define MOUSE_DATA 5
#define MOUSE_CLOCK 6

PS2Mouse mouse(MOUSE_CLOCK, MOUSE_DATA, STREAM);
int motor1Pin1 = 9;
int motor1Pin2 = 10;

int motor2Pin1 = 8;
int motor2Pin2 = 7;

int motorsPinEnable = 2;
char mx = 0;
char my = 0;
void setup()
{
  Serial.begin(38400);
  mouse.initialize();
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(motorsPinEnable, OUTPUT);

  digitalWrite(motorsPinEnable,HIGH);

}

void loop()
{
  int data[2];
  mouse.report(data);
  mx = data[1];
  my = data[2];
 
   if(my < 0)
   backward(my);
 else if(my > 0)
   forward(my);
  
 if(mx < 0)
   right(mx);
 else if (mx > 0)
    left(mx);
}
void forward(int del){
   Serial.println(“Begin”);
    digitalWrite(motorsPinEnable,HIGH);
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
   
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
  
    delay(abs(del)*2);
    digitalWrite(motorsPinEnable,LOW);
    Serial.println(“Einde”);
}

void backward(int del){
    digitalWrite(motorsPinEnable,HIGH);
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
   
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
   
    delay(abs(del)*2);
    digitalWrite(motorsPinEnable,LOW);
}

void left(int del){
    digitalWrite(motorsPinEnable,HIGH);
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH); 
   
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
   
    delay(abs(del)*2);
    digitalWrite(motorsPinEnable,LOW);
}

void right(int del){
    digitalWrite(motorsPinEnable,HIGH);
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW); 
   
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
   
    delay(abs(del)*2); 
    digitalWrite(motorsPinEnable,LOW);
}

Results:

Leave a Reply