AVRUSB running on Arduino hardware with minishield

Hi all...

Just a quick update on getting AVR-USB to run with the Arduino.

I've just managed to put together a "mini" expansion shield with a USB "B" socket and the 2 diodes and 3 resistors needed to create a USB connection to the Arduino hardware. (Have plans to upload schematic/diagram/photos eventually.)

After some code modifications (to fit using ATmega168 etc) I got Linux and OS X recognising the demo devices. I'm currently trying to get HID keyboard data to be recognised.

So, while it's not yet integrated with the Arduino libraries it's at least progress to have a shield working and the code running on the Arduino hardware.

You can follow AVRUSB on Arduino progress here.


Where did you get the USB receptacle and how much did you pay for ?


digikey, sparkfun, futurlec all carry them and they are usually around $1.

I've now got the PowerSwitch USB echo demo working from an Arduino sketch as I'm flashing an LED every second.

This test sends a character from the host (a Linux box in this case) and the USB device responds with the same character.

I haven't made any changes to the core Arduino code which will probably result in an issue if an interrupt happens at the wrong time.

But it's something, and it's currently flashing away happily. :slight_smile:

Here's the exciting screenshot:

sudo ./powerSwitch test
test succeeded

(Imagine LED still flashing here.)


Oh man! This is awesome, I will most definitely be following your progress.


Just a quick update that last night I managed to get the Arduino to function as a USB HID keyboard (with one button!) when connected to a Mac OS X 10.4 machine. I had previously had some success connected to an Ubuntu box.

I managed to send standard characters, modified keystrokes (e.g. Command-B), arrow keys and function keys--the latter of which I used to bring the Dashboard up on screen whenever I pushed the button attached to the Arduino. :slight_smile:

So, the short answer does seem to be that at least at some level AVRUSB and Arduino can be compatible--I'm not sure if there is a point at which one or other of them will break as I haven't tried anything very sophisticated.

I'm hoping that my web server will be back on line soon so I can upload more details.


:slight_smile: good news

That's sweet! Please post a reply when you get this up on your website - I am very interested in doing something similar to this, and a simple howto / tutorial would go a long way!


I'm hoping that my web server will be back on line soon so I can upload more details.

Great learning about this with you on IRC, thanks a lot. Now we all know you really really want to upload some kicad files for us ;D

Best wishes...

Okay, okay...

I have just uploaded photos of the USB mini-shield for Arduino on my 9 July 2008 project log entry which I think should be enough for you to recreate it on strip- or vero-board. (Real board layout not...er, "complete". :slight_smile: )

For those that care, the current implementation isn't 100% ideal because it would be nice if the "device present" resistor could be controlled by the microcontroller to be "more polite" in conjunction with the bootloader but that's not a huge deal. (But at the cost of an extra microcontroller pin it might have some benefits at a later date.)

Let me guess, now you'll be asking for some code & compilation instructions? :stuck_out_tongue:


Edit: I've now added a link to a schematic exported from KiCad in SVG format to the page.

Hi all,

Just a quick note to say I've uploaded the arduinousb library and an example sketch--both tailored to create a USB HID keyboard device.

Here's the guts of an example:

#include "UsbKeyboard.h"

# ...[snip]...

void loop() {

  if (digitalRead(BUTTON_PIN) == 0) {
    //UsbKeyboard.sendKeyStroke(KEY_B, MOD_GUI_LEFT);

    //UsbKeyboard.sendKeyStroke(KEY_B, MOD_GUI_LEFT);



Another quick update, I've added links to the project page of user-submitted code contributions implementing USB mouse and keyboard functionality.

Thanks Michel!


I'm having a bit of trouble getting this to work:

hardware\libraries\UsbKeyboard/UsbKeyboard.h:139: error: 'byte' has not been declared

hardware\libraries\UsbKeyboard/UsbKeyboard.h:143: error: 'byte' has not been declared

hardware\libraries\UsbKeyboard/UsbKeyboard.h:143: error: 'byte' has not been declared

hardware\libraries\UsbKeyboard/UsbKeyboard.h: In constructor 'UsbKeyboardDevice::UsbKeyboardDevice()':

hardware\libraries\UsbKeyboard/UsbKeyboard.h:127: error: 'sei' was not declared in this scope

hardware\libraries\UsbKeyboard/UsbKeyboard.h:131: error: 'memset' was not declared in this scope

hardware\libraries\UsbKeyboard/UsbKeyboard.h: In member function 'void UsbKeyboardDevice::sendKeyStroke(int, int)':

hardware\libraries\UsbKeyboard/UsbKeyboard.h:151: error: 'memset' was not declared in this scope

Any suggestions would be much appreciated!

Any suggestions would be much appreciated!

My first suggestion would be to try with IDE 0011 rather than 0012 if at all possible. The issue is caused by a change from the 0011 environment in the 0012 environment.

My second suggestion if you need to use 0012 is to try to apply this patch to UsbKeyboard.h (but be warned it was hacked up late one night as I was trying to meet a deadline--and the final result doesn't work as reliably as previous attempts but at present I am unsure if that is hardware or software related):

--- UsbKeyboard.h~      2008-09-24 22:45:15.000000000 +1200
+++ UsbKeyboard.h      2008-09-24 23:46:34.000000000 +1200
@@ -10,6 +10,15 @@
 #include "usbdrv.h"
+#include <avr/interrupt.h>
+#include <string.h>
+// #include "WConstants.h"
+typedef uint8_t byte; // avoid int() issue 
+#include <util/delay.h>
 #define BUFFER_SIZE 4 // Minimum of 2: 1 for modifiers + 1 for keystroke 
@@ -124,7 +133,8 @@
     // Control pull-up resistor
-    delay(500);
+    //delay(500);
+    _delay_ms(500);

(If you don't know how to interpret the patch formatting I can try resupplying it in a different form.)

Please let me know if this helps you or not.


P.S. As always, I'm keen to learn what people are using the code for. :slight_smile:

Has this been tested with the Arduino NG (Atmega8)?

I haven't tested on an ATmega8.

If you modify the configuration defines and recompile it, it should probably work I'd think.


FYI, with your little patch it does compile and upload in arduino-0012.

I am ordering parts tomorrow and with any luck at some point I'll feel like putting together a look-alike of the example minishield and giving this a try :slight_smile:

My intention would be to build a super cheap serial-to-usbHID converter for use in entertainment automation of power point stuff (to be controlled by an Alcorn Mcbride V16+). Retail devices that do such a thing are always expensive or a pain, and just plain not fun.


Many thanks for this library and the schematic. I was able to go from a complete microprocessor noob to having the "hello world" USB demo working in a weekend (would have been less had I not had to make quite so many trips to Radio Shack!).

I have found one issue - and I don't know if it's my setup? The USB device only seems to correctly connect through a hub - if I attach it directly to a USB port on the computer it fails. However, if I attach a hub to that port and then attach my device to this hub it works.

My Arduino environment:
Arduino Nano, Arduino-012 IDE (*), ArduinoUSB_release_001 running UsbKeyboardDemo1

Host OSs tested include Linux (SuSE) and Windows XP.

(*) Note, I patched the code as per the patch file posted on this thread. However, only the first part - the file did not seem to contain "// Control pull-up resistor".

Any help greatly appreciated.

Although, I'm still enjoying pressing my button and seeing "hello world" magically appear! ;D

hello world
hello world
hello world
(as "typed" by my Arduino Nano)

Many thanks!

I am confused by the patch for IDE 0012. Would someone mind posting the patch for download or maybe a little better explanation for a complete noob like myself? Thanks :slight_smile: