Arduino UNO as HID

I am trying to build a keyboard and a mouse which is connected to PC by USB and with a few buttons/Joystick as inputs the Arduino UNO reads those inputs and presses the specified keys. Basically Arduino UNO used as a HID. If anyone has a tutorial related to my topic, it would be I thankful of you.

Did you even try Googling: arduino uno hid keyboard?

How to Make a Arduino HID Keyboard

Arduino Uno as a USB HID interface

Arduino USB HID Keyboard - MitchTech

3 Likes

I would drop the Uno and use a board that has native USB. Arduino Leonardo if you need the same form factor as the Uno, Arduino Micro if you want / need something smaller and SparkFun Pro Micro if you need even smaller than the Arduino Micro; there are plenty other choices.

Note:
Only Unos with the 16U2 can be modified to be used as a HID; clones often don't use that 16U2 (but e.g. CH340) and in that case you can forget to use the Uno as a HID.

4 Likes

I thought the 8U2 can also do that.

Who is the company that made your uno?

I've never seen an Uno with 8U2 :wink: But you are right.

1 Like

Yes, I did but because of the difference in every video, I was confused. Thanks for letting me know

How can I identify if my UNO is a clone or an original one?

From the model numbers of the chips on the board.

But, seriously, even on a genuine Uno, using it as HID is very difficult for a beginner. Using one of the other boards recommended by @sterretje would be much more achievable.

2 Likes

There is also the option of using V-USB with Arduino UNO, but as PaulRB points out, it isn't completely straight-forward.

Be aware that some clones use the 16U2.

As indicated by @PaulRB in reply #8 is one way. The chip (right bottom) in below image is the Atmel 16U2; the common CH340 is a rectangular chip, not a square one).

image

The other indication that the chip is programmable is the 6-pin header above it (marked with red).

1 Like

I tried the 2nd link and tried to make it. Instead of buttons I used a analog Joystickg and when I plug in my arduino, my laptop spam opens word and edge and narrator. I think it spam opens an application which is on the taskbar which is located at a specific place like earlier, I had spotify in my taskbar and when I tried to plug my arduino in, it used to spam open spotify, so I uninstalled it thinking it was a glitch with Spotify but now it happens same thing for Edge as it is in the placein the taskbar where earlier spotify was.

The point is not whether it's a clone or not, but whether it has a 16U2 (or 8U2) chip.

It is the 16U2 (or 8U2) chip which gives the capability to do USB HID.

Note that "16U2" (or "8U2") is short for ATmega16U2 (or ATmega8U2):

1 Like

Does the UNO R4 have that?

Why, yes - so it does:

1 Like

Did you try the unmodified code? Did that work OK?

Not sure what you mean by that?

1 Like

If it plays well with an Uno that I cannot guarantee. But building stand-alone uC USB enabled circuits it works great for.

My UNO was manufacured by Arduino, and I have taken it from my School, so I am not able to prove where it was actually manufactured from, but by looking from it it looks like an original one. And talking about my project, I am succesfully able to configure the Arduino as a HID, it also shows up on Device Manager but the problem is that when I plug my Arduino into my Laptop, my laptop spam opens applications like Word, Narrator, Edge and other applications even when I am not giving any input. As a input I was using an Analog Joystick due to the lack of various inputs. I followed the instructions from this tutorial. I would be thankful if you could help me related to my problem.

I was unable to understand what you meant by the "unmodified code", I would request you to state it clearly so I can understand it well. As "my laptop spam opens word and edge and narrator" I meant that when I plug my Arduino into my Laptop, my laptop constantly opens applications like Word, Narrator, Edge and other applications even when I am not giving any input. It may be a fault in any hex file which I uplaoded to modify the firmware of the Arduino or may be a fault in hardware. ts. I followed the instructions from this tutorial.

That's good.

That's not so good (I guess :slight_smile:) Post your code.

1 Like
uint8_t buf[8] = { 0 };   //Keyboard report buffer

#define X_AXIS A0  // Analog pin for X-axis
#define Y_AXIS A1  // Analog pin for Y-axis

#define FORWARD_THRESHOLD 600 // Define a threshold for moving forward
#define LEFT_THRESHOLD 500    // Define a threshold for moving left

void releaseKey() 
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Send Release key  
}

void setup() {
  Serial.begin(9600); // Setup Serial communication
}

void loop() {
  int xValue = analogRead(X_AXIS);
  int yValue = analogRead(Y_AXIS);

  Serial.print("X: ");
  Serial.print(xValue);
  Serial.print(" | Y: ");
  Serial.println(yValue);

  if (yValue < FORWARD_THRESHOLD) { 
    buf[2] = 24;   // W keycode
    Serial.write(buf, 8); // Send keypress
    releaseKey();
  }

  if (xValue < LEFT_THRESHOLD) {
    buf[2] = 26;   // A keycode
    Serial.write(buf, 8); // Send keypress
    releaseKey();
  }
}

As "spam opesn words" I mean that when I plug my Arduino into my Laptop, my Laptop cotinuously opens applications like Word, Edge, Narrator etc.