Have fun with PS/2: Keyboard and Mouse


Have you heard PS/2 before? Many might be thinking about the PS2 controller when you all see the title of this project. Actually the PS/2 here is the purple color and green color socket at the back of the older desktop computer, for mouse and keyboard connection. Anyway I am using bread board friendly breakout board MiniDIN-6 connector. It breaks out all 6 pins to a 0.1″ spaced connector.

In this tutorial we will be showing that interfacing to PS/2 mouse or keyboard is not as difficult as you imaging. Today I will show you example solution to use the ps/2 keyboard and mouse using the PS/2 protocol and make the program simple so that everyone can understand and use the PS/2 easily.

PS/2 Interface and Protocol

A diagram of the PS2 connector is below.


The serial communication typically runs at a baud rate of 10 KHz to 16.7 KHz. Each byte sent by a device to a controller is transmitted as a packet containing 11 bits. The “device to host” packet has the following bits:

  • 1 start bit (always 0)
  • 8 data bits (least significant bit first)
  • 1 parity bit (odd parity)
  • 1 stop bit (always 1)

For more info please look at the reference slot below .

please note that the LSB and MSB is the same as the normal UART protocol, in fact it is very similar. This is 9-bit transmission with the 9th bit as odd parity bit. But the problem here, the baudrate might change and UART do not have clock line 


Hardware required:

1. SK40C + PIC16F877A




3. KEYBOARD AND MOUSE(which use PS/2 socket)


Software required:

• MPLAB IDE & HITECH C PRO Installation


Related References:

• How to set up MPLAB IDE

• How to Create a project

• How to use PicKit2

PS/2 with mBed

PS/2 Mouse Interface with Arduino

The PS/2 Keyboard Interface

•PS/2 Cytron website



Use the BB-mDIN-6P, interface with PS/2 keyboard and PS/2 mouse. Grab the information and do simple demo. Use PIC16F877A


We will start with PS/2 keyboard and continue with PS/2 mouse.


For the connection of the both keyboard and the mouse are the same. The different is we either plug in keyboard or plug in mouse.

This is the picture of the connection I made for test.


This is the connection picture from Fritzing

Untitled Sketch_bb


Plug in the keyboard to the BB-mDIN-6P



The sample code contains the main body of the programming code. Some function can be referred below.


This is the part where we indicate the result and display at the LCD



Plug in the mouse to the BB-mDIN-6P



This sample code contains the main body of the mouse ps/2 programming code.


This is the part where indicate the result the display at the LCD



Function of the conversion. This means that we swap the 8-bit data making the LSB become MSB and vice versa


Since the serial communication contain the parity bit. This is the part where to check the parity bits. Parity bit is being use to check whether the 8-bit data received are correct.  Some sort of calculation is involve and I will explain later.



The PS/2 mouse and keyboard are widely use in the pass year before USB come out. Is good for us to know how to receive or transmit data from the controller to the mouse.

The basic protocol

For the keyboard, there are not much protocol in it, it is just transmit signal from the keyboard when a button is press follow with clock to indicate the signal like a SPI transmit data. But in this tutorial we are not using SPI to receive the signal since the protocol transmit from the keyboard is 11 bits.

Normally, when we press the button it will transmit a 11bits signal out and when we release it, it will transmit a break signal to notify the key is released.

The hex code for the command is here.

For PS/2 mouse, it is more tricky. When we start up the mouse, it need to be initialize at a certain time limit else it won’t give any signal. Moreover there are setting for the mouse before we could use it to send the signal to controller.

If I am not mistaken, we need to transmit reset, 0xFF to the mouse once receive a package to acknowledgement from the mouse which also contain 11 bits. Then, you can send the setting to the mouse anytime.

Below are some lesson about the mouse protocol,

The default protocol


Mouse Commands

Every command or data byte sent to the mouse (except for the resend command fe) is ACKed with fa. If the command or data is invalid, it is NACKed with fe. If the next byte is again invalid, the reply is ERROR: fc.

Command d0: Read extended ID

Read up to 256 bytes.

Commands d1df: Vendor unique commands

