Connect a USB Keyboard with an Arduino

Connect a USB Keyboard with an Arduino © MIT

Ever thought about interfacing a USB keyboard with an Arduino in one of your projects? In fact, it is easier than you might think.

  • 4,509 views
  • 4 comments
  • 12 respects

Components and supplies

Ard nano
Arduino Nano R3
Or any other microcontroller
×1
12002 04
Breadboard (generic)
×1
11026 02
Jumper wires (generic)
×1
USB or PS/2 Keyboard
×1

Apps and online services

About this project

USB keyboards still support the legacy serial PS/2 protocol. Therefore these can easily used in Arduino projects. Schematic and code below should work with all Atmega AVR based Arduino boards like Uno, Nano, Mega etc.

This is rather an example of how to integrate USB or PS/2 keyboards in a simple and resource-saving way. Therefore there is no support of modifier keys like Shift or Alt there. There is however a fully functional PS/2 library available through the Arduino IDE library manager.

There seem to be some newer USB keyboards around, that no more support the PS/2 protocol, though.


Code

Arduino sketchC/C++
#define CLOCK 6 //D-
#define DATA 7  //D+

#include <LiquidCrystal.h>  //Best imported by library manager

const char keymap[] = {
  0, 0,  0,  0,  0,  0,  0,  0,
  0, 0,  0,  0,  0,  0, '`', 0,
  0, 0 , 0 , 0,  0, 'q','1', 0,
  0, 0, 'z','s','a','w','2', 0,
  0,'c','x','d','e','4','3', 0,
  0,' ','v','f','t','r','5', 0,
  0,'n','b','h','g','y','6', 0,
  0, 0, 'm','j','u','7','8', 0,
  0,',','k','i','o','0','9', 0,
  0,'.','/','l',';','p','-', 0,
  0, 0,'\'', 0,'[', '=', 0, 0,
  0, 0,13, ']', 0, '\\', 0, 0,
  0, 0, 0, 0, 0, 0, 127, 0,
  0,'1', 0,'4','7', 0, 0, 0,
  '0','.','2','5','6','8', 0, 0,
  0,'+','3','-','*','9', 0, 0,
  0, 0, 0, 0 };

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup()
{
  Serial.begin(115200);
  pinMode(CLOCK, INPUT_PULLUP); //For most keyboards the builtin pullups are sufficient, so the 10k pullups can be omitted
  pinMode(DATA, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  lcd.begin(20, 4);
  lcd.cursor();
  lcd.blink();
  bitSet(PCICR, PCIE2); // Enable pin change interrupts on pin D0-D7
  bitSet(PCMSK2, CLOCK); // Pin change interrupt on Clock pin
 }

uint8_t lastscan;
uint8_t line = 0, col = 0;


ISR(PCINT2_vect)
{
  uint16_t scanval = 0;
  for(int i = 0; i<11; i++)
  {
    while(digitalRead(CLOCK));
    scanval |= digitalRead(DATA) << i;
    while(!digitalRead(CLOCK));
  }
  scanval >>= 1;
  scanval &= 0xFF;
  Serial.println(scanval, HEX);
  if(lastscan != 0xF0 && scanval != 0xF0)
  switch(scanval)
  {
    case 0x5A: //Enter
      lcd.setCursor(0, ++line & 0x03);
      col = 0;
      break;
    case 0x66: //Backspace
      lcd.setCursor(--col, line);
      lcd.write(' ');
      lcd.setCursor(col, line);
    break;
    default:
      lcd.write(keymap[scanval]);
      col++;
  }
  lastscan = scanval;
  bitSet(PCIFR, PCIF2);
}

void loop()
{
  /*digitalWrite(13, LOW);
  delay(500);  
  digitalWrite(13, HIGH);
  delay(500);*/
}

Schematics

Breadboard
Fritzing sketch bb gv9htsrgsq
Schematic
Fritzing sketch schem 7ahrodnagi

Comments

Similar projects you might like

How to connect two 16*2 LCD together with arduino

Project tutorial by Shashwat Raj

  • 2,654 views
  • 0 comments
  • 7 respects

Keyboard Melodies

Project tutorial by Ava Baker -21 and Natalie Clark -20

  • 10,711 views
  • 3 comments
  • 13 respects

Connect USB Joystick to Commodore C64

Project tutorial by DocSnyderde

  • 4,294 views
  • 0 comments
  • 5 respects

How to make Arduino Piano keyboard

Project tutorial by Sharifdeen Ashshak

  • 10,015 views
  • 2 comments
  • 13 respects

Using I2C Communication Protocol to Connect 6 Arduino Megas

Project showcase by Sherwin Chiu

  • 11,295 views
  • 12 comments
  • 31 respects
Add projectSign up / Login