Tutorial: How to Use Arduino Uno as HID | Part 1: Arduino Keyboard Emulation

Introduction

Arduino boards are pre-configured to function as serial device so to emulate keyboard, we will reconfigure it as Human Interface Device (HID) so our computer will see it as keyboard.

Circuit Diagram

Video Demonstration

Source Code

  1
  2uint8_t buf[8] = {
  3  0
  4};  // Keyboard Report Buffer: 8 bytes
  5
  6#define PIN_W A0
  7#define PIN_A A1
  8#define PIN_S A2
  9#define PIN_D A3
 10
 11//#define SERIAL_DEBUG  // for serial debug: remove //
 12                        // for actual HID: put //
 13
 14bool currState_W = HIGH;
 15bool currState_A = HIGH;
 16bool currState_S = HIGH;
 17bool currState_D = HIGH;
 18          
 19bool prevState_W = HIGH; 
 20bool prevState_A = HIGH; 
 21bool prevState_S = HIGH; 
 22bool prevState_D = HIGH; 
 23
 24unsigned long prevTime_W = 0;
 25unsigned long prevTime_A = 0;
 26unsigned long prevTime_S = 0;
 27unsigned long prevTime_D = 0;
 28
 29unsigned long waitTime_W = 50;
 30unsigned long waitTime_A = 50;
 31unsigned long waitTime_S = 50;
 32unsigned long waitTime_D = 50;
 33
 34void setup() 
 35{
 36  Serial.begin(9600);
 37
 38  pinMode(PIN_W, INPUT_PULLUP);
 39  pinMode(PIN_A, INPUT_PULLUP);
 40  pinMode(PIN_S, INPUT_PULLUP);
 41  pinMode(PIN_D, INPUT_PULLUP);
 42  
 43  delay(200);
 44}
 45
 46void loop() 
 47{
 48  checkButton();
 49}
 50
 51
 52void checkButton() {
 53
 54  bool currRead_W = digitalRead(PIN_W);
 55  bool currRead_A = digitalRead(PIN_A);
 56  bool currRead_S = digitalRead(PIN_S);
 57  bool currRead_D = digitalRead(PIN_D);
 58
 59  if (currRead_W != prevState_W) {
 60    prevTime_W = millis();
 61  }
 62  if (currRead_A != prevState_A) {
 63    prevTime_A = millis();
 64  }
 65  if (currRead_S != prevState_S) {
 66    prevTime_S = millis();
 67  }
 68  if (currRead_D != prevState_D) {
 69    prevTime_D = millis();
 70  }
 71
 72  if ((millis() - prevTime_W) > waitTime_W) {
 73    if (currRead_W != currState_W) {
 74      currState_W = currRead_W;
 75      if (currState_W == LOW) {
 76        // Send the code
 77        buf[2] = 26;    // HID: W key
 78#ifdef SERIAL_DEBUG
 79        buf[2] = 'W';     // Serial: W key
 80#endif
 81        Serial.write(buf, 8); // Send keypress
 82      } else {
 83        // Release the keyboard
 84        releaseKey();
 85      }
 86    }
 87  }
 88  if ((millis() - prevTime_A) > waitTime_A) {
 89    if (currRead_A != currState_A) {
 90      currState_A = currRead_A;
 91      if (currState_A == LOW) {
 92        // Send the code
 93        buf[2] = 4;   // HID: A key
 94#ifdef SERIAL_DEBUG
 95        buf[2] = 'A';   // Serial: A key
 96#endif
 97        Serial.write(buf, 8); // Send keypress
 98      } else {
 99        // Release the keyboard
100        releaseKey();
101      }
102    }
103  }
104  if ((millis() - prevTime_S) > waitTime_S) {
105    if (currRead_S != currState_S) {
106      currState_S = currRead_S;
107      if (currState_S == LOW) {
108        // Send the code
109        buf[2] = 22;    // HID: S key
110#ifdef SERIAL_DEBUG
111        buf[2] = 'S';     // Serial: S key
112#endif
113        Serial.write(buf, 8); // Send keypress
114      } else {
115        // Release the keyboard
116        releaseKey();
117      }
118    }
119  }
120  if ((millis() - prevTime_D) > waitTime_D) {
121    if (currRead_D != currState_D) {
122      currState_D = currRead_D;
123      if (currState_D == LOW) {
124        // Send the code
125        buf[2] = 7;   // HID: D key
126#ifdef SERIAL_DEBUG        
127        buf[2] = 'D';   // Serial: D key
128#endif
129        Serial.write(buf, 8); // Send keypress
130      } else {
131        // Release the keyboard
132        releaseKey();
133      }
134    }
135  }
136
137  prevState_W = currRead_W;
138  prevState_A = currRead_A;
139  prevState_S = currRead_S;
140  prevState_D = currRead_D;
141
142}
143
144void releaseKey() 
145{
146  buf[0] = 0;
147  buf[2] = 0;
148  Serial.write(buf, 8); // Release key  
149}

References And Credits

Download Links Arduino Uno R3 Original Firmware
Arduino Uno Keyboard HID Firmware

For more H.I.D. key values, please refer to:
https://www.freebsddiary.org/APC/usb_hid_usages

Call To Action

If you found this tutorial as helpful, please share this to your friends so that many could benefit from it. Don’t forget to Subscribe to TechToTinker Youtube channel: Click me to SUBSCRIBE

Thank you and have a good day.



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.