Connect A USB Keyboard With An Arduino

Hackster.ioLearning Hardware Community. Projects× Log inSign up ProjectsProjectsChannelsChannelsNewsContestsEventsVideos ×

Embed the widget on your own site

Add the following snippet to your HTML:

Connect a USB Keyboard with an Arduino

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

Read up about this project on Hackster.io

Connect a USB Keyboard with an Arduino

Doctor Volt28 40,410
  • Overview
  • Things
  • Story
  • Schematics
  • Code
  • Credits
  • Comments(8)
Doctor VoltDoctor VoltPublished © MITConnect a USB Keyboard with an Arduino

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

BeginnerProtip1 hour40,410Connect a USB Keyboard with an Arduino

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
Or any other microcontroller
×1
  • Buy from Newark
  • Buy from CPC
Breadboard (generic)
Breadboard (generic)
×1
  • Buy from Newark
  • Buy from SparkFun
Jumper wires (generic)
Jumper wires (generic)
×1
  • Buy from Newark
  • Buy from SparkFun
USB or PS/2 Keyboard
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

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.

Read more

Schematics

Breadboard

Schematic

Code

  • Arduino sketch

Arduino sketch

C/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);*/ }

Credits

Doctor Volt

Doctor Volt

20 projects • 131 followersFollow ContactContact

Comments

Related channels and tags
  • embedded
  • keyboard
  • microcontroller
  • usb

Tag » Arduino V-usb Keyboard