UNO as USB HID Device Problem

Okay, so after 3 days of reading, trying things, reading more, and failing I am coming for guidance...

I'll start off with a background of my project. I am using an HID multiCLASS RFID reader hooked up to an Arduino UNO to read an RFID card and report back through keystrokes to the PC. My first attempt was trying to use AAC Keys and have it read the serial port of the Arduino and type it out... unfortunately it would not reliably type all of the numbers/letters. The second attempt was to type through a PS/2 port... I got that working but only on a few PCs. I ultimately wanted to go the USB HID Keyboard route to send the keystrokes and since I had an UNO then I was going to try and reprogram it to be a HID Keyboard. I ordered in a SparkFun AVR Pocket Programmer and got it in Monday and have been pulling my hair out trying to get it to work since then.

I tried to follow the instructions A.McD posted at Arduino Forum but he linked a post from ant.b http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1285962838 that I cannot access as it says, "Error: You are not allowed to access this section." -- So I found another post from ant.b on how to change the firmware on the 8u2 on making the joystick emulator and tried to adapt it to the keyboard. I also tried following Darrans instructions at http://hunt.net.nz/users/darran/ but it's still not working.

I've attempted loading the provided sketch, jumping the board to put it in DFU mode, then using FLIP to load the hex file provided on both of the above URLs (Darran and A.McD) but neither have started typing. Then I tried loading the sketches, hooking up the avr pocket programmer, and running avrdude using the commands:
avrdude -p at90usb82 -F -c usbtiny -U flash:w:Keyboard_.hex
avrdude -p at90usb82 -F -c usbtiny -U flash:w:Arduino-keyboard-0.3.hex

Both of these go through the process of writing to the ATMEGA and saying that it was successful but still no typing.

I'm really getting to the end of my attempts as I've tried what I could and I refuse to believe that it is supposed to be this difficult. So if anyone could assist or sees what I am doing wrong I'd much appreciate it.

TL:DR; I need assistance getting my Arduino UNO to act as an HID keyboard through Windows. Thanks!

linked a post from ant.b http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1285962838 that I cannot access as it says, "Error: You are not allowed to access this section."

Appears to be available here...
http://arduino.cc/forum/index.php/topic,111.0.html

Edit: link corrected.

That's not ant.b's post (which is giving the error) but rather a post by another person linking ant.b's post. Regardless, I am able to get the device to show up as a HID keyboard through some other posts I found but loading via the AVR Pocket Programmer and through FLIP I can't get it working.

Sorry about that. I posted the wrong link. Let me see if I can find the one I meant to post... There it is...
http://arduino.cc/forum/index.php/topic,111.0.html

I feel that I am doing something wrong when I try to write the hex file after uploading the sketch. By using the code at the http://arduino.cc/forum/index.php/topic,99.0.html example I also added a pin 13 LED blink to notify when the loop cycle comes around. I then attempted to write the hex via the AVR Pocket Programmer using avrdude:
avrdude -p at90usb82 -F -D -c usbtiny -U flash:w:Keyboard_.hex
avrdude -p at90usb82 -F -c usbtiny -U flash:w:Keyboard_.hex

The LED no longer blinks meaning it overwrote the sketch. So maybe it's writing correctly but erasing the written sketch.

No problem. I went off of that post and tried to adapt the joystick example to the keyboard and it's still not working.

avrdude -p at90usb82 -F -c usbtiny -U flash:w:Keyboard_.hex

Forced upload? What happens if you do not include that option?

Can you post the sketch you were using when you tried my keyboard firmware? Might be a problem with the interface to the atmega8u2.

Also it's really odd that your blink sketch stopped working after you programmed the 8u2. Silly question but are you sure you're connected to the 8u2 program header and not the atmega328's?

Darran.

Okay, so being new to the Arduino I made the rookie mistake of using the ICSP headers to try and program it instead of the 8u2. I tried it before but I guess my connector was on backwards. I hate making stupid mistakes like that but I suppose it's part of learning it all.