Command d1: Logitech PS/2++ command

This command was to be used, followed by an arbitrary data sequence. Now replaced by the sliced commands using e8.

Command e1: Read secondary ID

Replies with two bytes. An IBM TrackPoint returns 01 as first byte, and a second byte depending on the model.

Command e2: IBM TrackPoint command

Followed by several parameter bytes. For details, see ykt3dext.pdf.

Command e6: Set mouse scaling to 1:1

Often ingredient in magic sequences.

Command e7: Set mouse scaling to 2:1

Often ingredient in magic sequences.

Command e8: Set mouse resolution

This command is followed by a byte indicating the resolution (0, 1, 2, 3: 1, 2, 4, 8 units per mm, respectively). It is used in magic sequences to transport two bits, so that four of these are needed to send a byte to the mouse. See below.

Command e9: Status request

This command returns three bytes:

First a status byte: Bit 7: unused, 0. Bit 6: 0: stream mode, 1: remote mode. Bit 5: 0: disabled, 1: enabled. Bit 4: 0: scaling set to 1:1, 1: scaling set to 2:1. Bit 3: unused, 0. Bit 2: 1: left button pressed. Bit 1: 1: middle button pressed. Bit 0: 1: right button pressed.

Then a resolution byte: 0, 1, 2, 3: 1, 2, 4, 8 units per mm, respectively.

Finally a sample rate (in Hz).

See below for special Synaptics Touchpad handling.

Command ea: Set stream mode

Command eb: Read data

Read a mouse packet. Needed in remote mode to ask the mouse for data. Also functions in stream mode.

Command ec: Clear echo mode

Command ee: Set echo mode

Command f0: Set remote mode

Command f2: Read mouse ID

(Only supported on some systems.) This command reads a 1-byte mouse ID. The reply is a single byte 00. Wait at least 10ms after issuing this command.

For the keyboard reply, see above.

BallPoint (trackball) devices return a single byte 02, Intellimouse returns 03, Explorer Mouse returns 04, 4d Mouse returns 06, 4dplus Mouse returns 08,as does the Typhoon mouse.

Command f3: Set mouse sample rate

(Only supported on some systems.) Set mouse sample rate in Hz. If the given sampling rate is acceptable the ACK is fa. Otherwise the NACK is fe, and the host can correct. If it is incorrect again fc is sent. Correct values are, e.g., 10, 20, 40, 60, 80, 100, 200.

Command f4: Mouse enable

The stream mode mouse data reporting is disabled after a reset and after the disable command. This command enables it again.

Command f5: Mouse disable

This stops mouse data reporting in stream mode. In stream mode, this command should be sent before sending any other commands.

Command f6: Set defaults

If this command is recognized, a reset is done (set sampling rate 100 Hz, resolution 4 counts/mm, stream mode, disabled, scaling 1:1), but no diagnostics are performed. For some enhanced mice that require a magic sequence to get into enhanced mode, this command will reset them to default PS/2 mode.

Command fe: Resend

If this command is recognized, the last mouse packet (possibly several bytes) is resent. There is no ACK to this command, but if the last reply was ACK, it is sent.

Command ffMouse reset

A self-test is performed. When OK, the response is aa 00. On error, the response is fc 00. The mouse is reset to default PS/2 mode.


Transmit and receive data

Here is the link which will let use know clearly how to transmit and receive with a good detail provided.


Still meet problem??

If there are still some parts which don’t understand, please kindly to post in our forum.


· Mouse configuration is set up when it is starting up. Please don’t use reset button if the mouse are not responding. Use the on/off to start up the mouse

· Mouse and keyboard are quite different, keyboard send data once is connected and button is press, mouse would have same function but need to be initialized and the desire timing once it is connected.

· Please note that 5v and gnd wire miss plug will lead to short circuit which will damage the keyboard, mouse and SK40C.




source code:


Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
DIY Automated Vacuum Cleaner Using REKA:BIT With Micro:bit
Rainbow Spark in Mini House using Maker Uno.
TinyML on Arduino using Edge Impulse
Tutorials of Cytron Technologies Scroll to Top