Connect PS/2 Keyboard to Arduino

40,009

73

13

Introduction: Connect PS/2 Keyboard to Arduino

About: My life is Not only Writing Lines of Codes, But gaining something good. I'm simple in physical world but Very complex in ICT word.

Hi everyone, this is also an Interesting project that brings 106 Inputs to your Arduino. Can't believe? Follow the project and see how this happens with a PS/2 Keyboard.

OK First of all you need

  • Arduino (UNO)
  • PS/2 Keyboard
  • PS/2 Keyboard connector

Step 1: Keyboard Conection

Following is the pin-out of the Connector. There are 4 wires coming from the keyboard and their connections to arduino Digital pins are as follows.

  • 5V :- Arduino 5V out
  • Ground :- Arduino GND
  • Clock :- Arduino Pin 3
  • Data :- Arduino Pin 8

Step 2: Code

First include this library to Arduino Software. http://www.pjrc.com/teensy/arduino_libraries/PS2Keyboard.zip

#include < PS2Keyboard.h>

const int DataPin = 8; const int IRQpin = 3;

PS2Keyboard keyboard;

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

void loop() { if (keyboard.available()) { // read the next key char c = keyboard.read(); // check for some of the special keys if (c == PS2_ENTER) { Serial.println(); } else if (c == PS2_TAB) { Serial.print("[Tab]"); } else if (c == PS2_ESC) { Serial.print("[ESC]"); } else if (c == PS2_PAGEDOWN) { Serial.print("[PgDn]"); } else if (c == PS2_PAGEUP) { Serial.print("[PgUp]"); } else if (c == PS2_LEFTARROW) { Serial.print("[Left]"); } else if (c == PS2_RIGHTARROW) { Serial.print("[Right]"); } else if (c == PS2_UPARROW) { Serial.print("[Up]"); } else if (c == PS2_DOWNARROW) { Serial.print("[Down]"); } else if (c == PS2_DELETE) { Serial.print("[Del]"); } else { // otherwise, just print all normal characters Serial.print(c); } } }

Step 3: Testing

So we have finished our coding, Upload it to arduino and keep the Arduino connected to PC.Then open the Serial Monitor on Arduino Software and Press some keys on the Keyboard connected to Arduino and you will see It prints what you type on that keyboard.Comment your ideas.

4 People Made This Project!

Recommendations

  • Puzzles Speed Challenge

    Puzzles Speed Challenge
  • "Can't Touch This" Family Contest

    "Can't Touch This" Family Contest
  • CNC Contest 2020

    CNC Contest 2020

13 Discussions

0
Dr Bee
Dr Bee

Question 7 months ago

Hey Man. This worked beautifully. i am actually quite impressed with how responsive it is.
my only problem is.. well... it only works with certain keys. when I want it to work with all of them. When I tried to modify the code to work with all of it, it kept giving me weirs errors and such. so if you can respond with updated code or a reason that my modifications are not working, that would be great. (for bonus points, can you show me how to make an arduino respond to a PS/2 Mouse as well? many thanks.)

0
quadracycle4000
quadracycle4000

Question 4 months ago

Does this allow hot-swapping? I want to use the keyboard only when needs be.

0
kkant2291
kkant2291

Question 1 year ago

whether this work with arduino mega and uno?

0
simone.mastroianni.04

Please help me! I followed all the steps and did not succeed! I attach you to the pictures

IMG_20170617_124446.jpgIMG_20170617_124453.jpgIMG_20170617_124511.jpg
0
simone.mastroianni.04
simone.mastroianni.04

Reply 3 years ago

And The keyboard sometimes blink when is connected to arduino

0
Victor Bezerra
Victor Bezerra

Reply 2 years ago

Did you get a solution? I have the same problem.

0
simone.mastroianni.04
simone.mastroianni.04

Reply 2 years ago

I resolved. My keyboard was fried because I inverted + and - I tried with another keyboard and SUCCESS! :)

0
AliK140
AliK140

2 years ago

Hi sir,

I have pannel look like keyboard ..can you help to interface with it..Thank you

0
susyrobl.
susyrobl.

3 years ago

excelente!, muchas gracias, funciona perfecto!, pero no con adaptador USB a PS/2.

0
jmacias2
jmacias2

3 years ago

It works nicely, thanks

0
Gabrielin
Gabrielin

3 years ago

My Keyboard doesn't seem to turn on when connecting the 5V and GND, but it's an RJ12 keyboard, so I'm not really sure if it's the same pinout (I looked up the pinout of a PS/2 to RJ12 adaptor and it seemed pretty straightforward)

0
KarínS5
KarínS5

4 years ago

hi, I have done this proyect and it doesn´t work :c