I'm having difficulties getting the PJRC PS2 keyboard library working reliably with the Arduino Leonardo. I'm relatively new to using the Arduino, but this library seems to be the recommended way to read PS2 keystrokes.

When I run the example sketch to echo keystrokes to the serial terminal, I get about 50% of the characters echoed correctly. The other half typically consists of random characters. In addition, I'd say only 30% of the keystrokes are actually detected at all. I tested many keys on the keyboard to see if there was a mechanical fault but this mis-identification and detection lag happened to all of them.

I get very different results using code from this blog which just implements the PS2 protocol in a barebones way. All of the scan codes echoed from this program are consistent (and correct after I manually looked them up) and there is no lag in detection.

So I could implement my own lookup tables with the second example but I'm curious if there is a known fix for the PJRC lib. There are more features in that library that I would like to make use of.

  • \$\begingroup\$ the example sketch I mention is the one in the PJRC lib here. I couldn't link it originally because new users get a max of two links per post.. \$\endgroup\$
    – Matt
    Commented Dec 9, 2012 at 8:44
  • \$\begingroup\$ I took a look at the PJRC library code and didn't see anything obvious. Are you using the same pins for the PS2 connections in both cases? \$\endgroup\$
    – Dave Tweed
    Commented Dec 9, 2012 at 15:05
  • \$\begingroup\$ Thanks. Yes, I used pin 9 for DATA and pin 3 for CLK in both cases. \$\endgroup\$
    – Matt
    Commented Dec 9, 2012 at 18:16

1 Answer 1


I got the PJRC lib working through a stroke of luck -- I don't understand why this works. In the example sketch I use these definitions:

const int DataPin = 9;
const int IRQpin =  3;

But when I actually make the physical connections, I have to put the IRQpin (aka CLK) on pin 2 of the Leonardo. (DATA stays on pin 9 as expected based on the software definitions.) This software setup and the seemingly-wrong connection is letting me use the library as expected.

When I do move the CLK wire from the keyboard over to pin 3 I get the delayed and incorrect measurements mentioned in the question.

So I'm pretty confused as to why this mismatch in software and wiring works, but I'll keep searching..

Edit: Got things working properly. The Leonardo was not supported in the 2.3 version of the PJRC lib. I added some of the missing interrupt definitions in my fork.

Edit 2: Paul of PJRC told me that Leonardo support is now available in the PS2Keyboard lib he hosts.

  • \$\begingroup\$ The reason for the disconnect between the pin-mappings is because the arduino people decided to randomly define their own numbering scheme for the ATmega MCU pins. The mechanism the PS2 library uses to determine which arduino "pin" number equates to which real IO pin on the micro does not evaluate to the correct hardware IO line, and as a result, the IO lines are mis-mapped. The fact that the DATA pin stays the same simply is a coincidence that whichever incorrect definition the PS2 library is defaulting to happens to be correct for that IO. \$\endgroup\$ Commented Mar 29, 2013 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.