I do have my RFID reader reporting back the HID numbers through keyboard but the strangest thing happens. When I wanted to test it on another PC I plugged it in and nothing... I brought it back to my workstation and try it again to no avail. I did notice that pin 13 flashes 3 times, pauses for a second, flashes 3 times, and continues on like it's constantly resetting. I reflashed the 8U2 with the arduino-usbserial.hex, reupload my sketch, reflash with the keyboard.hex and it works perfectly. Unplug USB and plug back in and it does the same thing.

To clarify the new problem. I am running a HID multiCLASS RFID reader on pins 2 & 3 of the UNO. I am using the Arduino-keyboard-0.3.hex file on the 8U2 and using the following sketch:

uint8_t buf[8] = { 0 };	
volatile long reader1 = 0;
volatile int reader1Count = 0;

#define KEY_LEFT_CTRL	0x01
#define KEY_LEFT_SHIFT	0x02
#define KEY_RIGHT_CTRL	0x10
#define KEY_RIGHT_SHIFT	0x20

void reader1One(void) {
  reader1Count++;
  reader1 = reader1 << 1;
  reader1 |= 1;
}

void reader1Zero(void) {
  reader1Count++;
  reader1 = reader1 << 1;
}

void setup() {
  Serial.begin(9600);
  attachInterrupt(0, reader1Zero, RISING);
  attachInterrupt(1, reader1One, RISING); 
  delay(10);
  for(int i = 2; i<4; i++){
   pinMode(i, OUTPUT);
   digitalWrite(i, HIGH);
   digitalWrite(i, LOW);
   pinMode(i, INPUT);
   digitalWrite(i, HIGH);
  }
  delay(10);
  reader1 = 0;
  reader1Count = 0;  
  delay(200);
}

void loop() {
  if(reader1Count >=26){

    String HIDnum = ((reader1 & 0x1FFFF) >> 1);
    HIDnum = HIDnum+"~";
    char *HIDchars;
    HIDchars=&HIDnum[0];

    SendHIDChar(HIDchars);

    reader1 = 0;
    reader1Count = 0;
  }
}

void SendHIDChar(char *chp){
  while (*chp) {
    switch (*chp) {
      case ' ':  buf[2] = 0x2c; break;
      case '1':  buf[2] = 0x1E; break;
      case '2':  buf[2] = 0x1F; break;
      case '3':  buf[2] = 0x20; break;
      case '4':  buf[2] = 0x21; break;
      case '5':  buf[2] = 0x22; break;
      case '6':  buf[2] = 0x23; break;
      case '7':  buf[2] = 0x24; break;
      case '8':  buf[2] = 0x25; break;
      case '9':  buf[2] = 0x26; break;
      case '0':  buf[2] = 0x27; break; 
      case '~':  buf[2] = 0x28; break;
      default:  buf[2] = 0x37; break;
    }
    Serial.write(buf, 8);
    buf[0] = 0;
    buf[2] = 0;
    Serial.write(buf, 8);
    chp++;
  }
}

I am following these steps:

  • Upload the above sketch
  • Flash the Arduino-keyboard-0.3.hex to the 8U2
  • Scan RFID card and it properly outputs the HID number read from the card and a return.
  • Unplug the AVR Programmer and USB
  • Plug into different workstation. RFID reader powers up but the pin 13 L LED flashes continuously.

From here I have to reflash the arduino-usbserial.hex, reupload the sketch, then reflash the keyboard hex file and it works fine until I unplug the USB and plug back in.

Now strangely enough if I keep the AVR Programmer hooked up and unplug the USB and plug it back into a different workstation it works just fine and types as I scan the RFID card.

The bootloader on your board has a bug that results in the sketch being erased. Install this bootlaoder...

If you have two (or more) boards, westfw has graciously published a sketch named "optiloader" that dramatically simplifies the process.

Perfecto! Got it working... much appreciation to you guys :slight_smile: Been working since Tuesday morning on this one issue.