Skip to content

Arduino - Keyboard Message

Certain Arduino boards can act as a Human Interface Device (HID), such as a keyboard or mouse. This allows your Arduino to send keystrokes to a connected computer. This feature is available on boards with native USB functionality, like the Leonardo, Micro, Due, Zero, MKR family, and UNO R4.

In this tutorial, we’ll make a button press send a message to the computer, counting how many times it has been pressed. Once programmed, simply open any text editor to see the output.

WARNING: When you use the Keyboard library, the Arduino takes control of your computer’s keyboard. To avoid losing control, always include a physical control (like a button) in your circuit to trigger the keyboard commands, as shown in this example.

  • 1 × Breadboard
  • 1 × Arduino Leonardo, Micro, or other HID-compatible board
  • 1 × Momentary Pushbutton
  • 1 × 10kΩ Resistor (pull-down)
  1. Connect one leg of the pushbutton to the Arduino’s 5V pin.
  2. Connect the opposite leg to a digital input pin (e.g., pin 4).
  3. Connect the 10kΩ resistor from that same leg (pin 4) to GND. This is a pull-down resistor, ensuring the pin reads LOW when the button is not pressed.

Method 2: The Modern Approach with INPUT_PULLUP

Section titled “Method 2: The Modern Approach with INPUT_PULLUP”
  • 1 × Breadboard
  • 1 × Arduino Leonardo, Micro, or other HID-compatible board
  • 1 × Momentary Pushbutton
  1. Connect one leg of the pushbutton to a digital input pin (e.g., pin 4).
  2. Connect the opposite leg to the Arduino’s GND pin.

This method is simpler and more reliable. It uses the microcontroller’s internal pull-up resistor, eliminating the need for an external one. This is the recommended approach.

Arduino Code (for Method 2 with INPUT_PULLUP)

Section titled “Arduino Code (for Method 2 with INPUT_PULLUP)”

This code uses the simpler INPUT_PULLUP circuit and includes debouncing to prevent single presses from registering multiple times.

/*
Keyboard Message Sender
For HID-compatible Arduino boards.
Uses the recommended INPUT_PULLUP method with debouncing.
*/
#include "Keyboard.h"
// Constants for pins and settings
const int BUTTON_PIN = 4; // Pin connected to the pushbutton
const long DEBOUNCE_DELAY = 50; // Debounce time in milliseconds
// Variables to track button state and timing
int buttonState = 0; // Current reading from the input pin
int lastButtonState = HIGH; // Previous reading from the input pin
unsigned long lastDebounceTime = 0; // Last time the output pin was toggled
int pressCounter = 0; // Counter for button presses
void setup() {
// Set the button pin as an input with an internal pull-up resistor.
// The pin will be HIGH when the button is not pressed, and LOW when pressed.
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Initialize keyboard control
Keyboard.begin();
}
void loop() {
// Read the state of the switch into a local variable:
int reading = digitalRead(BUTTON_PIN);
// Check if the button state has changed (from HIGH to LOW or LOW to HIGH)
if (reading != lastButtonState) {
// Reset the debouncing timer
lastDebounceTime = millis();
}
// If the button state has been stable for longer than the debounce delay,
// it's a confirmed state change.
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
// If the state has changed, and it's a valid press event
if (reading != buttonState) {
buttonState = reading;
// We only want to type when the button is pressed (goes LOW)
if (buttonState == LOW) {
pressCounter++;
Keyboard.print("Button pressed ");
Keyboard.print(pressCounter);
Keyboard.println(" times.");
}
}
}
// Save the current reading for the next loop iteration
lastButtonState = reading;
}
  • #include "Keyboard.h": This line includes the Arduino Keyboard library, which contains all the functions for emulating a keyboard.
  • pinMode(BUTTON_PIN, INPUT_PULLUP): This configures the pin as an input and connects an internal resistor to 5V. This means the pin reads HIGH by default and LOW only when the button is pressed (connecting it to GND).
  • millis()-based Debouncing: The code checks that the button’s state has remained stable for DEBOUNCE_DELAY milliseconds before registering it as a valid press. This is a robust way to handle the noisy electrical signal from a mechanical switch.
  • Keyboard.print(): This function sends keystrokes to the connected computer, just as if you were typing on a real keyboard.

After programming your board, unplug the USB cable. Open a text editor on your computer and place the cursor in the typing area. Plug the Arduino back in. Now, each time you press the button, a new line of text should appear in your editor, reporting the updated press count.