USB-Keyboard with Arduino and V-USB library, an example

This is another short example of a demo application that makes use of the V-USB library together with an Arduino. I show, how an USB keyboard functionality can be implemented.

The motivation: The V-USB library provides a quite simple possibility to realize USB devices with arbitrary USB descriptors. This is useful, for example, if the device that you want to connect to can only handle certain types of USB devices. The V-USB library allows you to implement exactly the device descriptor that can be understood by the other device.

This exemplary project is a summary from various sources. I do not want to repeat all the detailed explanations from these sources in the following, but rather show how to build a working example. For a deeper understanding I give a list of usefull links that should help for an understanding at the end of this post.

This example makes use of the Arduino 1.0 IDE and the fifth alpha release of the V-USB library. To setup the USB keyboard, we need the following parts:

    • 1 Arduino board. I have a Duemilanove, but I assume that any other board (even any other ATMEGA controller?) would also do.
    • For the USB interface:
      • 1x USB “B” female socket
      • 1x 2K2 (i.e., “2.2 kilo Ohms”) resistor, 1/4W
      • 2x 68R (i.e., “68 Ohms”) resistor, 1/4W
      • 2x 3.6V Zener diode, 1/4W

For the button:

  • 1x switch/push button etc.
  • 1x 10K resistor

For the assembling, we need a breadboard and some jumper wires. I used these schematic for setting up the button circuit:

To ease the connection to the USB socket, I prepared the following image, which indicates the pin out of a USB-B female socket:
For connecting the USB socket, I used this schematic:

To give you an idea about how everything could look after assembling, here is my circuit:

If you have not installed the V-USB library already, download the library and unzip it into the “libraries” directory of your Arduino folder. No modifications of the library files are needed in order to make this example work. The following program initializes the USB keyboard and iteratively checks, if the button is pressed. If a press is recognized, the letter “a” is send via the USB keyboard interface. This is the source code, that you simply have to compile and upload with the Arduino IDE:
[gist id=3051606]

More details about this small USB keyboard project can be found in the book “Practical Arduino“. If you feel that you lack some more basic understanding of the Arduino and microelectronics, the short book “Getting started with Arduino” provides a really nice introduction to these topics. Detailed tutorials about the V-USB library can also be found in the blog of Joonas Pihlajamaa.

45 thoughts on “USB-Keyboard with Arduino and V-USB library, an example

  1. Mark says:

    Hello, i tried to launch same example but without success. Whenever it runs “UsbKeyboard.sendKeyStroke” command, it hangs there. Any ideas? Thank you.

  2. ale8oneboy says:

    Thank you! Works great on a Mac. Having issues on Windows. But I’m not surprised. If I figure out anything I’ll post the solution I find.

  3. Tam says:

    Hello geeks! :-P
    I did a succesful project on the vusb library and m quite impressed!!
    I was thinking that is there a way that i can read keyboard strokes (with the vusb library )like capslock, shift, cntrl keys n detect whether they are press down or up…then it would be of great help to me…
    Plz help.
    Thanx to all in advance!

  4. moi says:

    Hello, I try to launch this example but without success. I get some errors like that “…librariesUsbKeyboard/usbdrv.h:479:26: error: variable ‘usbDescriptorDevice’….” on the 1.6.4 Arduino IDE. Any idea? thank you.

  5. pablo iglesias says:

    Hello People

    I had this problem, ” this is a unknown device ” , i solve it with a simple solution,
    the resistor 2k2 ohm , i’m replaced it with a 1k8 resistor , but it not Works again, i’ve replaced with a 1k ohm , it Works, but failled again in a few seconds, i had a new replacement, the 1k ohm resistor, was replaced with a 470 ohm and it Works perfect !!!
    The installation icon, in the taskbar shows installing a usb keyboard….

    • Emrah AKBIYIK says:

      Hello Pablo, i tried 470R instead of 2k2. but problem continue. it’s appearing “unknown USBdevice”. Also shown this in detailed description seciton. –> USBDEVICE_DESCRIPTOR_FAILURE. Could you advice the solution to me?

  6. Goutham Vijapur says:

    I rigged up the circuit as shown and uploaded the code onto my Arduino Uno. Bt when I connect it it to my windows systwm it shous ‘USB device not found’. I checked my circuit many times for faults, but I couldnt find any! Please help me with the problem…

  7. adammhaile says:

    I keep seeing the device show in Windows Device Manager with the error “USB Device descriptor request failed”. I’ve checked the circuit about a 100 times but it looks good. I am using 0.5W zener diodes instead of 0.25W… could that be it? Various sites say it should be OK, but I’m grasping here.

    • Goytham Vijapur says:

      I rigged up the circuit as shown and uploaded the code onto my Arduino Uno. Bt when I connect it it to my windows systwm it shous ‘USB device not found’. I checked my circuit many times for faults, but I couldnt find any! Please help me with the problem

  8. Halucyn says:

    I made a circuit on a universal pcb board and it’s not working, but i’ not sure in which direction should i insert zener diodes and i’m not sure about pins on my USB socket, because it’s got pins on it’s back, not on bottom

  9. borgar says:

    Brilliant, got it working- it seems like rejects it every once in a while, but that may be loose connections from the breadboard as i move it between computers to test.
    thanks for making such a detailed writeup. the examples that come with the library are pretty cryptic.

    I’m trying this on an attiny using high-low techs arduino isp method.
    i’ve seen it done on tinys before, but using avr dude. i want to stick to the arduino IDE.

    when i try to upload no the attiny 2313 i get “timsk0 was not decleared in this scope”
    i’m guessing the ide wont upload it because the library contains functions unavailable to attinys.
    so they dont get decleared.

    do you have any idea how to circumvent this?
    could you explain what defines the Vusb pins? they are not decleared in the sketch.

    Thanks again

  10. Andrew says:

    I tried to verify this code with a UNO, it gives an error,”UsbKeyboard.h:132: error: ‘USB_CFG_DPLUS_BIT’ was not declared in this scope” and more…any ideas on what I am doing wrong.

  11. Wu Feng says:

    Hello, most interesting. Do you have the example for Visual Basic 6? I would like to use VB6 instead of C as it more powerful and more widely popular.

    • Florian says:

      Hi,
      at first make sure that you are using component with the exact specification as given above.
      Then check, if all connections are correct. I rememeber, when I set this up first, that I did not connect D5 with D- (with a resitor in between), but with D+.
      If you are very, very sure that the hardware setup is correct, open the “Console” (Linux/Mac) or “Device Manager” (Windows) or “USBProber” (Mac) etc. to see, which (USB) device exactly is recognized by your system.
      Try to connect the USB cable first to your own device and then to the PC or USB hub. I am not sure why, but in my case this was an issue once.
      Good luck and don’t give up!

      • Igor says:

        The problem might be with Windows, in my case: everything works perfect under Linux, but Windows complains about “unknown device”.

      • Oscar says:

        I have the same problem, Windows 7 Home Starter don´t recognize the USB-B female socket, appears this message “unknow device”.

      • Sergey says:

        The problem might be with AMD motherbords. The intell work fine. I have tested it on 8 computers – only one (AMD based) is not worked…

    • Igor says:

      Are you using 0.25W zeners or 0.5W?
      Not sure whether this is crucial, but in my case (0.5W) Linux works, Windows – don’t.
      (Also, these are different PCs, I shall try on several different ones.)

Leave a Reply

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.