Home     Contact     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
 Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   

6 March 2009
Author: Giorgos Lazaridis
The PS2 protocol

Overview of the PS2 connection

The keyboard that most of you have in front, is using the IBM standard protocol to communicate with your computer. This protocol has the responsibility to send the key scan codes that you press to the pc and get some response commands from it. This means that we are dealing with a bi-directional type of protocol as each device (pc/keyboard) sends and receives commands.

At first, we will take a look at the pin-out of the ps/2 connector (actually it might be also a 5 pins connector but is the same as a ps/2).

The PS2
1. KBD Clock
2. GND
3. KBD Data
4. N/C
5. +5V (VCC)
6. N/C

The PS2
5pins connector
1. KBD Clock
2. KBD Data
3. N/C
4. GND
5. +5V (VCC)

The protocol

As said before, the protocol will take over the communication. This is done using the data and clock lines. These lines are High when no communication takes place (idle). The communication from keyboard to host and from host to keyboard are slightly different so will be explained separately. Keep in mind that the frequency of the clock is about 20-30Khz and is generated by the keyboard but only when a transmission takes place.

From the keyboard to the host

The keyboard is free to send data to the host when both Data and Clock lines are kept high. The keyboard will take the Data line low (Start bit) and then start generating the clock pulses on the Clock line. Each bit is sent in series with the following order:

Start bit => 0...7 data bits => Odd parity bit => Stop bit. (8/O/1)

Each bit is read on the falling edge of the clock so keep it synchronized correctly as seen on the following diagram:

The host has priority number 1 against the keyboard. This means that it can send data to the keyboard whenever it wants. So, before you start sending data to the host make sure it is not ready to transmit to the keyboard (the Clock line must be High). Otherwise both data will be lost.

From the host to the keyboard

The host will take the Clock line Low for about 60uSec. This is more than one bit length and thus the keyboard is prevented from sending data to the host. Then, the Data line will be taken into Low state and the Clock line will be released by the host. The keyboard must now start generating the clock pulses. All data are read from the keyboard after every falling edge excluding the first one that is for synchronization purposes. After receiving the parity bit, the keyboard will send another one pulse and then take the Data line into Low state for the next pulse. This is for acknowledging the reception of new data. If the Data line is not idle (High state) after the reception of the tenth bit, the keyboard will continue sending clock pulses as new data will arrive by the host.

The red line show the state changes driven from the host and the blue line are changes driven from the keyboard.

Sending a key

The keyboard scan codes

The keyboard scan codes

Special keys scan codes

Now what happens when you press a key? Simply, the keyboard will send the scan code of the key that you pressed to the host. The keyboard distinguish two states for the key: the key down state and the key up state. What does this means? The keyboard will send the scan code for the key that you pressed (key down) and when you leave the key up it will send an 'F0' following by the scan code of the key (key up). An exception is made when the key-down state was sent for an extended scan code key. If a key with two bytes scan code (E0.....) was pressed, upon release the keyboard will first send the E0, then the F0 and then the other half of the key scan code. So use extra caution when programming.

For example, if you press the 'A' key, the scan code 1C will be send. When you leave the key, the F0 1C will be sent. If you press the 'A' key and you keep it pressed more than it's typematic delay, the keyboard will keep sending the key's scan code according to it's typematic rate, until the key is released.

The keys do not have simply one byte scan codes. It could be possible as one byte can hold up to 255 different keys but it is far from true. The extended keys have two bytes and the first isE0. As for the pause/break key... the scan code is E1 14 77 E1 F0 14 F0 77.

Relative pages
  • Basic transistor circuits
  • PWM signal theory
  • Learn about the origin of Internet, how it started and the course to what we know today as Internet
  • Learn about the WiFi interferences to the human body
  • How to make a MAMME arcade machine controller without the use of a keyboard
  • Learning PICs @ PCB Heaven On-Line Book

  • Comments


      Email (shall not be published)


    Notify me of new posts via email

    Write your comments below:
    BEFORE you post a comment:You are welcome to comment for corrections and suggestions on this page. But if you have questions please use the forum instead to post it. Thank you.


  • At 25 March 2014, 14:21:47 user Bruce Smith wrote:   [reply @ Bruce Smith]
    • Your diagram of the PS/2 connector could use some changes:

      1. There is no description of whether this is a diagram of the female connector on the host PC or the male connector on the device (mouse or keyboard).
      2. The "notch" in the mini-DIN connector is at the same end as the square polarization tab, not at the opposite end as you have drawn it.
      3. The pin numbers for every other PS/2 connector in the Internet including Wikipedia.org has the pin numbers alternating from side to side instead of around the circle as you have drawn it.

      Perhaps you could change your diagram so that it matches the Wikipedia.org diagram?

  • At 21 December 2010, 10:54:24 user Slam wrote:   [reply @ Slam]
    • Dear Kammenos,

      As far as I know, the data sent to PS/2 mouse are latched on the rising edge of CLK. The data received from PS/2 mouse are latched on the falling edge of CLK instead.

  • At 30 September 2010, 9:43:26 user Kammenos wrote:   [reply @ Kammenos]
    • I think i have not write this quite clear. I mean that data are read on the falling edge of the clock all the time. The phrase "except the first one...." does not mean that the first bit is read on the rising edge. I mean that the first bit is not data (it is the start bit actually).

  • At 30 September 2010, 8:12:42 user tmp wrote:   [reply @ tmp]
    • From the host to the keyboard:
      "All data are read from the keyboard after every falling edge excluding the first one that is for synchronization purposes."

      -- Are you sure this is correct?

  • At 9 June 2010, 17:49:40 user Shourav wrote:   [reply @ Shourav]
    • Thanks a lot. The timing diagram makes things really clear.

  • At 17 March 2010, 6:02:07 user David wrote:   [reply @ David]
    • I do not know if it is helpful of my design.

    reddit this Reddit this

     HOT in heaven!

    NEW in heaven!

    New Theory: AC electric motor working principle

     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS   

    Site design: Giorgos Lazaridis
    © Copyright 2008
    Please read the Terms of services and the Privacy policy