In this project the Arduino pro-mini board is used which is pre-programmed with the Arduino boot-loaderArduino IDE version 1.0.3 on windows operating system. The image of the Arduino pro-mini board and the Arduino IDE is shown in the following;
Fig. 2: Typical Arduino Pro-Mini Board
Fig. 3: Arduino IDE Software Window
Another hardware which can perform the USB to TTL conversion is used to upload the program into the arduino board.
Fig. 4: External USB to TTL converter board for programming Arduino and serial communication
It is assumed that the reader has gone through the project how to get started with the arduino and done all the things discussed in it.
The PS2 mouse uses a synchronous communication with the PC which is very much similar to the Two Wire Interface protocol. The PS2 connector has a pin for Data and another pin for Clock and using only these two pins the mouse communicate with the host device. The mouse always has 6 pin mini-DIN male connector for PS2 interface and the host device always has the corresponding female pin. The images and the pin-outs of the PS2 male and female connectors are shown in the following image;
The image of the PS2 male pin
Fig. 5: 6 pin mini-DIN Male connector for PS2 interface
The image of the PS2 female pin
Fig. 6: 6 Pin Mini DIN Female Connector Plug for PS2 interface
The pin-out of the PS2 male and female connectors
Fig. 7: Pin-Out Of PS2 Male and Female Connectors
When it comes to connecting the female connector with the circuit board one should be able to identify the pins at the bottom of the PS2 connector and the following image will be helpful.
Fig. 8: Bottom of Mini DIN Female Connector Plug for PS2 interface
The code written for this project uses the custom PS2 library file called “PS2Mouse.h” which has all the necessary routines for accessing a PS2 mouse. There are two functions which the user can directly make use in their code and are namely “mouse.initialize()” and “mouse.report(data)”. The details of the functions are discussed in the following.
The function mouse.initialize() is used to perform all the necessary things to initialize data reporting from a mouse. Once the initialization is done properly the mouse starts sending the data regarding its X-Y movement and the status of the buttons on the mouse. The function takes no parameter and returns nothing but will exist only when the initialization is properly done.
The function mouse.report(data) can be used to read the current data from the mouse. The function should be provided with an integer array of three elements and the function will fill the elements with the required values. The first element is written with the status value, the second element with the X movement value and the third element will be written with the Y movement value.
If there is no movement in the X-Y plane the value for both the second and the third variables will be zero and if there is no button press the first element will be having a value 8. The first element of the array will be having the values 8, 10 and 12 for left-button press, right-button press and center-button press respectively.
Hence in this project the status values are read and compared in the code to identify whether if there is a button has been pressed or not and also identify the button. The details of reading the X-Y value and the status value from a mouse and displaying them in a Serial Monitor window can be found in a previous project on how to interface a PS2 mouse with the Arduino.
Once a button press occurs the code identifies the button from the status value read from the mouse and then turns a LED on or off according to which button has been identified. The LED is controlled with the help of functions pinMode(), delay() and digitalWrite() which are already discussed in a previous project on how to get started with the Arduino andhow to use digital input and output of the Arduino.
Make sure that the files “PS2Mouse.h” and “PS2Mouse.cpp” are kept in the same folder where the .pde file has been saved. When the coding is finished one can verify and upload the code to the Arduino board as explained in the project how to get started with the Arduino and can control the LED using the left button and the right button of the mouse.