Emulating USB HID keyboard and mouse with Arduino is one of the most practical maker skills — it turns your microcontroller into an invisible input device for automation, accessibility tools, or creative projects. This tutorial covers the hardware requirements and complete code for keyboard, mouse, and combined HID device projects.
Table of Contents
- Understanding USB HID
- Compatible Hardware
- USB Keyboard Project
- USB Mouse Project
- USB Gamepad
- Indian Use Cases
- Frequently Asked Questions
Understanding USB HID
USB HID (Human Interface Device) is a USB device class that doesn’t require drivers — every modern OS (Windows, Linux, macOS, Android) has built-in HID support. When your Arduino presents itself as an HID keyboard or mouse, the OS immediately recognises it and processes inputs as if from a real hardware keyboard or mouse. No driver installation, no custom software needed on the host.
For Indian makers, this opens up automation (pressing keyboard shortcuts, moving mouse for repetitive tasks), accessibility devices (voice control to keyboard), and custom gaming peripherals without any installed software.
Compatible Hardware
Not all Arduino boards support USB HID. You need a board with native USB support:
- Arduino Leonardo / Pro Micro (ATmega32U4): Native USB HID — best option in India for cost. Pro Micro available for ₹200–400
- Arduino Due (SAM3X8E): Native USB HID — more powerful but more expensive
- Arduino Micro: Like Leonardo in Nano form factor
- ESP32-S3: Native USB OTG — supports HID via TinyUSB library
- RP2040/RP2350: Native USB HID via Adafruit TinyUSB
Arduino Uno, Mega, Nano do NOT support native USB HID — they have a USB-to-UART bridge (CH340/ATmega16U2). Avoid these for HID projects.
USB Keyboard Project
#include <Keyboard.h> // Works on Leonardo, Pro Micro, Due
const int BUTTON_PIN = 2;
bool lastButtonState = HIGH;
void setup() {
Keyboard.begin();
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
bool currentState = digitalRead(BUTTON_PIN);
if (currentState == LOW && lastButtonState == HIGH) {
// Button pressed - type a macro
Keyboard.begin();
delay(10);
// Example: Open Run dialog and type a command
Keyboard.press(KEY_LEFT_GUI); // Windows key
Keyboard.press('r');
Keyboard.releaseAll();
delay(500);
Keyboard.println("notepad");
Keyboard.write(KEY_RETURN);
delay(1000);
// Type some text
Keyboard.println("Automated by Arduino Made in India!");
Keyboard.releaseAll();
}
lastButtonState = currentState;
delay(50);
}
Available key codes: KEY_F1–KEY_F12, KEY_LEFT_CTRL, KEY_LEFT_SHIFT, KEY_LEFT_ALT, KEY_LEFT_GUI (Win/Cmd), KEY_RETURN, KEY_BACKSPACE, KEY_DELETE, KEY_TAB, KEY_INSERT, KEY_HOME, KEY_END, KEY_PAGE_UP, KEY_PAGE_DOWN, and all standard printable characters.
USB Mouse Project
#include <Mouse.h>
const int X_PIN = A0; // Joystick X
const int Y_PIN = A1; // Joystick Y
const int CLICK_PIN = 2;
void setup() {
Mouse.begin();
pinMode(CLICK_PIN, INPUT_PULLUP);
}
void loop() {
int xRaw = analogRead(X_PIN); // 0-1023
int yRaw = analogRead(Y_PIN);
// Map to -10 to +10 range
int xMove = map(xRaw, 0, 1023, -10, 10);
int yMove = map(yRaw, 0, 1023, -10, 10);
// Dead zone in centre
if (abs(xMove) < 2) xMove = 0;
if (abs(yMove) < 2) yMove = 0;
if (xMove != 0 || yMove != 0)
Mouse.move(xMove, yMove, 0);
if (digitalRead(CLICK_PIN) == LOW) {
Mouse.click(MOUSE_LEFT);
delay(100);
}
delay(10);
}
USB Gamepad
For a custom gamepad (popular for Indian gaming cafes and game developers), use the Joystick library with ATmega32U4:
#include <Joystick.h> // Install from Library Manager
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_GAMEPAD,
6, // 6 buttons
0, // 0 hat switches
true, true, false, false, false, false, false, false, false, false
);
void setup() { Joystick.begin(); }
void loop() {
Joystick.setXAxis(analogRead(A0));
Joystick.setYAxis(analogRead(A1));
Joystick.setButton(0, digitalRead(2) == LOW);
delay(10);
}
Indian Use Cases
- Custom macro pad for Photoshop/AutoCAD shortcut keys (popular with Indian graphic designers)
- Accessibility keyboard for physically challenged users
- Automated testing of software UIs without mouse/keyboard
- Custom gaming controller for Indian game developers
- Biometric login automation (fingerprint → keyboard login sequence)
Frequently Asked Questions
Does Arduino USB HID work with Indian Android phones?
Yes, via OTG cable. Android supports USB HID keyboards and mice natively — useful for hands-free tablet control at trade shows or accessibility applications.
Can I detect if the USB HID is connected to a computer?
On Leonardo/Pro Micro, check USB_CONNECTED or use Serial for status. Direct USB plug detection is not straightforward in the Arduino HID library.
How do I type Hindi or other Indian language characters via USB HID?
Use Keyboard.write() with Unicode code points in conjunction with OS input method editors (IME). Alternatively, use keyboard shortcuts for the OS IME to switch input language.
Add comment