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
- How to Get Started with ATTiny85 in Arduino IDE
- Tutorial: How to use MFRC522 RFID module using Arduino
- SOS Flasher Using Millis Function with Enable Switch
- Tutorial: How to use DS3231 RTC in Arduino
- Tutorial: How to use 0.96 OLED - a small and cute display
- Tutorial: Getting Started with the NRF24L01 | How to use | Arduino
- Tutorial: How to use SIM800L GSM Module for Controlling Anything | Arduino
- Tutorial: How to use Keypad | Text Entry Mode | Arduino
- Tutorial: How to use 4x4 Keypad | Arduino
- Project Idea: Arduino Voltmeter
- Project Idea: Door Lock Security | Arduino
- Multitasking with Arduino | Relay Timer Controller | using millis
- Tutorial Understanding Blink Without Delay | How to millis
- Arduino Simple LCD Menu
- How to use SIM800L GSM Module using Arduino | Make or Answer Voice Calls
- Tutorial: How to Use Arduino Uno as HID | Part 2: Arduino Mouse Emulation
- Tutorial: How to use SIM800L DTMF to Control Anything | Arduino
- Tutorial: Arduino EEPROM
- How to use SIM800L GSM Module | Arduino | Send and Receive SMS
- 16x2 LCD Menu for Arduino
- Tutorial: Arduino GPIO | How to use Arduino Pins
- MIT App Inventor for Arduino
- RC Car using L298N, HC-06, and Arduino Uno
- How to Use LCD Keypad Shield for Arduino
- How to Use Arduino Interrupts
- Project: Automatic Alcohol Dispenser
- TUTORIAL: How to use HC-SR04 Ultrasonic Sensor with Arduino
- Source Code: Astronomia Meme and Funeral Dance | melodies the Arduino way
- How to Get Started with L293D Motor Driver Shield with Arduino
- How to Get Started with L298N Motor Driver module using Arduino
- Part 2: Wav Music Player with Lyrics Using Arduino and SD Card
- Interfacing Infrared to Arduino Uno
- Part 1: Wav Music Player Using Arduino Uno and SD Card
- How to Interface Stepper Motor to Arduino Uno
- How To Play MP3 Files on Arduino from SD Card
- What is Arduino Software Serial
- How to Interface SD card to Arduino (without SD card shield)?
- Playing Melodies Using Arduino
- 8 Degrees Of Freedom (DOF) Robot Using Arduino Uno
- How to Interface PS2 Controller to Arduino Uno
- Part 3: DF Player Mini Tinkering with Arduino Nano and LCD
- How to Interface HC-06 to Arduino
- How to make a Remote Control RC car using Arduino and HC-06 bluetooth module
- Part 2: DF Player Mini Tinkering with Arduino Nano
- Part 1: DF Player Mini - a mini cheap mp3 player
No comments yet!