Saturday, December 3, 2011

PS2 keyboard interface with arduino

PS2 Keyboards are easy to interface, requiring only 5 volt power and 2 signals.

The keyboard's clock signal must connect to an interrupt pin. The data signal may connect to any pin, but do not use the pin with an LED connected to ground because the LED current will interfere with the data signal.

Basic Usage

PS2Keyboard keyboard;
Create the keyboard object. Even though you could create multiple objects, only a single PS2 keyboard is supported by this library.
keyboard.begin(DataPin, IRQpin)
Begin the receiving keystrokes. DataPin and IRQpin are the pin numbers where you connected the PS2 keyboard's Data and Clock signals.
Check if a keystroke has been received. Returns true if at least one keystroke.
Read the next keystroke. -1 is returned if no keystrokes have been received. Keystrokes are returned as ASCII characters. Special keys are mapped to control characters. 

Example Program


#include <PS2Keyboard.h>

const int DataPin = 8;
const int IRQpin =  5;

PS2Keyboard keyboard;

void setup() {
  keyboard.begin(DataPin, IRQpin);
  Serial.println("Keyboard Test:");

void loop() {
  if (keyboard.available()) {
    // read the next key
    char c =;
    // check for some of the special keys
    if (c == PS2_ENTER) {
    } else if (c == PS2_TAB) {
    } else if (c == PS2_ESC) {
    } else if (c == PS2_PAGEDOWN) {
    } else if (c == PS2_PAGEUP) {
    } else if (c == PS2_LEFTARROW) {
    } else if (c == PS2_RIGHTARROW) {
    } else if (c == PS2_UPARROW) {
    } else if (c == PS2_DOWNARROW) {
    } else if (c == PS2_DELETE) {
    } else {
      // otherwise, just print all normal characters

PS2 Connector Pinout

PS2 Library Link :-




1 comment: