30

Is it possible to build a HID device (like a keyboard) using an Arduino uno?

At the time being, I have button inputs on the Arduino giving outputs on the serial line. So, how can I transform my current firmware into something that can behave like a HID keyboard?

  • Shall I write a new window device driver?
  • Shall I create a software layer that could take my serial data and have windows read it as HID input?
  • Is there any better way to achieve this, if it is possible at all?
6
  • 1
    I may be wrong, but this is not really about Arduino. Its about Window's side device driver development.
    – asheeshr
    Feb 27, 2014 at 14:41
  • 1
    First answer in google : mitchtech.net/arduino-usb-hid-keyboard ... but you did not specify which Arduino. On a Leonardo it's builtin, and the appropriate driver already exists (see arduino.cc/en/Reference/MouseKeyboard).
    – FredP
    Feb 27, 2014 at 14:42
  • I'm using an Uno, but I'll look at what you posted because last time we considered the DFU update but we're kinda scared of bricking stuff as the components aren't as readily available and cheap in my area as in most places. Feb 27, 2014 at 14:49
  • 3
    @AsheeshR This issue is on meta now: meta.arduino.stackexchange.com/q/102/46
    – Ricardo
    Feb 27, 2014 at 16:17
  • 1
    Well, that's definitely possible, as the Arduino Uno features an Atmega16u2 as USB driver chip, the user can use either DFU or ICSP flashing to push on the device a firmware close to the leonardo's which will make the Arduino behave as a HID keyboard. There's no windows development involved, no driver to be built as USB/HID devices are recognized per default. i.e. that question should be reopened.
    – zmo
    Mar 1, 2014 at 13:46

2 Answers 2

16

Is it possible to build a HID device (like a keyboard) using an Arduino uno?

yes it is! And there's a great tutorial on how to do that over here

At the time being, I have button inputs on the Arduino giving outputs on the serial line. So, how can I transform my current firmware into something that can behave like a HID keyboard?

As the tutorial shows you, you need to transform your firmware code so instead of printing the events, you actually trigger events. What you need to do, is add to your code:

void triggerKey(uint8_t mod, uint8_t chr) {
    uint8_t buf[8] = {mod, chr, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    // press key
    Serial.write(buf, 8);
    // emulate key press delay
    delay(10);
    buf[1] = 0x00;
    // release key
    Serial.write(buf, 8);
}

What you're doing here, is send from your main Arduino microcontroller events over the serial line to the smaller microcontroller that handles USB communication. That microcontroller will take those key "buffers" and transfer them as USB HID Keyboard key presses over USB.

Beware that the key character (chr in the code here), is actually a keycode sent over USB that gets interpreted by your USB HID driver, which then translates it into a real character using the current layout you've setup for your keyboard.

The mod character is one of the modifier keys you have on the keyboard (shift, control, alt...), 0x00 being no modifier.

  • Shall I write a new window device driver?

No, you don't need! With the DFU upgrade, your arduino will be seen as a USB keyboard by windows.

  • Shall I create a software layer that could take my serial data and have windows read it as HID input?

You neither need to do that, and it's actually a very bad idea for many reasons:

  • your solution would not be portable, and thus you'd need to hack something for every platform you may use,
  • your solution would imply a lot of new code ; the more code one write, the more bugs is likely to be in your system,
  • your solution would be really hackish, as it would behave as a device driver (i.e. kernel land) taking input from a user program (i.e. user land) and is likely to not work, be a hell to debug, and prone for security issues... If that's even possible to do!

I'm using an Uno, but I'll look at what you posted because last time we considered the DFU update but we're kinda scared of bricking stuff

Finally you're having concerns, in a comment, about bricking your arduino. Please believe me and the author of the article that you cannot brick your arduino doing a firmware flash:

NOTE: There is no chance of ‘bricking’ the device using this method. The Arduino bootloader firmware can always be updated using the DFU protocol!

In case something gets really badly wrong, and you get a bootloader corruption in the little AVR (atmega8u2), you still can reflash it using another arduino or an atmel programmer using the 6 pins next to the USB plug.

In the end, know that the only way to brick an AVR, thus an Arduino as well, is to play with the fuses and reset the programming fuses. And even then, you may use HVSP to save your device, though it's often less trouble to just take a new chip.

6
  • 1
    Thanks for all the useful info, we ended up achieving this. Mar 11, 2014 at 12:08
  • Well, the proper way to brick an AVR is with over current / over voltage. Then it will be toasted...
    – user2973
    Aug 7, 2014 at 12:38
  • 1
    But then, it's not called bricking, but frying it, or as you say toasting it. Put 15amps through the MCU, and I bet you'll see it jump through the room ;-)
    – zmo
    Aug 8, 2014 at 13:27
  • How to release a specific key? I want to emulate a key HOLD then release. And also in the link, the character goes to array [2] while here goes to [1]. Why?
    – 5argon
    Aug 25, 2016 at 11:23
  • well, as shown in my example, you do it in two steps, first you press the key, then there's the delay, then you release it. Well, then you'd need to lecture yourself on how hardware keyboard implementation works, it's kind of not natural, and each value of the array have a meaning. Pardon me for not having a link to share atm, but whenever you find something searching the web, please post a new comment ☺
    – zmo
    Aug 25, 2016 at 12:14
10

I created a project that fully replaces the Arduino Bootloader. You are able to upload sketches and use it as HID. See Project HID on Github: https://github.com/NicoHood/HID

4
  • 5
    Welcome to Arduino SE! Can you add a little bit about how to use your library? Thanks! Aug 7, 2014 at 2:25
  • 2
    Read the readme and try the examples? Its all there, no need to dupe everything here. Everything on Github is up to date :)
    – user3486
    Aug 8, 2014 at 9:19
  • 2
    Sorry dude, it looks like a cool library, but your documentation is crazy lacking. There's no real installation instructions as far as I can tell, and entire topics in your wiki just say "TODO" (specifically the gamepad topic is completely empty like this). :-/ Mar 12, 2016 at 23:23
  • 4
    Actually there is a need to present a basic description here: answers are required to be meaningful even without the external link, which could die. And this one is not. Mar 3, 2018 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.