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:

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.

Advertisements
This entry was posted in microelectronics fun and tagged , , , , . Bookmark the permalink.

16 Responses to USB-Keyboard with Arduino and V-USB library, an example

  1. Hello, I am trying to do this project but I am getting an error message with the drivers, it does not recognize it as a HID it says that is an unknown device, Can you help me, please?

    • 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”.

    • 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.)

  2. Nice post! And also thanks for providing a link to my USB tutorial. 🙂 I also read your Ponoko article, very topical since I’ve been thinking of trying them out, too!

  3. Pingback: V-USB tutorial continued: HID mouse » Code and Life

  4. Lucas says:

    Hi, in fuction delayMs, one error with the for…

    • Florian says:

      Thank you! I just corrected the code.

      • Lucas says:

        would be possible to use the serial arduino’s own, to make the joystick?

      • Florian says:

        This would be possible with the Arduino Uno or newer. Older versions of the Arduino just provide serial communication capabilities with the USB connector (as far as I know). A very quick search for “Arduino Uno joystick” gave, for example, http://hunt.net.nz/users/darran/weblog/a3599/. This approach works as long as your target device works well with arbitrary aggregations of USB devices (usually every PC), because the Uno, Leonardo and other newer Arduino-like boards provide USB serial communication and USB keyboard/mouse/joystick at the same time. An example, where this does NOT work is, for example, the iPad with iOS. Here, a “USB-keyboard” only device needs to be implemented. For this reason I designed the Pancake board (https://petrockblog.wordpress.com/2012/07/01/pancake-board-arduino-compatible-minimum-configuration-breadboard-for-atmega168328-with-usb-connector/), which has a USB connector that can be used in an arbitrary way. The downside is that one needs an ISP to transfer programs on the micro. Besides the Pancake board there are of course many other boards with a comparable capability out there …

  5. Pingback: Vusb schematic | Mkirankumar

  6. 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:

      Thanks!
      Unfortunately, I do not have this example in VB6. If you know a good tutorial about VB6 and ATMega programming, feel free to post it here!

  7. 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.

  8. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s