Skip to main content

How to Use Arduino as a Keyboard or Mouse

What is Digitala Inputmouse/keyboard Pull-Up resistor?emulator?

InA Arduino,mouse/keyboard emulator on Arduino means the board pretends to be a Digitalreal InputUSB Pull-Upkeyboard Resistoror mouse when plugged into a computer.

Instead of sending data over serial like a normal Arduino sketch, the board identifies itself as a USB HID device (Human Interface Device), the same category used by real keyboards and mice. Visit here for more information.

This tutorial is an internal resistor that you can enablebased on the circuit of a simple button with a digital input pinsresistor. Please refer to ensure a known default voltage level (HIGH) when the pin is not actively connected to anything (i.e. it’s “floating”).

We have anotherthis tutorial showingfor howthe wiring.

<ins class="diffmod">Keyboard/Mouse Emulation vs Serial Communication</ins>

Arduino HID vs Serial Communication

FeatureHID EmulationSerial Communication
PurposePretend to be keyboard/mouseSend data between devices/programs
How PC sees itKeyboard, mouse, joystickCOM port / serial device
Driver neededUsually noUsually USB serial driver
Human input simulationYesNo
Can type into apps directlyYesNo
Can move mouse cursorYesNo
Communication typeHID protocolUART/USB Serial
Typical librariesKeyboard.h, Mouse.hSerial.h
Data visibilityActs as user inputRaw data stream
Best forAutomation/macros/controllersSensors/debugging/device communication
Security sensitivityHigherLower
Ease of debuggingHarderEasier
Works in BIOS/login screenOften yesUsually no
Bidirectional communicationLimitedExcellent
Speed for data transferLowerBetter for data
Common boardsLeonardo, Micro, UNO R4 seriesAlmost all Arduino boards

In short, if you only need simple interaction (no raw data and two-way communication) to replace the mundane mouse and keyboard, the HID Emulation is easier to set up and more straightforward to use a button the normal way.

Advantage of using a button this way

  1. Fewer External Components

    • No need for an external resistor—the internal pull-up does the job.
    • Makes circuits simpler and saves space, especially on a breadboard.
  2. Stable Default State (HIGH)

    • Ensures the input pin has a known state (HIGH) when the button is not pressed.
    • Prevents floating inputs, which can cause random or noisy readings.
  3. Simpler Wiring

    • You only need to wire the button between the pin and GND.
    • Ground is often easier to route in a circuit than Vccserial (especially with many buttons).

Wiring

  • one pin to GND
  • one pin to 2
communication.

Getting started

This code will make every button click into pressing a "w" on the keyboard.

#include <Keyboard.h>
boolean prevBtnState = LOW;

void setup() {
  Keyboard.begin();
  delay(1000);

  pinMode(2, INPUT_PULLUP); // Internal pull-up enabled
  Serial.begin(9600);
}

void loop() {
  int buttonStatebtnState = digitalRead(2);
  Serial.println(buttonState);if //( ReadsbtnState == LOW && prevBtnState == HIGH when) not{
      pressed,Serial.println("1");
      LOW when pressed (to GND)Keyboard.press('w');
      delay(100);
      Keyboard.releaseAll();
      delay(1000); 

  }
  prevBtnState = btnState;
}

After the code is uploaded, you may see a pop-up like this, as your computer now sees the Arduino as an actual keyboard! keyboardSetup.png