How to Make a Arduino HID Keyboard

37,917

44

23

Introduction: How to Make a Arduino HID Keyboard

About: Great passion for making stuff, Winner of multiple maker awards

Controller by picklesandicecream

In this tutorial I shall explain how you can turn your Arduino chip into a HID keyboard device.

This is done by updating the Firmware on your chip with FLIP.

The cool thing about this trick is that you can make your own game controller or macro keyboard with the power of the Arduino coding method, so possibility are endless for great prototypes.

I have put all files into a Google Drive map for you to download.

link: https://drive.google.com/open?id=1jfOJmFf9C6QCb8I_...

I also have made a youtube Video for the coding part.

Supplies

Arduino UNO or Mega

4 push buttons

4 resitors 1k ohm

Jumper cables

Step 1: Setup of the Arduino

The basic setup of the Arduino for this example. (This can also be done with the Arduino Mega if more pin inputs are required) Note that we use the 3.3v of the Arduino as the input current of the push buttons onto the Digital-pins. The resistors used for this example are 1k ohm resistors.

Step 2: CODE - How to Code a Key Press Onto the Arduino

Video link: https://www.youtube.com/watch?v=j05vj8zRP1o

Step 3: CODE - Write a Keyboard Buffer

First we initialize a keyboard buffer, this is required for the Arduino to send a bit register as a HID Keyboard.

Step 4: CODE - Define the Pins for Input Into the Arduino

Define the pins of the Arduino, you can also use (const int PIN_W = 4;) but I prefer the #define method.

Step 5: CODE - Write the Setup Function

Write the setup function, as with every Arduino code project the setup is pretty much the same except that setting the Baud rate and serial communication with the (Serial.begin(9600)) function is required for the Arduino to communicate with your computer.

Step 6: CODE - Write a Button Release

Write an end buffer method to send a bit when button is released, this step is required to end the data stream of the HID keyboard. For this example we send the key code through buf[2] so we need to reset them to 0 when the button on the Arduino is released. Note: Without this step you’re HID keyboard can start sending button inputs but it will never stop sending bits until the HID keyboard (Arduino) USB is unplugged.

Step 7: CODE - Write a Keypress

This step is done in the Loop() function to be updated every cycle the Arduino get through checking if a button is pressed.

Step 8: Complete Code for Reference

uint8_t buf[8] = { 0 }; //Keyboard report buffer

#define PIN_W 4 // Pin for w

#define PIN_A 5 // Pin for a

#define PIN_S 6 // Pin for s

#define PIN_D 7 // Pin for d

void setup() {

Serial.begin(9600); // Setup Serial communication

//Set pinmode of Input pins

pinMode(PIN_W, INPUT);

pinMode(PIN_A, INPUT);

pinMode(PIN_S, INPUT);

pinMode(PIN_D, INPUT);

}

void loop() {

//When button representing W is pressed

if (digitalRead(PIN_W) == HIGH) {

buf[2] = 26; // W keycode

Serial.write(buf, 8); // Send keypress

releaseKey();

}

//When button representing A is pressed

if (digitalRead(PIN_A) == HIGH) { buf[2] = 4; // A keycode

Serial.write(buf, 8); // Send keypress

releaseKey();

}

//When button representing S is pressed

if (digitalRead(PIN_S) == HIGH) { buf[2] = 22; // S keycode

Serial.write(buf, 8); // Send keypress

releaseKey();

}

//When button representing D is pressed

if (digitalRead(PIN_D) == HIGH) { buf[2] = 7; // D keycode

Serial.write(buf, 8); // Send keypress

releaseKey();

}

}

// Function for Key Release

void releaseKey() {

buf[0] = 0;

buf[2] = 0;

Serial.write(buf, 8); // Send Release key

}

Step 9: Install Flip

To install FLIP please install JRE - Flip Installer - 3.4.7.112.exe provided in the Google Drive link

Note: When starting up you get the error "AtLibUsbDfu.dll not found" you have to install a driver.

Here is a solution made by MDGrein link: https://www.youtube.com/watch?v=KQ9BjKjGnIc

Step 10: FLIP - Device Selection

Press the Highlighted chip button, then Select ATmega16u2 and then the button OK. As this is the chipset for the Arduino Uno R3 and the Arduino Mega R3.

And now you are done with setting up flip and we can go to the Next step of Flashing the Arduino.

Step 11: Upload Your Arduino Code

You will get a message at the bottom from the Arduino IDE saying done Uploading

Note: if uploading fails
Check 1 COM port under Tools>port if none is available unplug and replug USB or check device manager. Check 2 if you have the correct board selected in Tools>board.

