Arduino UNO R4 Minima USB HID

Learn how to use the UNO R4 Minima as a mouse/keyboard.

In this tutorial you will learn how to emulate a mouse/keyboard using an Arduino UNO R4 Minima board with the Keyboard and Mouse APIs.

This feature can be used to create game controllers, keyboard extensions or other HID devices.

Goals

The goals of this tutorial are to:

  • Learn how to emulate a keyboard (keypresses),
  • Learn how to emulate a mouse (x,y coordinates).

Hardware & Software Needed

Human Interface Device (HID)

Human interface devices (HID) are devices designed for humans (keyboards, mice, game controllers etc.), that frequently send data over USB to a computer. When you press a key on a keyboard, you send data to a computer, which reads it and in turn activates the corresponding key.

The UNO R4 Minima has built-in support for HID, a feature found on most modern day development boards, but not on previous UNO revisions.

To turn your board into an HID, you can use the keyboard/mouse API that is built in to the Board Package. You can visit the documentation for this API in the language reference at:

In the section below, you will see a couple of useful examples to get you started!

Keyboard

To use keyboard functionalities, we need to include the library at the top of our sketch. The Keyboard class contains several methods that are useful to emulate a keyboard.

1#include <Keyboard.h>
2
3Keyboard.method()

Keyboard Example

To emulate a keyboard, we can use the

press()
and
releaseAll()
methods. This will emulate a keypress, as well as releasing the keypress. The following example prints a "w" every second.

1#include <Keyboard.h>
2
3void setup() {
4 Keyboard.begin();
5 delay(1000);
6}
7
8void loop() {
9 Keyboard.press('w');
10 delay(100);
11 Keyboard.releaseAll();
12 delay(1000);
13}

To see more examples, please refer to links below:

Mouse

To use mouse functionalities, we need to include the library at the top of our sketch. The Mouse class contains several methods that are useful to emulate a mouse.

1#include <Mouse.h>
2
3Mouse.method();

Mouse Example

The following example moves both axis of mouse just slightly (10 points), back and forth.

1#include <Mouse.h>
2
3void setup() {
4 Mouse.begin();
5 delay(1000);
6}
7
8void loop() {
9 Mouse.move(10,10);
10 delay(1000);
11 Mouse.move(-10,-10);
12 delay(1000);
13}

To see more examples, please refer to links below:

Summary

In this tutorial, we have demonstrated some basic HID usage with the UNO R4 Minima. To view the full API, please refer to the following APIs:

In there, you will find a detailed reference along with some good examples to get you started with HID features.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.