Connect A USB Keyboard With An Arduino
Maybe your like
Embed the widget on your own site
Add the following snippet to your HTML:

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 
Connect a USB Keyboard with an Arduino
Doctor Volt28 40,410Ever 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,410Things used in this project
Hardware components | ||||||
![]() |
| × | 1 |
| ||
![]() |
| × | 1 |
| ||
![]() |
| × | 1 |
| ||
| × | 1 | ||||
Software apps and online services | ||||||
![]() |
| |||||
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 moreSchematics
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
20 projects • 131 followersFollowComments
Related channels and tags
- embedded
- keyboard
- microcontroller
- usb
Tag » Arduino V-usb Keyboard
-
Julianschuler/USBKeyboard: Arduino Library To Add HID ... - GitHub
-
Connect A USB Keyboard With An Arduino - Arduino Project Hub
-
Connect Arduino UNO To USB Keyboard
-
V-USB And Arduino - Microcontrollers
-
USB-Keyboard With Arduino And V-USB Library, An Example - Pinterest
-
USB-Keyboard With Arduino And V-USB Library, An Example
-
Arduino UNO As A USB Keyboard (HID Device) [Anything ... - YouTube
-
Arduino V-USB / HID 14 Channel Data Logger
-
How To Make A Arduino HID Keyboard : 18 Steps - Instructables
-
Learn More | Trinket USB Keyboard - Adafruit Learning System
-
Vusb Arduino Shield - Fritzing
-
A Firmware-Only USB Driver For Atmel(r) AVR(r) Microcontrollers
-
Usb-keyboard · GitHub Topics
-
How To Use The V-USB Library To Emulate A Keyboard On The Arduino ...