Step 12: Arduino Into DFU Mode

short the 2 ICSP2 pins as shown in the image

To put the Arduino in the DFU (Device Firmware Update) mode, so that FLIP can access the Firmware

on the Arduino Chip. More info on this reference link: https://www.arduino.cc/en/Hacking/DFUProgramming8...

Step 13: FLIP - Press the USB Icon in FLIP and Then Open

Step 14: FLIP - Parse the Firmware HEX

Load the arduino-keyboard.hex into FLIP

You will see at the bottom USB ON, this is that the Arduino is connected through DFU Mode.

Step 15: FLIP - Press RUN Not Start Application

Step 16: FLIP - Unplug and Replug USB

You will see this all in green if not close FLIP and return to step 12 Arduino into DFU mode.

Step 17: How to Check If the Flash Worked.

From now on the Arduino is a HID Keyboard Congratulations!

The only thing now is that the Arduino IDE does not support your Arduino anymore. So to check if your HID keyboard works we open Notepad, or any other text editor.

Step 18: Want Your Arduino Back :(

You can do that by repeating the steps after Step 12 Arduino into DFU mode,

only then chose Arduino-usbserial-uno.hex instead of arduino-keyboard.hex

2 People Made This Project!

Recommendations

  • Unusual Uses Contest

    Unusual Uses Contest
  • Tiny Things Speed Challenge

    Tiny Things Speed Challenge
  • Reclaimed Materials Contest

    Reclaimed Materials Contest

23 Comments

0
filipjunek730
filipjunek730

13 days ago

What is the code for the left windows key?

0
SuperKillaRed
SuperKillaRed

Question 5 months ago

Is there any way to code it so it only sends one input when held instead of it repeatedly when held?

0
TiTO4
TiTO4

Answer 5 weeks ago

Hey. Did you manage to find a solution? I have the same problem...

0
Zeracool
Zeracool

Question 9 months ago

Is this possible to do the same steps with Leonardo?

0
Tomas Kucera
Tomas Kucera

Question 10 months ago

I am having an issue opening the HEX file(s). Getting Cannot open file error (see the screenshot).

Running Win10, FLIP from the archive and the HEX as well.

Tried different boards but it looks like the issue is somewhere else. :/

Any idea? Thanks in advance!

Capture.PNG
0
Rimbo05
Rimbo05

Question 1 year ago

In this example you are using the W,A,S,D keys and using certain keycodes for each. Sorry to comment on an old post but i couldnt figure out a pattern for the keycodes, how can I find out what keycode each key has? I want to make some modifications for it to suit my needs but i need other/more keys.

0
Rimbo05
Rimbo05

Reply 1 year ago

I get a 404 page not found error, also i made a prototype using only one button and it didnt work, it would write the letter like the button was pressed down when i wasnt even near the circuit, i made sure everything was wired correctly and still had the same problem, also it stopped and started writing again without me interacting with it.

0
Gen_Mudal
Gen_Mudal

1 year ago on Step 7

Why is buf[2] used? Why not buf[3] or other indices?

0
Eward Hage
Eward Hage

Reply 1 year ago

So you can use shift+f for example to get F.
Note the shift needs to be in buf[2] and f in buf[3]

So shift before the letter key

0
Gen_Mudal
Gen_Mudal

Reply 1 year ago

Thanks. Must buf[2] be involved in sending the characters?

0
Gen_Mudal
Gen_Mudal

Reply 1 year ago

Thanks once again. If one number can send the characters, then what's the purpose of the other numbers in the array

0
Gen_Mudal
Gen_Mudal

Reply 1 year ago

Alright :)

0
Gen_Mudal
Gen_Mudal

Reply 1 year ago

I don't really understand the keyboard report buffer part of the code, that's why these questions are popping up.

0
Eward Hage
Eward Hage

Reply 1 year ago

Well delete the buffer and find out for yourself.

0
Eward Hage
Eward Hage

Reply 1 year ago

nope, can be any number in the buffer array

0
Shaikh Amaan FM
Shaikh Amaan FM

1 year ago

How to send multiple key press. For Example how can I send ctrl+c or shift+tab.

0
Eward Hage
Eward Hage

Reply 1 year ago

If you want to do a multiple keystroke, for example ctrl+c you can use the buffer feed

So example
Ctrl in buf[2] and c in buf[3]
Note that you need to put the Ctrl in buffer before c.

You can do this automaticly in the code but my example code is for easy use so a dummy can also understand it.

0
Shaikh Amaan FM
Shaikh Amaan FM

Reply 1 year ago

Thank you so much.✌🏻